### Question:
BsonDecoder { public static Document decodeBson(ByteBuf buffer) { final int totalObjectLength = buffer.readIntLE(); final int length = totalObjectLength - 4; if (buffer.readableBytes() < length) { throw new IllegalArgumentException("Too few bytes to read: " + buffer.readableBytes() + ". Expected: " + length); } if (length > BsonConstants.MAX_BSON_OBJECT_SIZE) { throw new IllegalArgumentException("BSON object too large: " + length + " bytes"); } Document document = new Document(); int start = buffer.readerIndex(); while (buffer.readerIndex() - start < length) { byte type = buffer.readByte(); if (type == BsonConstants.TERMINATING_BYTE) { return document; } String name = decodeCString(buffer); Object value = decodeValue(type, buffer); Object existingValue = document.put(name, value); Assert.isNull(existingValue, () -> "Document already contains field '" + name + "'"); } throw new IllegalArgumentException("illegal BSON object. Terminating byte not found. totalObjectLength = " + totalObjectLength); } private  BsonDecoder(); static Document decodeBson(ByteBuf buffer); static Object decodeValue(byte type, ByteBuf buffer); static String decodeCString(ByteBuf buffer);  }### Answer:
@Test void testEncodeDecodeRoundtrip() throws Exception { List<Document> objects = new ArrayList<>(); objects.add(new Document("key", MaxKey.getInstance()).append("foo", "bar")); objects.add(new Document("key", MinKey.getInstance()).append("test", MaxKey.getInstance())); for (Document document : objects) { ByteBuf buffer = Unpooled.buffer(); try { BsonEncoder.encodeDocument(document, buffer); Document decodedObject = BsonDecoder.decodeBson(buffer); assertThat(decodedObject).isEqualTo(document); } finally { buffer.release(); } } }