Search in sources :

Example 21 with TOTorrentFile

use of com.biglybt.core.torrent.TOTorrentFile in project BiglyBT by BiglySoftware.

the class CacheFileManagerImpl method closeFile.

protected void closeFile(CacheFileWithCache file) {
    TOTorrentFile tf = file.getTorrentFile();
    if (tf != null && torrent_to_cache_file_map.get(tf) != null) {
        try {
            this_mon.enter();
            torrent_to_cache_file_map.remove(tf);
        } finally {
            this_mon.exit();
        }
    }
}
Also used : TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile)

Example 22 with TOTorrentFile

use of com.biglybt.core.torrent.TOTorrentFile in project BiglyBT by BiglySoftware.

the class HTTPNetworkConnectionFile method decodeHeader.

@Override
protected void decodeHeader(HTTPMessageDecoder decoder, final String header) throws IOException {
    if (switching) {
        Debug.out("new header received while paused");
        throw (new IOException("Bork"));
    }
    if (!isSeed()) {
        return;
    }
    PEPeerControl control = getPeerControl();
    DiskManager dm = control.getDiskManager();
    if (dm == null) {
        Debug.out("Disk manager is null");
        throw (new IOException("Disk manager unavailable"));
    }
    TOTorrent to_torrent = dm.getTorrent();
    char[] chars = header.toCharArray();
    int last_pos = 0;
    int line_num = 0;
    String target_str = null;
    DiskManagerFileInfo target_file = null;
    long file_offset = 0;
    List<long[]> ranges = new ArrayList<>();
    boolean keep_alive = false;
    for (int i = 1; i < chars.length; i++) {
        if (chars[i - 1] == '\r' && chars[i] == '\n') {
            String line = new String(chars, last_pos, i - last_pos).trim();
            last_pos = i;
            line_num++;
            if (line_num == 1) {
                line = line.substring(line.indexOf("files/") + 6);
                int hash_end = line.indexOf("/");
                final byte[] old_hash = control.getHash();
                final byte[] new_hash = URLDecoder.decode(line.substring(0, hash_end), "ISO-8859-1").getBytes("ISO-8859-1");
                if (!Arrays.equals(new_hash, old_hash)) {
                    switching = true;
                    decoder.pauseInternally();
                    flushRequests(new flushListener() {

                        private boolean triggered;

                        @Override
                        public void flushed() {
                            synchronized (this) {
                                if (triggered) {
                                    return;
                                }
                                triggered = true;
                            }
                            getManager().reRoute(HTTPNetworkConnectionFile.this, old_hash, new_hash, header);
                        }
                    });
                    return;
                }
                line = line.substring(hash_end + 1);
                line = line.substring(0, line.lastIndexOf(' '));
                String file = line;
                if (to_torrent.isSimpleTorrent()) {
                    // optimise for simple torrents. also support the case where
                    // client has the hash but doesn't know the file name
                    target_file = dm.getFiles()[0];
                } else {
                    target_str = file;
                    StringTokenizer tok = new StringTokenizer(file, "/");
                    List<byte[]> bits = new ArrayList<>();
                    while (tok.hasMoreTokens()) {
                        bits.add(URLDecoder.decode(tok.nextToken(), "ISO-8859-1").getBytes("ISO-8859-1"));
                    }
                    if (!to_torrent.isSimpleTorrent() && bits.size() > 1) {
                        if (Arrays.equals(to_torrent.getName(), (byte[]) bits.get(0))) {
                            bits.remove(0);
                        }
                    }
                    DiskManagerFileInfo[] files = dm.getFiles();
                    file_offset = 0;
                    for (int j = 0; j < files.length; j++) {
                        TOTorrentFile torrent_file = files[j].getTorrentFile();
                        byte[][] comps = torrent_file.getPathComponents();
                        if (comps.length == bits.size()) {
                            boolean match = true;
                            for (int k = 0; k < comps.length; k++) {
                                if (!Arrays.equals(comps[k], (byte[]) bits.get(k))) {
                                    match = false;
                                    break;
                                }
                            }
                            if (match) {
                                target_file = files[j];
                                break;
                            }
                        }
                        file_offset += torrent_file.getLength();
                    }
                }
            } else {
                line = line.toLowerCase(MessageText.LOCALE_ENGLISH);
                if (line.startsWith("range") && target_file != null) {
                    line = line.substring(5).trim();
                    if (line.startsWith(":")) {
                        String range_str = line.substring(1).trim();
                        if (range_str.startsWith("bytes=")) {
                            long file_length = target_file.getLength();
                            StringTokenizer tok2 = new StringTokenizer(range_str.substring(6), ",");
                            while (tok2.hasMoreTokens()) {
                                String range = tok2.nextToken();
                                try {
                                    int pos = range.indexOf('-');
                                    if (pos != -1) {
                                        String lhs = range.substring(0, pos);
                                        String rhs = range.substring(pos + 1);
                                        long start;
                                        long end;
                                        if (lhs.length() == 0) {
                                            // -222 is last 222 bytes of file
                                            end = file_length - 1;
                                            start = file_length - Long.parseLong(rhs);
                                        } else if (rhs.length() == 0) {
                                            end = file_length - 1;
                                            start = Long.parseLong(lhs);
                                        } else {
                                            start = Long.parseLong(lhs);
                                            end = Long.parseLong(rhs);
                                        }
                                        ranges.add(new long[] { start, end });
                                    }
                                } catch (Throwable e) {
                                }
                            }
                        }
                        if (ranges.size() == 0) {
                            log("Invalid range specification: '" + line + "'");
                            sendAndClose(getManager().getRangeNotSatisfiable());
                            return;
                        }
                    }
                } else if (line.contains("keep-alive")) {
                    keep_alive = true;
                }
            }
        }
    }
    if (target_file == null) {
        log("Failed to find file '" + target_str + "'");
        sendAndClose(getManager().getNotFound());
        return;
    }
    try {
        String name = target_file.getFile(true).getName();
        int pos = name.lastIndexOf(".");
        if (pos != -1) {
            setContentType(HTTPUtils.guessContentTypeFromFileType(name.substring(pos + 1)));
        }
    } catch (Throwable e) {
    }
    long file_length = target_file.getLength();
    boolean partial_content = ranges.size() > 0;
    if (!partial_content) {
        ranges.add(new long[] { 0, file_length - 1 });
    }
    long[] offsets = new long[ranges.size()];
    long[] lengths = new long[ranges.size()];
    for (int i = 0; i < ranges.size(); i++) {
        long[] range = (long[]) ranges.get(i);
        long start = range[0];
        long end = range[1];
        if (start < 0 || start >= file_length || end < 0 || end >= file_length || start > end) {
            log("Invalid range specification: '" + start + "-" + end + "'");
            sendAndClose(getManager().getRangeNotSatisfiable());
            return;
        }
        offsets[i] = file_offset + start;
        lengths[i] = (end - start) + 1;
    }
    addRequest(new httpRequest(offsets, lengths, file_length, partial_content, keep_alive));
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DiskManager(com.biglybt.core.disk.DiskManager) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) StringTokenizer(java.util.StringTokenizer) TOTorrent(com.biglybt.core.torrent.TOTorrent) PEPeerControl(com.biglybt.core.peer.impl.PEPeerControl)

