org.klomp.snark
Class Storage

java.lang.Object
  extended by org.klomp.snark.Storage
All Implemented Interfaces:
Closeable

public class Storage
extends Object
implements Closeable

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


Field Summary
static int MAX_PIECE_SIZE
          bigger than this will be rejected
static int MAX_PIECES
          The maximum number of pieces in a torrent.
static long MAX_TOTAL_SIZE
           
 
Constructor Summary
Storage(I2PSnarkUtil util, File baseFile, MetaInfo metainfo, StorageListener listener, boolean preserveFileNames)
          Creates a new storage based on the supplied MetaInfo.
Storage(I2PSnarkUtil util, File baseFile, String announce, List<List<String>> announce_list, String created_by, boolean privateTorrent, StorageListener listener)
          Creates a storage from the existing file or directory.
 
Method Summary
 void check()
          Creates (and/or checks) all files from the metainfo file list.
 void check(long savedTime, BitField savedBitField)
          Creates (and/or checks) all files from the metainfo file list.
 void cleanRAFs()
          Close unused RAFs - call periodically
 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.
static String filterName(String name)
          Removes 'suspicious' characters from the given file name.
 File getBase()
          The base file or directory.
 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.
 double getCheckingProgress()
          If checking is in progress, return completion 0.0 ...
 SortedSet<File> getDirectories()
          Includes the base for a multi-file torrent.
 int getFileCount()
          Does not include directories.
 int[] getFilePriorities()
          Get the file priorities array.
 List<File> getFiles()
          Does not include directories.
 MetaInfo getMetaInfo()
          Returns the MetaInfo associated with this Storage.
 ByteArray 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[] getPiecePriorities()
          Call setPriority() for all changed files first, then call this.
 boolean getPreserveFileNames()
           
 int getPriority(int fileIndex)
           
 int indexOf(File file)
          Get index to pass to remaining(), getPriority(), setPriority()
 boolean isAllocating()
          Disk allocation (ballooning) in progress.
 boolean isChanged()
          Has the storage changed since instantiation?
 boolean isChecking()
          File checking in progress.
static void main(String[] args)
          Create a metainfo.
 int needed()
          How many pieces are still missing from this storage.
 boolean putPiece(PartialPiece pp)
          Put the piece in the Storage if it is correct.
 boolean recheck()
          Blocking.
 long[] remaining()
          For efficiency, calculate remaining bytes for all files at once
 void reopen()
          Doesn't really reopen the file descriptors for a restart.
(package private)  void setFilePriorities(int[] p)
          Set the file priorities array.
 void setPriority(int fileIndex, int pri)
          Must call Snark.updatePiecePriorities() (which calls getPiecePriorities()) after calling this.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_PIECE_SIZE

public static final int MAX_PIECE_SIZE
bigger than this will be rejected

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,
               File baseFile,
               MetaInfo metainfo,
               StorageListener listener,
               boolean preserveFileNames)
Creates a new storage based on the supplied MetaInfo. Does not check storage. Caller MUST call check(), which will try to create and/or check all needed files in the MetaInfo.

Parameters:
baseFile - the torrent data file or dir
preserveFileNames - if true, do not remap names to a 'safe' charset

Storage

public Storage(I2PSnarkUtil util,
               File baseFile,
               String announce,
               List<List<String>> announce_list,
               String created_by,
               boolean privateTorrent,
               StorageListener listener)
        throws IOException
Creates a storage from the existing file or directory. Creates an in-memory metainfo but does not save it to a file, caller must do that. Creates the metainfo, this may take a LONG time. BLOCKING.

Parameters:
announce - may be null
listener - may be null
created_by - may be null
Throws:
IOException - when creating and/or checking files fails.
Method Detail

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.


isChanged

public boolean isChanged()
Has the storage changed since instantiation?

Since:
0.8.5

isChecking

public boolean isChecking()
File checking in progress.

Since:
0.9.3

getCheckingProgress

public double getCheckingProgress()
If checking is in progress, return completion 0.0 ... 1.0, else return 1.0.

