Package org.klomp.snark
Class Storage
java.lang.Object
org.klomp.snark.Storage
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class Storage extends Object implements Closeable
Maintains pieces on disk. Can be used to store and retrieve pieces.
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_PIECE_SIZE
bigger than this will be rejectedstatic int
MAX_PIECES
The maximum number of pieces in a torrent.static long
MAX_TOTAL_SIZE
static int
PRIORITY_NORMAL
static int
PRIORITY_SKIP
-
Constructor Summary
Constructors Constructor Description Storage(I2PSnarkUtil util, File baseFile, String announce, List<List<String>> announce_list, String created_by, boolean privateTorrent, List<String> url_list, String comment, StorageListener listener)
Creates a storage from the existing file or directory.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.Storage(I2PSnarkUtil util, File baseFile, MetaInfo metainfo, StorageListener listener, boolean preserveFileNames)
Creates a new storage based on the supplied MetaInfo. -
Method Summary
Modifier and Type Method Description 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(package private) void
clearChanged()
Clear the storage changed variablevoid
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.long
getActivity()
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.boolean
getInOrder()
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)
long
getSkippedLength()
Call setPriority() for all changed files first, then call this.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 oncelong[][]
remaining2()
For efficiency, calculate remaining bytes for all files at once.void
reopen()
Doesn't really reopen the file descriptors for a restart.void
setActivity(long time)
(package private) void
setFilePriorities(int[] p)
Set the file priorities array.void
setInOrder(boolean yes)
Call AFTER setFilePriorites() so we know what's skippedvoid
setPriority(int fileIndex, int pri)
Must call Snark.updatePiecePriorities() (which calls getPiecePriorities()) after calling this.
-
Field Details
-
MAX_PIECE_SIZE
public static final int MAX_PIECE_SIZEbigger than this will be rejected- See Also:
- Constant Field Values
-
MAX_PIECES
public static final int MAX_PIECESThe 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
-
PRIORITY_SKIP
public static final int PRIORITY_SKIP- See Also:
- Constant Field Values
-
PRIORITY_NORMAL
public static final int PRIORITY_NORMAL- See Also:
- Constant Field Values
-
-
Constructor Details
-
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 dirpreserveFileNames
- 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 IOExceptionCreates 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 nulllistener
- may be nullcreated_by
- may be null- Throws:
IOException
- when creating and/or checking files fails.
-
Storage
public Storage(I2PSnarkUtil util, File baseFile, String announce, List<List<String>> announce_list, String created_by, boolean privateTorrent, List<String> url_list, String comment, StorageListener listener) throws IOExceptionCreates 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 nulllistener
- may be nullcreated_by
- may be nullurl_list
- may be nullcomment
- may be null- Throws:
IOException
- when creating and/or checking files fails.- Since:
- 0.9.48
-
-
Method Details
-
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
-
clearChanged
void clearChanged()Clear the storage changed variable- Since:
- 0.9.30
-
getActivity
public long getActivity()- Since:
- 0.9.42
-
setActivity
public void setActivity(long time)- Since:
- 0.9.42
-
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
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
-
remaining2
public long[][] remaining2()For efficiency, calculate remaining bytes for all files at once. Remaining bytes is rv[0]. Preview bytes is rv[1].- Returns:
- number of bytes remaining and number of bytes available for a preview for each file, use indexOf() to get index for a file
- Since:
- 0.9.45
-
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 indexOfpri
- 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
-
getInOrder
public boolean getInOrder()- Returns:
- as last set, default false
- Since:
- 0.9.36
-
setInOrder
public void setInOrder(boolean yes)Call AFTER setFilePriorites() so we know what's skipped- Parameters:
yes
- enable or not- Since:
- 0.9.36
-
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 and inOrder not set.
- Since:
- 0.8.1
-
getSkippedLength
public long getSkippedLength()Call setPriority() for all changed files first, then call this. The length of all the pieces that are not yet downloaded, and are set to skipped. This is not the same as the total of all skipped files, since pieces may span multiple files.- Returns:
- 0 on error, if complete, or if only one file
- Since:
- 0.9.24
-
getBitField
The BitField that tells which pieces this storage contains. Do not change this since this is the current state of the storage. -
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
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 onceIOException
-
check
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 onceIOException
-
reopen
Doesn't really reopen the file descriptors for a restart. Just does an existence check but no length check or data reverification- Throws:
IOException
- on fail
-
filterName
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
The base file or directory.- Returns:
- the File
- Since:
- 0.9.15
-
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
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
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
Closes the Storage and makes sure that all RandomAccessFiles are closed. The Storage is unusable after this.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getPiece
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
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
Create a metainfo. Used in the installer build process; do not comment out.- Since:
- 0.9.4
-