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);
}
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);
}
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]) });
}
}
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);
}
}
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);
}
}
Aggregations