Search in sources :

Example 1 with DiskAccessRequestListener

use of com.biglybt.core.diskmanager.access.DiskAccessRequestListener in project BiglyBT by BiglySoftware.

the class DMWriterImpl method zeroFile.

@Override
public boolean zeroFile(DiskManagerFileInfoImpl file, long length) throws DiskManagerException {
    CacheFile cache_file = file.getCacheFile();
    try {
        if (length == 0) {
            // create a zero-length file if it is listed in the torrent
            cache_file.setLength(0);
        } else {
            int buffer_size = pieceLength < MIN_ZERO_BLOCK ? MIN_ZERO_BLOCK : pieceLength;
            buffer_size = ((buffer_size + 1023) / 1024) * 1024;
            DirectByteBuffer buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_DM_ZERO, buffer_size);
            long remainder = length;
            long written = 0;
            try {
                final byte[] blanks = new byte[1024];
                for (int i = 0; i < buffer_size / 1024; i++) {
                    buffer.put(DirectByteBuffer.SS_DW, blanks);
                }
                buffer.position(DirectByteBuffer.SS_DW, 0);
                while (remainder > 0 && !stopped) {
                    int write_size = buffer_size;
                    if (remainder < write_size) {
                        write_size = (int) remainder;
                        buffer.limit(DirectByteBuffer.SS_DW, write_size);
                    }
                    final AESemaphore sem = new AESemaphore("DMW&C:zeroFile");
                    final Throwable[] op_failed = { null };
                    disk_access.queueWriteRequest(cache_file, written, buffer, false, new DiskAccessRequestListener() {

                        @Override
                        public void requestComplete(DiskAccessRequest request) {
                            sem.release();
                        }

                        @Override
                        public void requestCancelled(DiskAccessRequest request) {
                            op_failed[0] = new Throwable("Request cancelled");
                            sem.release();
                        }

                        @Override
                        public void requestFailed(DiskAccessRequest request, Throwable cause) {
                            op_failed[0] = cause;
                            sem.release();
                        }

                        @Override
                        public int getPriority() {
                            return (-1);
                        }

                        @Override
                        public void requestExecuted(long bytes) {
                        }
                    });
                    sem.reserve();
                    if (op_failed[0] != null) {
                        throw (op_failed[0]);
                    }
                    buffer.position(DirectByteBuffer.SS_DW, 0);
                    written += write_size;
                    remainder -= write_size;
                    disk_manager.setAllocated(disk_manager.getAllocated() + write_size);
                    disk_manager.setPercentDone((int) ((disk_manager.getAllocated() * 1000) / totalLength));
                }
            } finally {
                buffer.returnToPool();
            }
            cache_file.flushCache();
        }
        if (stopped) {
            return false;
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        throw new DiskManagerException(e);
    }
    return true;
}
Also used : CacheFile(com.biglybt.core.diskmanager.cache.CacheFile) DiskAccessRequestListener(com.biglybt.core.diskmanager.access.DiskAccessRequestListener) DiskAccessRequest(com.biglybt.core.diskmanager.access.DiskAccessRequest)

Aggregations

DiskAccessRequest (com.biglybt.core.diskmanager.access.DiskAccessRequest)1 DiskAccessRequestListener (com.biglybt.core.diskmanager.access.DiskAccessRequestListener)1 CacheFile (com.biglybt.core.diskmanager.cache.CacheFile)1