org.klomp.snark
Class Storage

java.lang.Object
  extended by org.klomp.snark.Storage

public class Storage
extends java.lang.Object

Maintains pieces on disk. Can be used to store and retrieve pieces.


Field Summary
(package private)  boolean changed
           
static int MAX_PIECE_SIZE
          note that we start reducing max number of peer connections above 1MB
static int MAX_PIECES
          The maximum number of pieces in a torrent.
static long MAX_TOTAL_SIZE
           
(package private)  int piece_size
           
(package private)  int pieces
           
 
Constructor Summary
Storage(I2PSnarkUtil util, java.io.File baseFile, java.lang.String announce, StorageListener listener)
          Creates a storage from the existing file or directory together with an appropriate MetaInfo file as can be announced on the given announce String location.
Storage(I2PSnarkUtil util, MetaInfo metainfo, StorageListener listener)
          Creates a new storage based on the supplied MetaInfo.
 
Method Summary
 void check(java.lang.String rootDir)
          Creates (and/or checks) all files from the metainfo file list.
 void check(java.lang.String rootDir, long savedTime, BitField savedBitField)
          use a saved bitfield and timestamp from a config file
 void cleanRAFs()
           
 void close()
          Closes the Storage and makes sure that all RandomAccessFiles are closed.
 boolean complete()
          Whether or not this storage contains all pieces if the MetaInfo.
 void create()
           
 java.lang.String getBaseName()
          The base file or directory name of the data, as specified in the .torrent file, but filtered to remove illegal characters.
 BitField getBitField()
          The BitField that tells which pieces this storage contains.
static java.io.File getFileFromNames(java.io.File base, java.util.List names)
           
 MetaInfo getMetaInfo()
          Returns the MetaInfo associated with this Storage.
 byte[] getPiece(int piece, int off, int len)
          Returns a byte array containing a portion of the requested piece or null if the storage doesn't contain the piece yet.
 int needed()
          How many pieces are still missing from this storage.
 boolean putPiece(int piece, byte[] ba)
          Put the piece in the Storage if it is correct.
 long remaining(java.lang.String file)
           
 void reopen(java.lang.String rootDir)
          Reopen the file descriptors for a restart Do existence check but no length check or data reverification
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

piece_size

int piece_size

pieces

int pieces

changed

boolean changed

MAX_PIECE_SIZE

public static final int MAX_PIECE_SIZE
note that we start reducing max number of peer connections above 1MB

See Also:
Constant Field Values

MAX_PIECES

public static final int MAX_PIECES
The maximum number of pieces in a torrent.

See Also:
Constant Field Values

MAX_TOTAL_SIZE

public static final long MAX_TOTAL_SIZE
See Also:
Constant Field Values
Constructor Detail

Storage

public Storage(I2PSnarkUtil util,
               MetaInfo metainfo,
               StorageListener listener)
        throws java.io.IOException
Creates a new storage based on the supplied MetaInfo. This will try to create and/or check all needed files in the MetaInfo.

Throws:
java.io.IOException - when creating and/or checking files fails.

Storage

public Storage(I2PSnarkUtil util,
               java.io.File baseFile,
               java.lang.String announce,
               StorageListener listener)
        throws java.io.IOException
Creates a storage from the existing file or directory together with an appropriate MetaInfo file as can be announced on the given announce String location.

Throws:
java.io.IOException
Method Detail

create

public void create()
            throws java.io.IOException
Throws:
java.io.IOException

getMetaInfo

public MetaInfo getMetaInfo()
Returns the MetaInfo associated with this Storage.


needed

public int needed()
How many pieces are still missing from this storage.


complete

public boolean complete()
Whether or not this storage contains all pieces if the MetaInfo.


remaining

public long remaining(java.lang.String file)
Parameters:
file - canonical path (non-directory)
Returns:
number of bytes remaining; -1 if unknown file
Since:
0.7.14

getBitField

public BitField getBitField()
The BitField that tells which pieces this storage contains. Do not change this since this is the current state of the storage.


getBaseName

public java.lang.String getBaseName()
The base file or directory name of the data, as specified in the .torrent file, but filtered to remove illegal characters. This is where the data actually is, relative to the snark base dir.

Since:
0.7.14

check

public void check(java.lang.String rootDir)
           throws java.io.IOException
Creates (and/or checks) all files from the metainfo file list.

Throws:
java.io.IOException

check

public void check(java.lang.String rootDir,
                  long savedTime,
                  BitField savedBitField)
           throws java.io.IOException
use a saved bitfield and timestamp from a config file

Throws:
java.io.IOException

reopen

public void reopen(java.lang.String rootDir)
            throws java.io.IOException
Reopen the file descriptors for a restart Do existence check but no length check or data reverification

Throws:
java.io.IOException

getFileFromNames

public static java.io.File getFileFromNames(java.io.File base,
                                            java.util.List names)

close

public void close()
           throws java.io.IOException
Closes the Storage and makes sure that all RandomAccessFiles are closed. The Storage is unusable after this.

Throws:
java.io.IOException

getPiece

public byte[] getPiece(int piece,
                       int off,
                       int len)
                throws java.io.IOException
Returns a byte array containing a portion of the requested piece or null if the storage doesn't contain the piece yet.

Throws:
java.io.IOException

putPiece

public boolean putPiece(int piece,
                        byte[] ba)
                 throws java.io.IOException
Put the piece in the Storage if it is correct.

Returns:
true if the piece was correct (sha metainfo hash matches), otherwise false.
Throws:
java.io.IOException - when some storage related error occurs.

cleanRAFs

public void cleanRAFs()