/** * {@inheritDoc} * * @see Writable#write(DataOutput) */ public void write(DataOutput out) throws IOException { BSONEncoder enc = new BSONEncoder(); BasicOutputBuffer buf = new BasicOutputBuffer(); enc.set(buf); enc.putObject(_doc); enc.done(); out.writeInt(buf.size()); // For better performance we can copy BasicOutputBuffer.pipe(OutputStream) // to have a method signature that works with DataOutput out.write(buf.toByteArray()); }
/** {@inheritDoc} */ @Override public String toString() { BSONEncoder enc = new BSONEncoder(); BasicOutputBuffer buf = new BasicOutputBuffer(); enc.set(buf); enc.putObject(_doc); enc.done(); String str = buf.asString(); log.debug("Output As String: '" + str + "'"); return str; }
void _test(BSONObject o, int size, String hash) throws IOException { BSONEncoder e = new BSONEncoder(); OutputBuffer buf = new BasicOutputBuffer(); e.set(buf); e.putObject(o); assertEquals(size, buf.size()); assertEquals(hash, buf.md5()); e.done(); BSONDecoder d = new BSONDecoder(); BasicBSONCallback cb = new BasicBSONCallback(); int s = d.decode(new ByteArrayInputStream(buf.toByteArray()), cb); assertEquals(size, s); OutputBuffer buf2 = new BasicOutputBuffer(); e.set(buf2); e.putObject((BSONObject) cb.get()); assertEquals(size, buf2.size()); assertEquals(hash, buf2.md5()); }