use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method toJson.
@Override
public String toJson(final JsonWriterSettings settings) {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
ByteBuf duplicate = byteBuf.duplicate();
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
try {
jsonWriter.pipe(reader);
return stringWriter.toString();
} finally {
duplicate.release();
reader.close();
}
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class InternalStreamConnection method receiveResponseBuffers.
private ResponseBuffers receiveResponseBuffers() throws IOException {
ByteBuf headerByteBuffer = stream.read(REPLY_HEADER_LENGTH);
ReplyHeader replyHeader;
ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(headerByteBuffer);
try {
replyHeader = new ReplyHeader(headerInputBuffer, description.getMaxMessageSize());
} finally {
headerInputBuffer.close();
}
ByteBuf bodyByteBuffer = null;
if (replyHeader.getNumberReturned() > 0) {
bodyByteBuffer = stream.read(replyHeader.getMessageLength() - REPLY_HEADER_LENGTH);
}
return new ResponseBuffers(replyHeader, bodyByteBuffer);
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class DBEncoderAdapter method encode.
// TODO: this can be optimized to reduce copying of buffers. For that we'd need an InputBuffer that could iterate
// over an array of ByteBuffer instances from a PooledByteBufferOutputBuffer
@Override
public void encode(final BsonWriter writer, final DBObject document, final EncoderContext encoderContext) {
BasicOutputBuffer buffer = new BasicOutputBuffer();
try {
encoder.writeObject(buffer, document);
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(buffer.toByteArray()))));
try {
writer.pipe(reader);
} finally {
reader.close();
}
} finally {
buffer.close();
}
}
use of org.bson.io.ByteBufferBsonInput 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());
}
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;
}
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BasicBSONDecoder method decode.
@Override
public int decode(final byte[] bytes, final BSONCallback callback) {
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
try {
BsonWriter writer = new BSONCallbackAdapter(new BsonWriterSettings(), callback);
writer.pipe(reader);
//TODO check this.
return reader.getBsonInput().getPosition();
} finally {
reader.close();
}
}
Aggregations