org.klomp.snark

## Class MagnetState

- java.lang.Object
- - org.klomp.snark.MagnetState

-     class MagnetState
      extends Object

  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

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `CHUNK_SIZE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MagnetState(byte[] iHash, MetaInfo meta)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `chunkSize(int chunk)` Get the size of a specific chunk.
    `int` | `chunksRemaining()` Get the number of chunks remaining to be downloaded.
    `byte[]` | `getChunk(int chunk)` Get the data for a specific chunk.
    `MetaInfo` | `getMetaInfo()`
    `int` | `getNextRequest()` Get the next chunk number to request.
    `int` | `getSize()`
    `void` | `initialize(int size)` Call this for a new magnet when you have the size
    `boolean` | `isComplete()` Check if the magnet download is complete.
    `boolean` | `isInitialized()` Check if the magnet state has been initialized.
    `boolean` | `saveChunk(int chunk, byte[] data, int off, int length)` Save a chunk of data to the magnet state.
    `void` | `setMetaInfo(MetaInfo meta)` Call this for a new magnet when the download is complete.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### CHUNK_SIZE

          public static final int CHUNK_SIZE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### MagnetState

          public MagnetState(byte[] iHash,
                             MetaInfo meta)

      Parameters:
        `meta` - null for new magnet

  - ### Method Detail

    - #### chunkSize

          public int chunkSize(int chunk)

      Get the size of a specific chunk.
      Parameters:
        `chunk` - the chunk number
      Returns:
        the size of the chunk in bytes

    - #### chunksRemaining

          public int chunksRemaining()

      Get the number of chunks remaining to be downloaded.
      Returns:
        the number of chunks remaining
      Throws:
        `IllegalArgumentException` - if not initialized

    - #### getChunk

          public byte[] getChunk(int chunk)

      Get 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

    - #### getMetaInfo

          public MetaInfo getMetaInfo()

      Throws:
        `IllegalArgumentException`

    - #### getNextRequest

          public int getNextRequest()

      Get 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

    - #### getSize

          public int getSize()

      Throws:
        `IllegalArgumentException`

    - #### initialize

          public void initialize(int size)

      Call this for a new magnet when you have the size
      Throws:
        `IllegalArgumentException`

    - #### isComplete

          public boolean isComplete()

      Check if the magnet download is complete.
      Returns:
        true if complete, false otherwise

    - #### isInitialized

          public boolean isInitialized()

      Check if the magnet state has been initialized.
      Returns:
        true if initialized, false otherwise

    - #### saveChunk

          public boolean saveChunk(int chunk,
                                   byte[] data,
                                   int off,
                                   int length)
                            throws Exception

      Save a chunk of data to the magnet state.
      Parameters:
        `chunk` - the chunk number to save
        `data` - the byte array containing the chunk data
        `off` - the offset in the data array where the chunk starts
        `length` - the length of the chunk data
      Returns:
        true if this was the last piece, false otherwise
      Throws:
        `IllegalArgumentException` - if not initialized, chunk number is invalid, or length is incorrect
        `Exception` - if there's an error building the MetaInfo

    - #### setMetaInfo

          public void setMetaInfo(MetaInfo meta)

      Call this for a new magnet when the download is complete.
      Throws:
        `IllegalArgumentException`