Esempio n. 1
0
 /**
  * {@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());
 }
Esempio n. 2
0
 /** {@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;
 }
Esempio n. 3
0
  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());
  }