Class Storage

    • 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.
      • 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 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
        url_list - may be null
        comment - may be null
        Throws:
        IOException - when creating and/or checking files fails.
        Since:
        0.9.48
    • 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
      • 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

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

        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:
        IOException - on fail
      • 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 AutoCloseable
        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