Search in sources :

Example 11 with DirectByteBuffer

use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.

the class DiskManagerImpl method write.

@Override
public DiskManagerWriteRequest write(final int piece_number, final int offset, PooledByteBuffer data, final DiskManagerWriteRequestListener listener) throws DiskManagerException {
    DirectByteBuffer buffer = ((PooledByteBufferImpl) data).getBuffer();
    if (!disk_manager.checkBlockConsistencyForWrite("plugin", piece_number, offset, buffer)) {
        throw (new DiskManagerException("write invalid - parameters incorrect"));
    }
    final int length = buffer.remaining(DirectByteBuffer.SS_EXTERNAL);
    final DMWR request = new DMWR(disk_manager.createWriteRequest(piece_number, offset, buffer, null), length);
    disk_manager.enqueueWriteRequest(request.getDelegate(), new com.biglybt.core.disk.DiskManagerWriteRequestListener() {

        @Override
        public void writeCompleted(com.biglybt.core.disk.DiskManagerWriteRequest _request) {
            DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
            DiskManagerPiece dm_piece = dm_pieces[piece_number];
            if (!dm_piece.isDone()) {
                int current_offset = offset;
                for (int i = 0; i < length; i += DiskManager.BLOCK_SIZE) {
                    dm_piece.setWritten(current_offset / DiskManager.BLOCK_SIZE);
                    current_offset += DiskManager.BLOCK_SIZE;
                }
            }
            listener.complete(request);
        }

        @Override
        public void writeFailed(com.biglybt.core.disk.DiskManagerWriteRequest _request, Throwable _cause) {
            listener.failed(request, new DiskManagerException("read failed", _cause));
        }
    });
    return (request);
}
Also used : PooledByteBufferImpl(com.biglybt.pifimpl.local.utils.PooledByteBufferImpl) com.biglybt.pif.disk(com.biglybt.pif.disk) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Example 12 with DirectByteBuffer

use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.

the class GenericMessageDecoder method performStreamDecode.

@Override
public int performStreamDecode(Transport transport, int max_bytes) throws IOException {
    protocol_bytes_last_read = 0;
    data_bytes_last_read = 0;
    long total_read = 0;
    while (total_read < max_bytes) {
        long bytes_read;
        int read_lim = (int) (max_bytes - total_read);
        ByteBuffer payload_buffer = buffers[1];
        if (payload_buffer == null) {
            int rem = length_buffer.remaining();
            int lim = length_buffer.limit();
            if (rem > read_lim) {
                length_buffer.limit(length_buffer.position() + read_lim);
            }
            bytes_read = transport.read(buffers, 0, 1);
            length_buffer.limit(lim);
            protocol_bytes_last_read += bytes_read;
            if (length_buffer.hasRemaining()) {
                total_read += bytes_read;
                break;
            } else {
                length_buffer.flip();
                int size = length_buffer.getInt();
                if (size > MAX_MESSAGE_LENGTH) {
                    Debug.out("Message too large for generic payload");
                    throw (new IOException("message too large"));
                }
                buffers[1] = ByteBuffer.allocate(size);
                length_buffer.flip();
            }
        } else {
            int rem = payload_buffer.remaining();
            int lim = payload_buffer.limit();
            if (rem > read_lim) {
                payload_buffer.limit(payload_buffer.position() + read_lim);
            }
            bytes_read = transport.read(buffers, 1, 1);
            payload_buffer.limit(lim);
            data_bytes_last_read += bytes_read;
            if (payload_buffer.hasRemaining()) {
                total_read += bytes_read;
                break;
            }
            payload_buffer.flip();
            messages.add(new GenericMessage(msg_type, msg_desc, new DirectByteBuffer(payload_buffer), false));
            buffers[1] = null;
        }
        total_read += bytes_read;
    }
    if (destroyed) {
        throw (new IOException("decoder has been destroyed"));
    }
    return ((int) total_read);
}
Also used : IOException(java.io.IOException) DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer) ByteBuffer(java.nio.ByteBuffer) DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Example 13 with DirectByteBuffer

use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.

the class GenericMessageEncoder method encodeMessage.

@Override
public RawMessage[] encodeMessage(Message _message) {
    GenericMessage message = (GenericMessage) _message;
    DirectByteBuffer payload = message.getPayload();
    if (message.isAlreadyEncoded()) {
        return (new RawMessage[] { new RawMessageImpl(message, new DirectByteBuffer[] { payload }, RawMessage.PRIORITY_NORMAL, // send immediately
        true, new Message[0]) });
    } else {
        DirectByteBuffer header = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_EXTERNAL, 4);
        header.putInt(DirectByteBuffer.SS_MSG, payload.remaining(DirectByteBuffer.SS_MSG));
        header.flip(DirectByteBuffer.SS_MSG);
        return (new RawMessage[] { new RawMessageImpl(message, new DirectByteBuffer[] { header, payload }, RawMessage.PRIORITY_NORMAL, // send immediately
        true, new Message[0]) });
    }
}
Also used : RawMessageImpl(com.biglybt.core.networkmanager.impl.RawMessageImpl) RawMessage(com.biglybt.core.networkmanager.RawMessage) Message(com.biglybt.core.peermanager.messaging.Message) DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Example 14 with DirectByteBuffer

use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.

the class FMFileAccessCompact method read.

@Override
public void read(RandomAccessFile raf, DirectByteBuffer[] buffers, long position) throws FMFileManagerException {
    for (int i = 0; i < buffers.length; i++) {
        DirectByteBuffer buffer = buffers[i];
        int len = buffers[i].limit(SS) - buffers[i].position(SS);
        read(raf, buffer, position);
        int rem = buffers[i].remaining(SS);
        position += len - rem;
        if (rem > 0) {
            break;
        }
    }
    if (position > current_length) {
        setLength(raf, position);
    }
}
Also used : DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Example 15 with DirectByteBuffer

use of com.biglybt.core.util.DirectByteBuffer in project BiglyBT by BiglySoftware.

the class FMFileAccessCompact method write.

@Override
public void write(RandomAccessFile raf, DirectByteBuffer[] buffers, long position) throws FMFileManagerException {
    for (int i = 0; i < buffers.length; i++) {
        DirectByteBuffer buffer = buffers[i];
        int len = buffers[i].limit(SS) - buffers[i].position(SS);
        write(raf, buffer, position);
        position += len;
    }
    if (position > current_length) {
        setLength(raf, position);
    }
}
Also used : DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Aggregations

DirectByteBuffer (com.biglybt.core.util.DirectByteBuffer)33 ByteBuffer (java.nio.ByteBuffer)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 RawMessage (com.biglybt.core.networkmanager.RawMessage)6 IOException (java.io.IOException)6 Message (com.biglybt.core.peermanager.messaging.Message)5 RawMessageImpl (com.biglybt.core.networkmanager.impl.RawMessageImpl)3 CacheFileManagerException (com.biglybt.core.diskmanager.cache.CacheFileManagerException)2 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)1 DiskManagerFileInfoImpl (com.biglybt.core.disk.impl.DiskManagerFileInfoImpl)1 DiskManagerRecheckInstance (com.biglybt.core.disk.impl.DiskManagerRecheckInstance)1 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)1 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)1 CacheFile (com.biglybt.core.diskmanager.cache.CacheFile)1 LogEvent (com.biglybt.core.logging.LogEvent)1 MessageException (com.biglybt.core.peermanager.messaging.MessageException)1 AESemaphore (com.biglybt.core.util.AESemaphore)1 ByteArrayHashMap (com.biglybt.core.util.ByteArrayHashMap)1