Mongodb 3.0 Java driver output Document to byte array

1,487 views
Skip to first unread message

Aplomb Chen

unread,
Aug 7, 2015, 2:40:08 AM8/7/15
to mongod...@googlegroups.com
I have a question that is it possible to output Document into byte array, so that I can transfer the Document into another server through any type of RPC. 

I know I can convert Document into JSON format, but I am a bit worried about the penaty of parsing JSON string back to Document. 

I have tried the API, but no luck, so please help me to clarify whether it is possible to output a Document directly into byte array for sending to another server and another server can convert the byte array back to Document? 

Many thanks!

Aplomb
Best regards 

Ross Lawley

unread,
Sep 22, 2015, 5:44:59 AM9/22/15
to mongodb-user
Hi Aplomb,

Apologies for the delay, this is not easily achievable via the api but if you follow the logic from the Document.toJson and the Document.parse methods you can use the BsonBinaryReader / Writer to achieve your goals.

The following is an example of creating static helpers to write to an InputStream and to read from an InputStream:

import org.bson.BsonBinaryReader;
import org.bson.BsonBinaryWriter;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.io.BasicOutputBuffer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import static java.util.Arrays.asList;

public class DocumentToBinary {

private static Codec<Document> DOCUMENT_CODEC = new DocumentCodec();

public static void main(final String[] args) {
Document myDocument = new Document("_id", 1).append("key1", asList(1, 2, 3, 4));

try {
Document myCopiedDocument = fromInputStream(toInputStream(myDocument));
assert myCopiedDocument.equals(myDocument);
} catch (IOException e) {
e.printStackTrace();
}

}

public static InputStream toInputStream(final Document document) {
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
DOCUMENT_CODEC.encode(writer, document, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
return new ByteArrayInputStream(outputBuffer.toByteArray());
}

public static Document fromInputStream(final InputStream input) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(outputStream.toByteArray()));
return DOCUMENT_CODEC.decode(bsonReader, DecoderContext.builder().build());
}
}


This is also available as a gist: https://gist.github.com/rozza/9c94808ed5b4f1edca75

I've added this as a feature request to our issue tracker https://jira.mongodb.org/browse/JAVA-1975, please click "watch" on the ticket to follow updates.

Ross
Reply all
Reply to author
Forward
0 new messages