Example 23 with TOTorrentFile

use of com.biglybt.core.torrent.TOTorrentFile in project BiglyBT by BiglySoftware.

the class DiskManagerUtil method getFileInfoSkeleton.

public static DiskManagerFileInfoSet getFileInfoSkeleton(final DownloadManager download_manager, final DiskManagerListener listener) {
    final TOTorrent torrent = download_manager.getTorrent();
    if (torrent == null) {
        return (new DiskManagerFileInfoSetImpl(new DiskManagerFileInfoImpl[0], null));
    }
    String tempRootDir = download_manager.getAbsoluteSaveLocation().getParent();
    if (// in case we alraedy are at the root
    tempRootDir == null)
        tempRootDir = download_manager.getAbsoluteSaveLocation().getPath();
    if (!torrent.isSimpleTorrent()) {
        tempRootDir += File.separator + download_manager.getAbsoluteSaveLocation().getName();
    }
    tempRootDir += File.separator;
    // prevent attempted state saves and associated nastyness during population of
    // the file skeleton entries
    final boolean[] loading = { true };
    try {
        final String root_dir = StringInterner.intern(tempRootDir);
        try {
            final LocaleUtilDecoder locale_decoder = LocaleTorrentUtil.getTorrentEncoding(torrent);
            TOTorrentFile[] torrent_files = torrent.getFiles();
            final FileSkeleton[] res = new FileSkeleton[torrent_files.length];
            final String incomplete_suffix = download_manager.getDownloadState().getAttribute(DownloadManagerState.AT_INCOMP_FILE_SUFFIX);
            final DiskManagerFileInfoSet fileSetSkeleton = new DiskManagerFileInfoSet() {

                @Override
                public DiskManagerFileInfo[] getFiles() {
                    return res;
                }

                @Override
                public int nbFiles() {
                    return res.length;
                }

                @Override
                public void setPriority(int[] toChange) {
                    if (toChange.length != res.length)
                        throw new IllegalArgumentException("array length mismatches the number of files");
                    for (int i = 0; i < res.length; i++) res[i].priority = toChange[i];
                    if (!loading[0]) {
                        DiskManagerImpl.storeFilePriorities(download_manager, res);
                    }
                    for (int i = 0; i < res.length; i++) if (toChange[i] != 0)
                        listener.filePriorityChanged(res[i]);
                }

                @Override
                public void setSkipped(boolean[] toChange, boolean setSkipped) {
                    if (toChange.length != res.length)
                        throw new IllegalArgumentException("array length mismatches the number of files");
                    if (!setSkipped) {
                        String[] types = DiskManagerImpl.getStorageTypes(download_manager);
                        boolean[] toLinear = new boolean[toChange.length];
                        boolean[] toReorder = new boolean[toChange.length];
                        int num_linear = 0;
                        int num_reorder = 0;
                        for (int i = 0; i < toChange.length; i++) {
                            if (toChange[i]) {
                                int old_type = DiskManagerUtil.convertDMStorageTypeFromString(types[i]);
                                if (old_type == DiskManagerFileInfo.ST_COMPACT) {
                                    toLinear[i] = true;
                                    num_linear++;
                                } else if (old_type == DiskManagerFileInfo.ST_REORDER_COMPACT) {
                                    toReorder[i] = true;
                                    num_reorder++;
                                }
                            }
                        }
                        if (num_linear > 0) {
                            if (!Arrays.equals(toLinear, setStorageTypes(toLinear, DiskManagerFileInfo.ST_LINEAR))) {
                                return;
                            }
                        }
                        if (num_reorder > 0) {
                            if (!Arrays.equals(toReorder, setStorageTypes(toReorder, DiskManagerFileInfo.ST_REORDER))) {
                                return;
                            }
                        }
                    }
                    File[] to_link = new File[res.length];
                    for (int i = 0; i < res.length; i++) {
                        if (toChange[i]) {
                            to_link[i] = res[i].setSkippedInternal(setSkipped);
                        }
                    }
                    if (!loading[0]) {
                        DiskManagerImpl.storeFilePriorities(download_manager, res);
                    }
                    List<Integer> from_indexes = new ArrayList<>();
                    List<File> from_links = new ArrayList<>();
                    List<File> to_links = new ArrayList<>();
                    for (int i = 0; i < res.length; i++) {
                        if (to_link[i] != null) {
                            from_indexes.add(i);
                            from_links.add(res[i].getFile(false));
                            to_links.add(to_link[i]);
                        }
                    }
                    if (from_links.size() > 0) {
                        download_manager.getDownloadState().setFileLinks(from_indexes, from_links, to_links);
                    }
                    if (!setSkipped) {
                        doFileExistenceChecks(this, toChange, download_manager, true);
                    }
                    for (int i = 0; i < res.length; i++) {
                        if (toChange[i]) {
                            listener.filePriorityChanged(res[i]);
                        }
                    }
                }

                @Override
                public boolean[] setStorageTypes(boolean[] toChange, int newStorageType) {
                    if (toChange.length != res.length)
                        throw new IllegalArgumentException("array length mismatches the number of files");
                    String[] types = DiskManagerImpl.getStorageTypes(download_manager);
                    boolean[] modified = new boolean[res.length];
                    boolean[] toSkip = new boolean[res.length];
                    int toSkipCount = 0;
                    DownloadManagerState dmState = download_manager.getDownloadState();
                    try {
                        dmState.suppressStateSave(true);
                        for (int i = 0; i < res.length; i++) {
                            if (!toChange[i])
                                continue;
                            final int idx = i;
                            int old_type = DiskManagerUtil.convertDMStorageTypeFromString(types[i]);
                            if (newStorageType == old_type) {
                                modified[i] = true;
                                continue;
                            }
                            try {
                                File target_file = res[i].getFile(true);
                                if (target_file.exists()) {
                                    CacheFile cache_file = CacheFileManagerFactory.getSingleton().createFile(new CacheFileOwner() {

                                        @Override
                                        public String getCacheFileOwnerName() {
                                            return (download_manager.getInternalName());
                                        }

                                        @Override
                                        public TOTorrentFile getCacheFileTorrentFile() {
                                            return (res[idx].getTorrentFile());
                                        }

                                        @Override
                                        public File getCacheFileControlFileDir() {
                                            return (download_manager.getDownloadState().getStateFile());
                                        }

                                        @Override
                                        public int getCacheMode() {
                                            return (CacheFileOwner.CACHE_MODE_NORMAL);
                                        }
                                    }, target_file, DiskManagerUtil.convertDMStorageTypeToCache(newStorageType));
                                    // need this to trigger recovery for re-order files :(
                                    cache_file.getLength();
                                    cache_file.close();
                                }
                                toSkip[i] = (newStorageType == FileSkeleton.ST_COMPACT || newStorageType == FileSkeleton.ST_REORDER_COMPACT) && !res[i].isSkipped();
                                if (toSkip[i]) {
                                    toSkipCount++;
                                }
                                modified[i] = true;
                            } catch (Throwable e) {
                                Debug.printStackTrace(e);
                                Logger.log(new LogAlert(download_manager, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to change storage type for '" + res[i].getFile(true) + "': " + Debug.getNestedExceptionMessage(e)));
                                // download's not running - tag for recheck
                                RDResumeHandler.recheckFile(download_manager, res[i]);
                            }
                            types[i] = DiskManagerUtil.convertDMStorageTypeToString(newStorageType);
                        }
                        /*
							 * set storage type and skipped before we do piece clearing and file
							 * clearing checks as those checks work better when skipped/stype is set
							 * properly
							 */
                        dmState.setListAttribute(DownloadManagerState.AT_FILE_STORE_TYPES, types);
                        if (toSkipCount > 0) {
                            setSkipped(toSkip, true);
                        }
                        for (int i = 0; i < res.length; i++) {
                            if (!toChange[i])
                                continue;
                            // download's not running, update resume data as necessary
                            int cleared = RDResumeHandler.storageTypeChanged(download_manager, res[i]);
                            if (cleared > 0) {
                                res[i].downloaded = res[i].downloaded - cleared * res[i].getTorrentFile().getTorrent().getPieceLength();
                                if (res[i].downloaded < 0)
                                    res[i].downloaded = 0;
                            }
                        }
                        DiskManagerImpl.storeFileDownloaded(download_manager, res, true);
                        doFileExistenceChecks(this, toChange, download_manager, newStorageType == FileSkeleton.ST_LINEAR || newStorageType == FileSkeleton.ST_REORDER);
                    } finally {
                        dmState.suppressStateSave(false);
                        dmState.save();
                    }
                    return modified;
                }
            };
            for (int i = 0; i < res.length; i++) {
                final TOTorrentFile torrent_file = torrent_files[i];
                final int file_index = i;
                FileSkeleton info = new FileSkeleton() {

                    private volatile CacheFile read_cache_file;

                    // do not access this field directly, use lazyGetFile() instead
                    private WeakReference dataFile = new WeakReference(null);

                    @Override
                    public void setPriority(int b) {
                        priority = b;
                        DiskManagerImpl.storeFilePriorities(download_manager, res);
                        listener.filePriorityChanged(this);
                    }

                    @Override
                    public void setSkipped(boolean _skipped) {
                        if (!_skipped && getStorageType() == ST_COMPACT) {
                            if (!setStorageType(ST_LINEAR)) {
                                return;
                            }
                        }
                        if (!_skipped && getStorageType() == ST_REORDER_COMPACT) {
                            if (!setStorageType(ST_REORDER)) {
                                return;
                            }
                        }
                        File to_link = setSkippedInternal(_skipped);
                        DiskManagerImpl.storeFilePriorities(download_manager, res);
                        if (to_link != null) {
                            download_manager.getDownloadState().setFileLink(file_index, getFile(false), to_link);
                        }
                        if (!_skipped) {
                            boolean[] toCheck = new boolean[fileSetSkeleton.nbFiles()];
                            toCheck[file_index] = true;
                            doFileExistenceChecks(fileSetSkeleton, toCheck, download_manager, true);
                        }
                        listener.filePriorityChanged(this);
                    }

                    @Override
                    public int getAccessMode() {
                        return (READ);
                    }

                    @Override
                    public long getDownloaded() {
                        return (downloaded);
                    }

                    @Override
                    public long getLastModified() {
                        return (getFile(true).lastModified());
                    }

                    @Override
                    public void setDownloaded(long l) {
                        downloaded = l;
                    }

                    @Override
                    public String getExtension() {
                        String ext = lazyGetFile().getName();
                        if (incomplete_suffix != null && ext.endsWith(incomplete_suffix)) {
                            ext = ext.substring(0, ext.length() - incomplete_suffix.length());
                        }
                        int separator = ext.lastIndexOf(".");
                        if (separator == -1)
                            separator = 0;
                        return ext.substring(separator);
                    }

                    @Override
                    public int getFirstPieceNumber() {
                        return (torrent_file.getFirstPieceNumber());
                    }

                    @Override
                    public int getLastPieceNumber() {
                        return (torrent_file.getLastPieceNumber());
                    }

                    @Override
                    public long getLength() {
                        return (torrent_file.getLength());
                    }

                    @Override
                    public int getIndex() {
                        return (file_index);
                    }

                    @Override
                    public int getNbPieces() {
                        return (torrent_file.getNumberOfPieces());
                    }

                    @Override
                    public int getPriority() {
                        return (priority);
                    }

                    @Override
                    protected File setSkippedInternal(boolean _skipped) {
                        // returns the file to link to if linkage is required
                        skipped_internal = _skipped;
                        if (!download_manager.isDestroyed()) {
                            DownloadManagerState dm_state = download_manager.getDownloadState();
                            String dnd_sf = dm_state.getAttribute(DownloadManagerState.AT_DND_SUBFOLDER);
                            if (dnd_sf != null) {
                                File link = getLink();
                                File file = getFile(false);
                                if (_skipped) {
                                    if (link == null || link.equals(file)) {
                                        File parent = file.getParentFile();
                                        if (parent != null) {
                                            String prefix = dm_state.getAttribute(DownloadManagerState.AT_DND_PREFIX);
                                            String file_name = file.getName();
                                            if (prefix != null && !file_name.startsWith(prefix)) {
                                                file_name = prefix + file_name;
                                            }
                                            File new_parent = new File(parent, dnd_sf);
                                            File new_file = new File(new_parent, file_name);
                                            if (!new_file.exists()) {
                                                if (!new_parent.exists()) {
                                                    new_parent.mkdirs();
                                                }
                                                if (new_parent.canWrite()) {
                                                    boolean ok;
                                                    if (file.exists()) {
                                                        ok = FileUtil.renameFile(file, new_file);
                                                    } else {
                                                        ok = true;
                                                    }
                                                    if (ok) {
                                                        return (new_file);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    if (link != null && !file.exists()) {
                                        File parent = file.getParentFile();
                                        if (parent != null && parent.canWrite()) {
                                            File new_parent = parent.getName().equals(dnd_sf) ? parent : new File(parent, dnd_sf);
                                            // use link name to handle incomplete file suffix if set
                                            File new_file = new File(new_parent, link.getName());
                                            if (new_file.equals(link)) {
                                                boolean ok;
                                                String incomp_ext = dm_state.getAttribute(DownloadManagerState.AT_INCOMP_FILE_SUFFIX);
                                                String file_name = file.getName();
                                                String prefix = dm_state.getAttribute(DownloadManagerState.AT_DND_PREFIX);
                                                boolean prefix_removed = false;
                                                if (prefix != null && file_name.startsWith(prefix)) {
                                                    file_name = file_name.substring(prefix.length());
                                                    prefix_removed = true;
                                                }
                                                if (incomp_ext != null && incomp_ext.length() > 0 && getDownloaded() != getLength()) {
                                                    if (prefix == null) {
                                                        prefix = "";
                                                    }
                                                    file = new File(file.getParentFile(), prefix + file_name + incomp_ext);
                                                } else if (prefix_removed) {
                                                    file = new File(file.getParentFile(), file_name);
                                                }
                                                if (new_file.exists()) {
                                                    ok = FileUtil.renameFile(new_file, file);
                                                } else {
                                                    ok = true;
                                                }
                                                if (ok) {
                                                    File[] files = new_parent.listFiles();
                                                    if (files != null && files.length == 0) {
                                                        new_parent.delete();
                                                    }
                                                    return (file);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        return (null);
                    }

                    @Override
                    public boolean isSkipped() {
                        return (skipped_internal);
                    }

                    @Override
                    public DiskManager getDiskManager() {
                        return (null);
                    }

                    @Override
                    public DownloadManager getDownloadManager() {
                        return (download_manager);
                    }

                    @Override
                    public File getFile(boolean follow_link) {
                        if (follow_link) {
                            File link = getLink();
                            if (link != null) {
                                return (link);
                            }
                        }
                        return lazyGetFile();
                    }

                    private File lazyGetFile() {
                        File toReturn = (File) dataFile.get();
                        if (toReturn != null)
                            return toReturn;
                        TOTorrent tor = download_manager.getTorrent();
                        String path_str = root_dir;
                        File simpleFile = null;
                        if (tor == null || tor.isSimpleTorrent()) {
                            // rumour has it tor can sometimes be null
                            simpleFile = download_manager.getAbsoluteSaveLocation();
                        } else {
                            byte[][] path_comps = torrent_file.getPathComponents();
                            for (int j = 0; j < path_comps.length; j++) {
                                String comp;
                                try {
                                    comp = locale_decoder.decodeString(path_comps[j]);
                                } catch (UnsupportedEncodingException e) {
                                    Debug.printStackTrace(e);
                                    comp = "undecodableFileName" + file_index;
                                }
                                comp = FileUtil.convertOSSpecificChars(comp, j != path_comps.length - 1);
                                path_str += (j == 0 ? "" : File.separator) + comp;
                            }
                        }
                        dataFile = new WeakReference(toReturn = simpleFile != null ? simpleFile : new File(path_str));
                        // System.out.println("new file:"+toReturn);
                        return toReturn;
                    }

                    @Override
                    public TOTorrentFile getTorrentFile() {
                        return (torrent_file);
                    }

                    @Override
                    public boolean setLink(File link_destination) {
                        /**
                         * If we a simple torrent, then we'll redirect the call to the download and move the
                         * data files that way - that'll keep everything in sync.
                         */
                        if (download_manager.getTorrent().isSimpleTorrent()) {
                            try {
                                download_manager.moveDataFiles(link_destination.getParentFile(), link_destination.getName());
                                return true;
                            } catch (DownloadManagerException e) {
                                // What should we do with the error?
                                return false;
                            }
                        }
                        return setLinkAtomic(link_destination);
                    }

                    @Override
                    public boolean setLinkAtomic(File link_destination) {
                        return (setFileLink(download_manager, res, this, lazyGetFile(), link_destination, null));
                    }

                    @Override
                    public boolean setLinkAtomic(File link_destination, FileUtil.ProgressListener pl) {
                        return (setFileLink(download_manager, res, this, lazyGetFile(), link_destination, pl));
                    }

                    @Override
                    public File getLink() {
                        return (download_manager.getDownloadState().getFileLink(file_index, lazyGetFile()));
                    }

                    @Override
                    public boolean setStorageType(int type) {
                        boolean[] change = new boolean[res.length];
                        change[file_index] = true;
                        return fileSetSkeleton.setStorageTypes(change, type)[file_index];
                    }

                    @Override
                    public int getStorageType() {
                        return (DiskManagerUtil.convertDMStorageTypeFromString(DiskManagerImpl.getStorageType(download_manager, file_index)));
                    }

                    @Override
                    public void flushCache() {
                    }

                    @Override
                    public DirectByteBuffer read(long offset, int length) throws IOException {
                        CacheFile temp;
                        try {
                            cache_read_mon.enter();
                            if (read_cache_file == null) {
                                try {
                                    int type = convertDMStorageTypeFromString(DiskManagerImpl.getStorageType(download_manager, file_index));
                                    read_cache_file = CacheFileManagerFactory.getSingleton().createFile(new CacheFileOwner() {

                                        @Override
                                        public String getCacheFileOwnerName() {
                                            return (download_manager.getInternalName());
                                        }

                                        @Override
                                        public TOTorrentFile getCacheFileTorrentFile() {
                                            return (torrent_file);
                                        }

                                        @Override
                                        public File getCacheFileControlFileDir() {
                                            return (download_manager.getDownloadState().getStateFile());
                                        }

                                        @Override
                                        public int getCacheMode() {
                                            return (CacheFileOwner.CACHE_MODE_NORMAL);
                                        }
                                    }, getFile(true), convertDMStorageTypeToCache(type));
                                } catch (Throwable e) {
                                    Debug.printStackTrace(e);
                                    throw (new IOException(e.getMessage()));
                                }
                            }
                            temp = read_cache_file;
                        } finally {
                            cache_read_mon.exit();
                        }
                        DirectByteBuffer buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_DM_READ, length);
                        try {
                            temp.read(buffer, offset, CacheFile.CP_READ_CACHE);
                        } catch (Throwable e) {
                            buffer.returnToPool();
                            Debug.printStackTrace(e);
                            throw (new IOException(e.getMessage()));
                        }
                        return (buffer);
                    }

                    @Override
                    public int getReadBytesPerSecond() {
                        CacheFile temp = read_cache_file;
                        if (temp == null) {
                            return (0);
                        }
                        return (0);
                    }

                    @Override
                    public int getWriteBytesPerSecond() {
                        return (0);
                    }

                    @Override
                    public long getETA() {
                        return (-1);
                    }

                    @Override
                    public void close() {
                        CacheFile temp;
                        try {
                            cache_read_mon.enter();
                            temp = read_cache_file;
                            read_cache_file = null;
                        } finally {
                            cache_read_mon.exit();
                        }
                        if (temp != null) {
                            try {
                                temp.close();
                            } catch (Throwable e) {
                                Debug.printStackTrace(e);
                            }
                        }
                    }

                    @Override
                    public void addListener(DiskManagerFileInfoListener listener) {
                        if (getDownloaded() == getLength()) {
                            try {
                                listener.dataWritten(0, getLength());
                                listener.dataChecked(0, getLength());
                            } catch (Throwable e) {
                                Debug.printStackTrace(e);
                            }
                        }
                    }

                    @Override
                    public void removeListener(DiskManagerFileInfoListener listener) {
                    }
                };
                res[i] = info;
            }
            loadFilePriorities(download_manager, fileSetSkeleton);
            loadFileDownloaded(download_manager, res);
            return (fileSetSkeleton);
        } finally {
            loading[0] = false;
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        return (new DiskManagerFileInfoSetImpl(new DiskManagerFileInfoImpl[0], null));
    }
}
Also used : ArrayList(java.util.ArrayList) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) CacheFile(com.biglybt.core.diskmanager.cache.CacheFile) LocaleUtilDecoder(com.biglybt.core.internat.LocaleUtilDecoder) WeakReference(java.lang.ref.WeakReference) DownloadManagerException(com.biglybt.core.download.DownloadManagerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) LogAlert(com.biglybt.core.logging.LogAlert) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) CacheFileOwner(com.biglybt.core.diskmanager.cache.CacheFileOwner) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) CacheFile(com.biglybt.core.diskmanager.cache.CacheFile) File(java.io.File)

Example 24 with TOTorrentFile

use of com.biglybt.core.torrent.TOTorrentFile in project BiglyBT by BiglySoftware.

the class DiskManagerImpl method getStorageTypes.

// Used by DownloadManagerImpl too.
public static String[] getStorageTypes(DownloadManager download_manager) {
    DownloadManagerState state = download_manager.getDownloadState();
    String[] types = state.getListAttribute(DownloadManagerState.AT_FILE_STORE_TYPES);
    if (types == null || types.length == 0) {
        TOTorrentFile[] files = download_manager.getTorrent().getFiles();
        types = new String[download_manager.getTorrent().getFiles().length];
        if (reorder_storage_mode) {
            int existing = state.getIntAttribute(DownloadManagerState.AT_REORDER_MIN_MB);
            if (existing < 0) {
                existing = reorder_storage_mode_min_mb;
                state.setIntAttribute(DownloadManagerState.AT_REORDER_MIN_MB, existing);
            }
            for (int i = 0; i < types.length; i++) {
                if (files[i].getLength() / (1024 * 1024) >= existing) {
                    types[i] = "R";
                } else {
                    types[i] = "L";
                }
            }
        } else {
            for (int i = 0; i < types.length; i++) {
                types[i] = "L";
            }
        }
        state.setListAttribute(DownloadManagerState.AT_FILE_STORE_TYPES, types);
    }
    return (types);
}
Also used : TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

Example 25 with TOTorrentFile

use of com.biglybt.core.torrent.TOTorrentFile in project BiglyBT by BiglySoftware.

the class TorrentOpenOptions method getFiles.

public TorrentOpenFileOptions[] getFiles() {
    if (files == null && torrent != null) {
        TOTorrentFile[] tfiles = torrent.getFiles();
        files = new TorrentOpenFileOptions[tfiles.length];
        Set<String> skip_extensons = TorrentUtils.getSkipExtensionsSet();
        long skip_min_size = COConfigurationManager.getLongParameter("File.Torrent.AutoSkipMinSizeKB") * 1024L;
        for (int i = 0; i < files.length; i++) {
            TOTorrentFile torrentFile = tfiles[i];
            // translated to locale
            String orgFullName = torrentFile.getRelativePath();
            String orgFileName = new File(orgFullName).getName();
            boolean wanted = true;
            if (skip_min_size > 0 && torrentFile.getLength() < skip_min_size) {
                wanted = false;
            } else if (skip_extensons.size() > 0) {
                int pos = orgFileName.lastIndexOf('.');
                if (pos != -1) {
                    String ext = orgFileName.substring(pos + 1);
                    wanted = !skip_extensons.contains(ext);
                }
            }
            files[i] = new TorrentOpenFileOptions(this, i, orgFullName, orgFileName, torrentFile.getLength(), wanted);
        }
    }
    return files;
}
Also used : TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Aggregations

TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)25 TOTorrent (com.biglybt.core.torrent.TOTorrent)11 File (java.io.File)9 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)5 DownloadManager (com.biglybt.core.download.DownloadManager)4 LocaleUtilDecoder (com.biglybt.core.internat.LocaleUtilDecoder)4 IOException (java.io.IOException)4 URL (java.net.URL)4 CacheFile (com.biglybt.core.diskmanager.cache.CacheFile)3 FMFileManagerException (com.biglybt.core.diskmanager.file.FMFileManagerException)3 DiskManager (com.biglybt.core.disk.DiskManager)2 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)2 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)2 FMFile (com.biglybt.core.diskmanager.file.FMFile)2 DownloadManagerException (com.biglybt.core.download.DownloadManagerException)2 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)2 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)2 PluginInterface (com.biglybt.pif.PluginInterface)2 Download (com.biglybt.pif.download.Download)2 RandomAccessFile (java.io.RandomAccessFile)2