Search in sources :

Example 11 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class CacheFileWithoutCacheMT method read.

@Override
public void read(DirectByteBuffer[] buffers, long position, short policy) throws CacheFileManagerException {
    int read_length = 0;
    for (int i = 0; i < buffers.length; i++) {
        read_length += buffers[i].remaining(DirectByteBuffer.SS_CACHE);
    }
    FMFile file = null;
    try {
        file = getFile();
        file.read(buffers, position);
        manager.fileBytesRead(read_length);
        bytes_read += read_length;
    } catch (FMFileManagerException e) {
        manager.rethrow(this, e);
    } finally {
        releaseFile(file);
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) FMFile(com.biglybt.core.diskmanager.file.FMFile)

Example 12 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class CacheFileWithoutCacheMT method moveFile.

@Override
public void moveFile(File new_file, FileUtil.ProgressListener pl) throws CacheFileManagerException {
    try {
        synchronized (this) {
            moving = true;
        }
        while (true) {
            synchronized (this) {
                boolean surviving = false;
                for (int i = 1; i < files_use_count.length; i++) {
                    if (files_use_count[i] > 0) {
                        surviving = true;
                        break;
                    }
                }
                if (!surviving) {
                    for (int i = 1; i < files_use_count.length; i++) {
                        FMFile file = files[i];
                        if (file.isClone()) {
                            synchronized (CacheFileWithoutCacheMT.class) {
                                num_clones--;
                            }
                        }
                        file.close();
                    }
                    files = new FMFile[] { base_file };
                    files_use_count = new int[] { files_use_count[0] };
                    base_file.moveFile(new_file, pl);
                    break;
                }
            }
            try {
                System.out.println("CacheFileWithoutCacheMT: waiting for clones to die");
                Thread.sleep(250);
            } catch (Throwable e) {
            }
        }
    } catch (FMFileManagerException e) {
        manager.rethrow(this, e);
    } finally {
        synchronized (this) {
            moving = false;
        }
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) FMFile(com.biglybt.core.diskmanager.file.FMFile)

Example 13 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileAccessLinear method read.

public void read(FileAccessor fa, DirectByteBuffer buffer, long offset) throws FMFileManagerException {
    if (fa == null) {
        throw new FMFileManagerException("read: fa is null");
    }
    FileChannel fc = fa.getChannel();
    if (!fc.isOpen()) {
        Debug.out("FileChannel is closed: " + owner.getName());
        throw (new FMFileManagerException("read - file is closed"));
    }
    AEThread2.setDebug(owner);
    try {
        if (USE_MMAP) {
            long remainingInFile = fc.size() - offset;
            long remainingInTargetBuffer = buffer.remaining(DirectByteBuffer.SS_FILE);
            MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, Math.min(remainingInFile, remainingInTargetBuffer));
            buffer.put(DirectByteBuffer.SS_FILE, buf);
        } else {
            fc.position(offset);
            while (fc.position() < fc.size() && buffer.hasRemaining(DirectByteBuffer.SS_FILE)) buffer.read(DirectByteBuffer.SS_FILE, fc);
        }
    } catch (Exception e) {
        Debug.printStackTrace(e);
        throw (new FMFileManagerException("read fails", e));
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException)

Example 14 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileImpl method closeSupport.

protected void closeSupport(boolean explicit) throws FMFileManagerException {
    FMFileManagerException flush_exception = null;
    try {
        flush();
    } catch (FMFileManagerException e) {
        flush_exception = e;
    }
    if (fa == null) {
        if (explicit) {
            releaseFile();
            deleteDirs();
        }
    } else {
        try {
            fa.close();
        } catch (Throwable e) {
            throw (new FMFileManagerException("close fails", e));
        } finally {
            fa = null;
            if (explicit) {
                releaseFile();
            }
        }
    }
    if (flush_exception != null) {
        throw (flush_exception);
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException)

Example 15 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileAccessLinear method read.

public void read(RandomAccessFile raf, DirectByteBuffer buffer, long offset) throws FMFileManagerException {
    if (raf == null) {
        throw new FMFileManagerException("read: raf is null");
    }
    FileChannel fc = raf.getChannel();
    if (!fc.isOpen()) {
        Debug.out("FileChannel is closed: " + owner.getName());
        throw (new FMFileManagerException("read - file is closed"));
    }
    AEThread2.setDebug(owner);
    try {
        if (USE_MMAP) {
            long remainingInFile = fc.size() - offset;
            long remainingInTargetBuffer = buffer.remaining(DirectByteBuffer.SS_FILE);
            MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, Math.min(remainingInFile, remainingInTargetBuffer));
            buffer.put(DirectByteBuffer.SS_FILE, buf);
        } else {
            fc.position(offset);
            while (fc.position() < fc.size() && buffer.hasRemaining(DirectByteBuffer.SS_FILE)) buffer.read(DirectByteBuffer.SS_FILE, fc);
        }
    } catch (Exception e) {
        Debug.printStackTrace(e);
        throw (new FMFileManagerException("read fails", e));
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException)

Aggregations

FMFileManagerException (com.biglybt.core.diskmanager.file.FMFileManagerException)35 FMFile (com.biglybt.core.diskmanager.file.FMFile)14 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)8 File (java.io.File)7 IOException (java.io.IOException)7 ByteBuffer (java.nio.ByteBuffer)6 MappedByteBuffer (java.nio.MappedByteBuffer)6 FileChannel (java.nio.channels.FileChannel)6 CacheFileManagerException (com.biglybt.core.diskmanager.cache.CacheFileManagerException)3 LogEvent (com.biglybt.core.logging.LogEvent)3 RandomAccessFile (java.io.RandomAccessFile)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 FMFileOwner (com.biglybt.core.diskmanager.file.FMFileOwner)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 FileNotFoundException (java.io.FileNotFoundException)1