Search in sources :

Example 41 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class Compressor method compress.

void compress(final List<ByteBuf> source, final BsonOutput target) {
    BufferExposingByteArrayOutputStream baos = new BufferExposingByteArrayOutputStream(1024);
    OutputStream outputStream = null;
    try {
        outputStream = getOutputStream(baos);
        byte[] scratch = new byte[BUFFER_SIZE];
        for (ByteBuf cur : source) {
            while (cur.hasRemaining()) {
                int numBytes = Math.min(cur.remaining(), scratch.length);
                cur.get(scratch, 0, numBytes);
                outputStream.write(scratch, 0, numBytes);
            }
        }
    } catch (IOException e) {
        throw new MongoInternalException("Unexpected IOException", e);
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
        // ignore
        }
    }
    target.writeBytes(baos.getInternalBytes(), 0, baos.size());
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuf(org.bson.ByteBuf) MongoInternalException(com.mongodb.MongoInternalException)

Example 42 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class ByteBufferBsonOutput method pipe.

@Override
public int pipe(final OutputStream out) throws IOException {
    ensureOpen();
    byte[] tmp = new byte[INITIAL_BUFFER_SIZE];
    int total = 0;
    for (final ByteBuf cur : getByteBuffers()) {
        ByteBuf dup = cur.duplicate();
        while (dup.hasRemaining()) {
            int numBytesToCopy = Math.min(dup.remaining(), tmp.length);
            dup.get(tmp, 0, numBytesToCopy);
            out.write(tmp, 0, numBytesToCopy);
        }
        total += dup.limit();
    }
    return total;
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 43 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class ByteBufferBsonOutput method writeBytes.

@Override
public void writeBytes(final byte[] bytes, final int offset, final int length) {
    ensureOpen();
    int currentOffset = offset;
    int remainingLen = length;
    while (remainingLen > 0) {
        ByteBuf buf = getCurrentByteBuffer();
        int bytesToPutInCurrentBuffer = Math.min(buf.remaining(), remainingLen);
        buf.put(bytes, currentOffset, bytesToPutInCurrentBuffer);
        remainingLen -= bytesToPutInCurrentBuffer;
        currentOffset += bytesToPutInCurrentBuffer;
    }
    position += length;
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 44 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class PowerOfTwoBufferPoolTest method testReuse.

@Test
public void testReuse() {
    ByteBuf buf = pool.getBuffer((int) Math.pow(2, 10));
    ByteBuffer byteBuffer = buf.asNIO();
    buf.release();
    assertSame(byteBuffer, pool.getBuffer((int) Math.pow(2, 10)).asNIO());
}
Also used : ByteBuf(org.bson.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 45 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class TestInternalConnection method sendMessage.

@Override
public void sendMessage(final List<ByteBuf> byteBuffers, final int lastRequestId) {
    // repackage all byte buffers into a single byte buffer...
    int totalSize = 0;
    for (ByteBuf buf : byteBuffers) {
        totalSize += buf.remaining();
    }
    ByteBuffer combined = ByteBuffer.allocate(totalSize);
    for (ByteBuf buf : byteBuffers) {
        combined.put(buf.array(), 0, buf.remaining());
    }
    ((Buffer) combined).flip();
    Interaction interaction = replies.getFirst();
    if (interaction.responseBuffers != null) {
        ReplyHeader header = replaceResponseTo(interaction.responseBuffers.getReplyHeader(), lastRequestId);
        interaction.responseBuffers = (new ResponseBuffers(header, interaction.responseBuffers.getBodyByteBuffer()));
        sent.add(new ByteBufferBsonInput(new ByteBufNIO(combined)));
    } else if (interaction.sendException != null) {
        replies.removeFirst();
        throw interaction.sendException;
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) ByteBuf(org.bson.ByteBuf) ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

ByteBuf (org.bson.ByteBuf)48 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)9 ArrayList (java.util.ArrayList)6 BsonBinaryReader (org.bson.BsonBinaryReader)6 ByteBufNIO (org.bson.ByteBufNIO)4 MongoSocketReadException (com.mongodb.MongoSocketReadException)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)3 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 MongoInternalException (com.mongodb.MongoInternalException)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 JsonWriter (org.bson.json.JsonWriter)2 MongoClientException (com.mongodb.MongoClientException)1 MongoCompressor (com.mongodb.MongoCompressor)1 MongoException (com.mongodb.MongoException)1 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoSocketClosedException (com.mongodb.MongoSocketClosedException)1 MongoSocketException (com.mongodb.MongoSocketException)1