java.lang.Object
org.klomp.snark.MagnetState
Simple state for the download of the metainfo, shared between Peer and ExtensionHandler.
Nothing is synchronized here! Caller must synchronize on this for everything!
Reference: BEP 9
Field Summary
Modifier and TypeFieldDescriptionstatic final intChunk size for magnet metadata fetches (16 KB).private booleanTrue once the metadata is fully fetched.private BitFieldWhich metadata pieces have arrived.private final byte[]The torrent's info hash.private booleanIf false, nothing below is valid.private MetaInfoOnly valid when finished.private byte[]Bitfield for the metainfo.private intTotal size of the metadata, in bytes.private BitFieldBitfield for the metainfo chunks; remains null if we start out complete.private intNumber of metadata pieces.Constructor Summary
ConstructorsConstructorDescriptionMagnetState(byte[] iHash, MetaInfo meta) Creates a new MagnetState for the given info hash.Method Summary
Modifier and TypeMethodDescriptionprivate MetaInfoBuilds a MetaInfo from the collected metadata bytes.intchunkSize(int chunk) The size of a specific chunk.intThe number of chunks remaining to be downloaded.byte[]getChunk(int chunk) The data for a specific chunk.Returns the MetaInfo for this magnet state.intThe next chunk number to request.intgetSize()Returns the total metadata size in bytes.voidinitialize(int size) Initializes the magnet state with the given metadata size.booleanCheck if the magnet download is complete.booleanCheck if the magnet state has been initialized.booleansaveChunk(int chunk, byte[] data, int off, int length) Save a chunk of data to the magnet state.voidsetMetaInfo(MetaInfo meta) Store the MetaInfo for a completed magnet download.
Field Details
CHUNK_SIZE
public static final int CHUNK_SIZEChunk size for magnet metadata fetches (16 KB).- See Also:
infohash
private final byte[] infohashThe torrent's info hash.complete
private boolean completeTrue once the metadata is fully fetched.isInitialized
private boolean isInitializedIf false, nothing below is valid.metaSize
private int metaSizeTotal size of the metadata, in bytes.totalChunks
private int totalChunksNumber of metadata pieces.requested
Bitfield for the metainfo chunks; remains null if we start out complete.have
Which metadata pieces have arrived.metainfoBytes
private byte[] metainfoBytesBitfield for the metainfo.metainfo
Only valid when finished.
Constructor Details
MagnetState
Creates a new MagnetState for the given info hash.- Parameters:
iHash- the 20-byte info hashmeta- the MetaInfo if already available, or null for a new magnet download
Method Details
initialize
public void initialize(int size) Initializes the magnet state with the given metadata size. Must only be called once.- Parameters:
size- the metadata size in bytes- Throws:
IllegalArgumentException- if already initialized
setMetaInfo
Store the MetaInfo for a completed magnet download.- Parameters:
meta- the completed MetaInfo
getMetaInfo
Returns the MetaInfo for this magnet state.- Returns:
- the MetaInfo built from the gathered pieces
- Throws:
IllegalArgumentException- if not complete
getSize
public int getSize()Returns the total metadata size in bytes.- Returns:
- the metadata size
- Throws:
IllegalArgumentException- if not initialized
isInitialized
public boolean isInitialized()Check if the magnet state has been initialized.- Returns:
- true if initialized, false otherwise
isComplete
public boolean isComplete()Check if the magnet download is complete.- Returns:
- true if complete, false otherwise
chunkSize
public int chunkSize(int chunk) The size of a specific chunk.- Parameters:
chunk- the chunk number- Returns:
- the size of the chunk in bytes
chunksRemaining
public int chunksRemaining()The number of chunks remaining to be downloaded.- Returns:
- the number of chunks remaining
- Throws:
IllegalArgumentException- if not initialized
getNextRequest
public int getNextRequest()The next chunk number to request. Uses a random selection algorithm to avoid requesting the same chunks repeatedly.- Returns:
- the next chunk number to request
- Throws:
IllegalArgumentException- if not initialized or complete
getChunk
public byte[] getChunk(int chunk) The data for a specific chunk.- Parameters:
chunk- the chunk number to retrieve- Returns:
- the chunk data as a byte array
- Throws:
IllegalArgumentException- if not complete or chunk number is invalid
saveChunk
Save a chunk of data to the magnet state.- Parameters:
chunk- the chunk number to savedata- the byte array containing the chunk dataoff- the offset in the data array where the chunk startslength- the byte count to copy, which must equal the chunk size in a complete file- Returns:
- true if this was the last piece, false otherwise
- Throws:
IllegalArgumentException- if not initialized, chunk number is invalid, or length is incorrectException- if there's an error building the MetaInfoStackOverflowError- if the received metadata nests lists or maps deeply enough to exhaust the decoder's recursionThrowable
buildMetaInfo
Builds a MetaInfo from the collected metadata bytes. Verifies the info hash matches.- Returns:
- the built MetaInfo
- Throws:
IOException- if the info hash doesn't match or data is corruptException- if there's an error parsing the metadataStackOverflowError- if the bencoded metadata is nested too deeply for the decoderThrowable