Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update DefaultWriter

Add final writeTo implementation which checks if the buffer passed to it is OK
and delegates to the abstract checkedWriteTo method which is implemented by all
enum values in the class.
  • Loading branch information...
commit 3f7ddc73dee5053ebaaf5a7e87415cd5897728f9 1 parent 0cc6e55
@kohanyirobert authored
Showing with 29 additions and 17 deletions.
  1. +29 −17 src/main/java/com/github/kohanyirobert/ebson/DefaultWriter.java
View
46 src/main/java/com/github/kohanyirobert/ebson/DefaultWriter.java
@@ -2,12 +2,14 @@
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
@@ -20,7 +22,7 @@
DOCUMENT {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
int markedPosition = buffer.position();
buffer.position(markedPosition + Ints.BYTES);
BsonWriter fieldWriter = BsonToken.FIELD.writer();
@@ -35,7 +37,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
FIELD {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
Entry<?, ?> entry = (Entry<?, ?>) reference;
BsonObject bsonObject = BsonObject.find(entry.getValue() == null
? null
@@ -49,7 +51,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
KEY {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put(((String) reference).getBytes(Charsets.UTF_8)).put(BsonBytes.EOO);
}
},
@@ -57,7 +59,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
DOUBLE {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.putDouble(((Double) reference).doubleValue());
}
},
@@ -65,7 +67,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
STRING {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
byte[] bytes = ((String) reference).getBytes(Charsets.UTF_8);
buffer.putInt(bytes.length + 1).put(bytes).put(BsonBytes.EOO);
}
@@ -74,7 +76,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
ARRAY {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
Object array = reference instanceof Collection
? ((Collection<?>) reference).toArray()
: reference;
@@ -89,7 +91,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
BINARY {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
int markedPosition = buffer.position();
buffer.position(markedPosition + Ints.BYTES);
BsonBinary bsonBinary = BsonBinary.find(reference.getClass());
@@ -102,7 +104,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
GENERIC {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put((byte[]) reference);
}
},
@@ -110,7 +112,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
OBJECT_ID {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put(((BsonObjectId) reference).objectId());
}
},
@@ -118,7 +120,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
BOOLEAN {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put(((Boolean) reference).booleanValue()
? BsonBytes.TRUE
: BsonBytes.FALSE);
@@ -128,7 +130,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
UTC_DATE_TIME {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.putLong(((Date) reference).getTime());
}
},
@@ -136,13 +138,13 @@ public void writeTo(ByteBuffer buffer, Object reference) {
NULL {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {}
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {}
},
REGULAR_EXPRESSION {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
Pattern regularExpression = (Pattern) reference;
BsonWriter keyWriter = BsonToken.KEY.writer();
keyWriter.writeTo(buffer, regularExpression.pattern());
@@ -179,7 +181,7 @@ private boolean hasFlag(int flags, int flag) {
SYMBOL {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
ByteBuffer symbol = ((BsonSymbol) reference).symbol();
buffer.putInt(symbol.capacity() + 1).put(symbol).put(BsonBytes.EOO);
}
@@ -188,7 +190,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
INT32 {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.putInt(((Integer) reference).intValue());
}
},
@@ -196,7 +198,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
TIMESTAMP {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put(((BsonTimestamp) reference).timestamp());
}
},
@@ -204,8 +206,18 @@ public void writeTo(ByteBuffer buffer, Object reference) {
INT64 {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.putLong(((Long) reference).longValue());
}
};
+
+ @Override
+ public final void writeTo(ByteBuffer buffer, Object reference) {
+ Preconditions.checkNotNull(buffer, "null buffer");
+ Preconditions.checkArgument(buffer.order() == ByteOrder.LITTLE_ENDIAN,
+ "buffer has big-endian byte order; expected little-endian");
+ checkedWriteTo(buffer, reference);
+ }
+
+ abstract void checkedWriteTo(ByteBuffer buffer, Object reference);
}
Please sign in to comment.
Something went wrong with that request. Please try again.