use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class ReplyMessageTest method shouldThrowExceptionIfOpCodeIsIncorrect.
@Test(expected = MongoInternalException.class)
public void shouldThrowExceptionIfOpCodeIsIncorrect() {
int badOpCode = 2;
ByteBuffer headerByteBuffer = ByteBuffer.allocate(36);
headerByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
headerByteBuffer.putInt(36);
headerByteBuffer.putInt(2456);
headerByteBuffer.putInt(5);
headerByteBuffer.putInt(badOpCode);
headerByteBuffer.putInt(0);
headerByteBuffer.putLong(0);
headerByteBuffer.putInt(0);
headerByteBuffer.putInt(0);
headerByteBuffer.flip();
ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(headerByteBuffer));
ReplyHeader replyHeader = new ReplyHeader(headerInputBuffer, ConnectionDescription.getDefaultMaxMessageSize());
new ReplyMessage<Document>(replyHeader, 5);
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method toBsonDocument.
private BsonDocument toBsonDocument() {
ByteBuf duplicateByteBuf = byteBuf.duplicate();
BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf));
try {
return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build());
} finally {
duplicateByteBuf.release();
bsonReader.close();
}
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method findInDocument.
private <T> T findInDocument(final Finder<T> finder) {
ByteBuf duplicateByteBuf = byteBuf.duplicate();
BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf));
try {
bsonReader.readStartDocument();
while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
T found = finder.find(bsonReader);
if (found != null) {
return found;
}
}
bsonReader.readEndDocument();
} finally {
duplicateByteBuf.release();
bsonReader.close();
}
return finder.notFound();
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testMarkAndReset.
// CHECKSTYLE:OFF
@Test
public void testMarkAndReset() throws IOException {
writer.writeStartDocument();
writer.writeStartArray("a");
{
writer.writeStartDocument();
writer.writeInt32("i", 1);
writer.writeEndDocument();
}
writer.mark();
{
writer.writeStartDocument();
writer.writeInt32("i", 2);
writer.writeEndDocument();
}
writer.reset();
{
writer.writeStartDocument();
writer.writeInt32("i", 3);
writer.writeEndDocument();
}
writer.writeEndArray();
writer.writeEndDocument();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffer.pipe(baos);
ByteBufferBsonInput basicInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer);
try {
reader.readStartDocument();
reader.readName("a");
reader.readStartArray();
{
reader.readStartDocument();
assertEquals(1, reader.readInt32("i"));
reader.readEndDocument();
}
{
reader.readStartDocument();
assertEquals(3, reader.readInt32("i"));
reader.readEndDocument();
}
reader.readEndArray();
reader.readEndDocument();
} finally {
reader.close();
}
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeNestedDocument.
@Test
public void testPipeNestedDocument() {
// {
// "value" : { "a" : true},
// "b" : 2
// }
writer.writeStartDocument();
writer.writeStartDocument("value");
writer.writeBoolean("a", true);
writer.writeEndDocument();
writer.writeInt32("b", 2);
writer.writeEndDocument();
byte[] bytes = buffer.toByteArray();
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
reader1.readStartDocument();
reader1.readName();
//pipe {'a':true} to writer
newWriter.pipe(reader1);
//continue reading from the same reader
assertEquals(BsonType.INT32, reader1.readBsonType());
assertEquals("b", reader1.readName());
assertEquals(2, reader1.readInt32());
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
assertEquals(BsonType.BOOLEAN, reader2.readBsonType());
assertEquals("a", reader2.readName());
assertEquals(true, reader2.readBoolean());
reader2.readEndDocument();
}
Aggregations