Package org.klomp.snark
Class PartialPiece
- java.lang.Object
-
- org.klomp.snark.PartialPiece
-
- All Implemented Interfaces:
Comparable<PartialPiece>
class PartialPiece extends Object implements Comparable<PartialPiece>
Store the received data either on the heap or in a temp file. The third option, to write chunks directly to the destination file, is unimplemented. This is the class passed from PeerCoordinator to PeerState so PeerState may start requests. It is also passed from PeerState to PeerCoordinator when a piece is not completely downloaded, for example when the Peer disconnects or chokes. New objects for the same piece are created during the end game - this object should not be shared among multiple peers.- Since:
- 0.8.2
-
-
Constructor Summary
Constructors Constructor Description PartialPiece(Piece piece, int len, File tempDir)
Used by PeerCoordinator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(PartialPiece opp)
boolean
equals(Object o)
Make this simple so PeerCoordinator can keep a List.int
getDownloaded()
How many bytes are good - as set by read().byte[]
getHash()
Piece must be complete.int
getLength()
int
getPiece()
piece numberRequest
getRequest()
Convert this PartialPiece to a request for the next chunk.boolean
hasChunk(int chunk)
Has this chunk been downloaded?boolean
hasData()
Have any chunks been downloaded?int
hashCode()
boolean
isComplete()
void
read(DataInputStream din, int offset, int len, BandwidthListener bwl)
Blocking.void
release()
Release all resources.String
toString()
static void
warn(String s)
void
write(DataOutput out, int offset, int len)
Piece must be complete.
-
-
-
Constructor Detail
-
PartialPiece
public PartialPiece(Piece piece, int len, File tempDir)
Used by PeerCoordinator. Creates a new PartialPiece, with no chunks yet downloaded. Allocates the data storage area, either on the heap or in the temp directory, depending on size.- Parameters:
piece
- Piece number requested.len
- must be equal to the piece length
-
-
Method Detail
-
getRequest
public Request getRequest()
Convert this PartialPiece to a request for the next chunk. Used by PeerState only.- Returns:
- null if complete
-
getPiece
public int getPiece()
piece number
-
getLength
public int getLength()
- Since:
- 0.9.1
-
isComplete
public boolean isComplete()
- Since:
- 0.9.62
-
hasData
public boolean hasData()
Have any chunks been downloaded?- Since:
- 0.9.63
-
hasChunk
public boolean hasChunk(int chunk)
Has this chunk been downloaded?- Since:
- 0.9.63
-
getDownloaded
public int getDownloaded()
How many bytes are good - as set by read(). As of 0.9.63, accurately counts good bytes after "holes".
-
getHash
public byte[] getHash() throws IOException
Piece must be complete. The SHA1 hash of the completely read data.- Throws:
IOException
- Since:
- 0.9.1
-
read
public void read(DataInputStream din, int offset, int len, BandwidthListener bwl) throws IOException
Blocking. If offset matches the previous downloaded amount (as set by a previous call to read() or setDownlaoded()), the downloaded amount will be incremented by len.- Throws:
IOException
- Since:
- 0.9.1
-
write
public void write(DataOutput out, int offset, int len) throws IOException
Piece must be complete. Caller must synchronize on out and seek to starting point. Caller must call release() when done with the whole piece.- Parameters:
out
- stream to write tooffset
- offset in the piecelen
- length to write- Throws:
IOException
- Since:
- 0.9.1
-
release
public void release()
Release all resources.- Since:
- 0.9.1
-
compareTo
public int compareTo(PartialPiece opp)
- Specified by:
compareTo
in interfaceComparable<PartialPiece>
-
equals
public boolean equals(Object o)
Make this simple so PeerCoordinator can keep a List. Warning - compares piece number only!
-
warn
public static void warn(String s)
- Since:
- 0.9.62
-
-