org.klomp.snark
Class PartialPiece

java.lang.Object
  extended by org.klomp.snark.PartialPiece
All Implemented Interfaces:
Comparable

 class PartialPiece
extends Object
implements Comparable

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
PartialPiece(Piece piece, int len, File tempDir)
          Used by PeerCoordinator.
 
Method Summary
 int compareTo(Object o)
           
 boolean equals(Object o)
          Make this simple so PeerCoordinator can keep a List.
 int getDownloaded()
          How many bytes are good - only valid by setDownloaded()
 byte[] getHash()
          Piece must be complete.
 int getLength()
           
 int getPiece()
          piece number
 Request getRequest()
          Convert this PartialPiece to a request for the next chunk.
 int hashCode()
           
 void read(DataInputStream din, int off, int len)
          Blocking.
 void release()
          Release all resources.
 void setDownloaded(int offset)
          Call this before returning a PartialPiece to the PeerCoordinator
 String toString()
           
 void write(DataOutput out, int offset, int len)
          Piece must be complete.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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.


getPiece

public int getPiece()
piece number


getLength

public int getLength()
Since:
0.9.1

getDownloaded

public int getDownloaded()
How many bytes are good - only valid by setDownloaded()


setDownloaded

public void setDownloaded(int offset)
Call this before returning a PartialPiece to the PeerCoordinator

Since:
0.9.1

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 off,
                 int len)
          throws IOException
Blocking.

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 to
offset - offset in the piece
len - length to write
Throws:
IOException
Since:
0.9.1

release

public void release()
Release all resources.

Since:
0.9.1

compareTo

public int compareTo(Object o)
              throws ClassCastException
Specified by:
compareTo in interface Comparable
Throws:
ClassCastException

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object o)
Make this simple so PeerCoordinator can keep a List. Warning - compares piece number only!

Overrides:
equals in class Object

toString

public String toString()
Overrides:
toString in class Object