Refine search
@Override public byte[] encode(final BSONObject document) { OutputBuffer outputBuffer = new BasicOutputBuffer(); set(outputBuffer); putObject(document); done(); return outputBuffer.toByteArray(); }
@Override public void writeString(final String str) { writeInt(0); // making space for size int strLen = writeCharacters(str, false); writeInt32(getPosition() - strLen - 4, strLen); }
@Override public String toString() { return getClass().getName() + " size: " + size() + " pos: " + getPosition(); }
/** * Gets a copy of the buffered bytes. * * @return the byte array * @see org.bson.io.OutputBuffer#pipe(java.io.OutputStream) */ public byte[] toByteArray() { try { ByteArrayOutputStream bout = new ByteArrayOutputStream(size()); pipe(bout); return bout.toByteArray(); } catch (IOException ioe) { throw new RuntimeException("should be impossible", ioe); } }
private void putBinary(String name, int type, byte[] data) { _put(BINARY, name); int totalLen = data.length; if (type == B_BINARY) totalLen += 4; _buf.writeInt(totalLen); _buf.write(type); if (type == B_BINARY) _buf.writeInt(totalLen - 4); int before = _buf.getPosition(); _buf.write(data); int after = _buf.getPosition(); // com.mongodb.util.MyAsserts.assertEquals( after - before , data.length // ); }
protected void putCodeWScope(String name, CodeWScope code) { _put(CODE_W_SCOPE, name); int temp = _buf.getPosition(); _buf.writeInt(0); _putValueString(code.getCode()); putObject(code.getScope()); _buf.writeInt(temp, _buf.getPosition() - temp); }
protected void putNumber(String name, Number n) { if(!(n instanceof Integer) && !(n instanceof Short) && !(n instanceof Byte) && !(n instanceof AtomicInteger)) { if(!(n instanceof Long) && !(n instanceof AtomicLong)) { if(!(n instanceof Float) && !(n instanceof Double)) { throw new IllegalArgumentException("can\'t serialize " + n.getClass()); } this._put(1, name); this._buf.writeDouble(n.doubleValue()); } else { this._put(18, name); this._buf.writeLong(n.longValue()); } } else { this._put(16, name); this._buf.writeInt(n.intValue()); } }
protected void putCode(String name, Code code) { this._put(13, name); int temp = this._buf.getPosition(); this._putValueString(code.getCode()); }
/** * Encodes a {@code BSONObject}. This is for the higher level api calls. * * @param document the document to encode * @return the number of characters in the encoding */ @Override public int putObject(final BSONObject document) { int startPosition = getOutputBuffer().getPosition(); bsonWriter.writeStartDocument(); if (isTopLevelDocument() && document.containsField("_id")) { _putObjectField("_id", document.get("_id")); } for (final String key : document.keySet()) { if (isTopLevelDocument() && key.equals("_id")) { continue; } _putObjectField(key, document.get(key)); } bsonWriter.writeEndDocument(); return getOutputBuffer().getPosition() - startPosition; }
private static byte[] toBson(final DBObject dbObject) { OutputBuffer outputBuffer = new BasicOutputBuffer(); DBObjectCodec.getDefaultRegistry().get(DBObject.class).encode(new BsonBinaryWriter(outputBuffer), dbObject, EncoderContext.builder().build()); return outputBuffer.toByteArray(); }
protected void _putValueString(String s) { int lenPos = _buf.getPosition(); _buf.writeInt(0); // making space for size int strLen = _put(s); _buf.writeInt(lenPos, strLen); }
public void writeInt(int pos, int x) { final int save = getPosition(); setPosition(pos); writeInt(x); setPosition(save); }
/** @deprecated */ @Deprecated protected void _put(int type, String name) { this._buf.write(type); this._buf.writeCString(name); }
@Override public void writeDouble(final double x) { writeLong(Double.doubleToRawLongBits(x)); }
protected void putDecimal(String name, BSONDecimal decimal) { int size = decimal.getSize(); int typemod = decimal.getTypemod(); short signscale = decimal.getSignScale(); short weight = decimal.getWeight(); short[] digits = decimal.getDigits(); // decimal is kept in bson in follow format: // type+name+size+typemod+dscale+weight+digits // put type+name _put(NUMBER_DECIMAL, name); // size+typemod+dscale+weight+data _buf.writeInt(size); _buf.writeInt(typemod); _buf.writeShort(signscale); _buf.writeShort(weight); for (int i = 0; i < digits.length; i++) { _buf.writeShort(digits[i]); } }