Since:
0.9.23

isAllocating

public boolean isAllocating()
Disk allocation (ballooning) in progress. Always false on Windows.

Since:
0.9.3

indexOf

public int indexOf(File file)
Get index to pass to remaining(), getPriority(), setPriority()

Parameters:
file - non-canonical path (non-directory)
Returns:
internal index of file; -1 if unknown file
Since:
0.9.15

remaining

public long[] remaining()
For efficiency, calculate remaining bytes for all files at once

Returns:
number of bytes remaining for each file, use indexOf() to get index for a file
Since:
0.9.23

getPriority

public int getPriority(int fileIndex)
Parameters:
fileIndex - as obtained from indexOf
Since:
0.8.1

setPriority

public void setPriority(int fileIndex,
                        int pri)
Must call Snark.updatePiecePriorities() (which calls getPiecePriorities()) after calling this.

Parameters:
fileIndex - as obtained from indexOf
pri - default 0; <0 to disable
Since:
0.8.1

getFilePriorities

public int[] getFilePriorities()
Get the file priorities array.

Returns:
null on error, if complete, or if only one file
Since:
0.8.1

setFilePriorities

void setFilePriorities(int[] p)
Set the file priorities array. Only call this when stopped, but after check()

Parameters:
p - may be null
Since:
0.8.1

getPiecePriorities

public int[] getPiecePriorities()
Call setPriority() for all changed files first, then call this. Set the piece priority to the highest priority of all files spanning the piece. Caller must pass array to the PeerCoordinator.

Returns:
null on error, if complete, or if only one file
Since:
0.8.1

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

getPreserveFileNames

public boolean getPreserveFileNames()
Since:
0.9.15

check

public void check()
           throws IOException
Creates (and/or checks) all files from the metainfo file list. Only call this once, and only after the constructor with the metainfo. Use recheck() to check again later.

Throws:
IllegalStateException - if called more than once
IOException

check

public void check(long savedTime,
                  BitField savedBitField)
           throws IOException
Creates (and/or checks) all files from the metainfo file list. Use a saved bitfield and timestamp from a config file. Only call this once, and only after the constructor with the metainfo. Use recheck() to check again later.

Throws:
IllegalStateException - if called more than once
IOException

reopen

public void reopen()
            throws IOException
Doesn't really reopen the file descriptors for a restart. Just does an existence check but no length check or data reverification

Throws:
IOE - on fail
IOException

filterName

public static String filterName(String name)
Removes 'suspicious' characters from the given file name. http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx Then replace chars not supported in the charset. This is called frequently and it can be pretty slow so cache the result. TODO: If multiple files in the same torrent map to the same filter name, the whole torrent will blow up. Check at torrent creation?


getBase

public File getBase()
The base file or directory.

Returns:
the File
Since:
0.9.15

getFiles

public List<File> getFiles()
Does not include directories. Unsorted.

Returns:
a new List
Since:
0.9.15

getFileCount

public int getFileCount()
Does not include directories.

Since:
0.9.23

getDirectories

public SortedSet<File> getDirectories()
Includes the base for a multi-file torrent. Sorted bottom-up for easy deletion. Slow. Use for deletion only.

Returns:
a new Set or null for a single-file torrent
Since:
0.9.15

recheck

public boolean recheck()
                throws IOException
Blocking. Holds lock. Recommend running only when stopped. Caller should thread. Calls listener.setWantedPieces() on completion if anything changed.

Returns:
true if anything changed, false otherwise
Throws:
IOException
Since:
0.9.23

close

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

Specified by:
close in interface Closeable
Throws:
IOException

getPiece

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

Throws:
IOException

putPiece

public boolean putPiece(PartialPiece pp)
                 throws IOException
Put the piece in the Storage if it is correct. Warning - takes a LONG time if complete as it does the recheck here. TODO thread the recheck?

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

cleanRAFs

public void cleanRAFs()
Close unused RAFs - call periodically


main

public static void main(String[] args)
Create a metainfo. Used in the installer build process; do not comment out.

Since:
0.9.4