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...
1 parent 0cc6e55 commit 3f7ddc73dee5053ebaaf5a7e87415cd5897728f9 @kohanyirobert committed Nov 24, 2012
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,23 +51,23 @@ 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);
}
},
DOUBLE {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.putDouble(((Double) reference).doubleValue());
}
},
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,23 +104,23 @@ 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);
}
},
OBJECT_ID {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put(((BsonObjectId) reference).objectId());
}
},
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,21 +130,21 @@ 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());
}
},
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,24 +190,34 @@ 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());
}
},
TIMESTAMP {
@Override
- public void writeTo(ByteBuffer buffer, Object reference) {
+ public void checkedWriteTo(ByteBuffer buffer, Object reference) {
buffer.put(((BsonTimestamp) reference).timestamp());
}
},
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);
}

0 comments on commit 3f7ddc7

Please sign in to comment.