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 Details

    • 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 Details

    • getRequest

      public Request getRequest()
      Convert this PartialPiece to a request for the next chunk. Used by PeerState only. This depends on the downloaded value as set by setDownloaded() or read().
    • getPiece

      public int getPiece()
      piece number
    • getLength

      public int getLength()
      Since:
      0.9.1
    • getDownloaded

      public int getDownloaded()
      How many bytes are good - as set by setDownloaded() or read()
    • setDownloaded

      public void setDownloaded(int offset)
      Call this if necessary before returning a PartialPiece to the PeerCoordinator. We do not use a bitmap to track individual chunks received. Any chunks after a 'hole' will be lost.
      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 offset, int len) 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 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(PartialPiece opp)
      Specified by:
      compareTo in interface Comparable<PartialPiece>
    • 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