jBittorrentAPI
Class Piece

java.lang.Object
  extended by jBittorrentAPI.Piece

public class Piece
extends java.lang.Object

Class representing a piece according to bittorrent definition. The piece is a part of data of the target file(s)


Field Summary
private  java.util.TreeMap<java.lang.Integer,java.lang.Integer> filesAndoffset
           
private  int index
          Index of the piece within the file(s)
private  int length
          Length of the piece.
private  java.util.TreeMap<java.lang.Integer,byte[]> pieceBlock
          Map containing the piece data
(package private)  byte[] sha1
          SHA1 hash of the piece contained in the torrent file.
 
Constructor Summary
Piece(int index, int length, int blockSize, byte[] sha1)
           
Piece(int index, int length, int blockSize, byte[] sha1, java.util.TreeMap<java.lang.Integer,java.lang.Integer> m)
          Constructor of a Piece
 
Method Summary
 void clearData()
           
 byte[] data()
          Returns the concatenated value of the pieceBlock map.
 java.util.TreeMap getFileAndOffset()
           
 int getIndex()
          Return the index of the piece
 int getLength()
          Returns the length of the piece
 void setBlock(int offset, byte[] data)
          Set a block of data at the corresponding offset
 void setFileAndOffset(int file, int offset)
           
 java.lang.String toString()
          Print some information about the Piece
 boolean verify()
          Verify if the downloaded data corresponds to the original data contained in the torrent by comparing it to the SHA1 hash in the torrent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

filesAndoffset

private java.util.TreeMap<java.lang.Integer,java.lang.Integer> filesAndoffset

index

private int index
Index of the piece within the file(s)


length

private int length
Length of the piece. It should be constant, except for the last piece of the file(s)


pieceBlock

private java.util.TreeMap<java.lang.Integer,byte[]> pieceBlock
Map containing the piece data


sha1

byte[] sha1
SHA1 hash of the piece contained in the torrent file. At the end of the download this value must correspond to the SHA1 hash of the pieceBlock map concatenated

Constructor Detail

Piece

public Piece(int index,
             int length,
             int blockSize,
             byte[] sha1)

Piece

public Piece(int index,
             int length,
             int blockSize,
             byte[] sha1,
             java.util.TreeMap<java.lang.Integer,java.lang.Integer> m)
Constructor of a Piece

Parameters:
index - Index of the piece
length - Length of the piece
blockSize - Size of a block of data
sha1 - SHA1 hash that must be verified at the end of download
m - HashTable containing the file(s) this piece belongs to and the index in these
Method Detail

clearData

public void clearData()

data

public byte[] data()
Returns the concatenated value of the pieceBlock map. This represent the piece data

Returns:
byte[]

getFileAndOffset

public java.util.TreeMap getFileAndOffset()

getIndex

public int getIndex()
Return the index of the piece

Returns:
int

getLength

public int getLength()
Returns the length of the piece

Returns:
int

setBlock

public void setBlock(int offset,
                     byte[] data)
Set a block of data at the corresponding offset

Parameters:
offset - Offset of the data within the current piece
data - Data to be set at the given offset

setFileAndOffset

public void setFileAndOffset(int file,
                             int offset)

toString

public java.lang.String toString()
Print some information about the Piece

Overrides:
toString in class java.lang.Object
Returns:
String

verify

public boolean verify()
Verify if the downloaded data corresponds to the original data contained in the torrent by comparing it to the SHA1 hash in the torrent

Returns:
boolean