#21
authorVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Wed, 27 Mar 2013 10:30:57 +0000 (17:30 +0700)
committerVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Wed, 27 Mar 2013 10:30:57 +0000 (17:30 +0700)
jejdb/src/java/org/ejdb/bson/BSONDecoder.java
jejdb/src/java/org/ejdb/bson/BSONEncoder.java
jejdb/src/java/org/ejdb/bson/BSONException.java [new file with mode: 0644]
jejdb/src/java/org/ejdb/bson/io/InputBuffer.java
jejdb/src/java/org/ejdb/bson/io/OutputBuffer.java

index f4ee2ea..1b96e8d 100644 (file)
@@ -42,7 +42,7 @@ class BSONDecoder {
         BSONObject result = this.readObject(input.subBuffer(length - 5));
 
         if (0x00 != input.read()) {
-            throw new IllegalArgumentException("unexpected end of document");
+            throw new BSONException("unexpected end of document");
         }
 
         return result;
@@ -84,7 +84,7 @@ class BSONDecoder {
                     int binlen = input.readInt();
                     byte subtype = input.read();
                     if (0x00 != subtype) {
-                        throw new IllegalArgumentException("unexpected binary type: " + subtype);
+                        throw new BSONException("unexpected binary type: " + subtype);
                     }
                     result.put(name, input.readBytes(binlen));
                     break;
@@ -96,7 +96,7 @@ class BSONDecoder {
                 case BSON.BOOLEAN:
                     byte bvalue = input.read();
                     if (0x00 != bvalue && 0x01 != bvalue) {
-                        throw new IllegalArgumentException("unexpected boolean value");
+                        throw new BSONException("unexpected boolean value");
                     }
                     result.put(name, 0x01 == bvalue);
                     break;
@@ -123,7 +123,7 @@ class BSONDecoder {
                     break;
 
                 default:
-                    throw new IllegalArgumentException("unexpected type: " + type);
+                    throw new BSONException("unexpected type: " + type);
             }
         }
 
index 3de9cd2..5349497 100644 (file)
@@ -91,7 +91,7 @@ class BSONEncoder {
         } else if (value instanceof Pattern) {
             writeRegex(name, (Pattern) value);
         } else {
-            throw new IllegalArgumentException("can not serialize object: " + value.getClass().getName());
+            throw new BSONException("can not serialize object: " + value.getClass().getName());
         }
     }
 
@@ -110,7 +110,7 @@ class BSONEncoder {
             writeFieldSpec(BSON.DOUBLE, name);
             output.writeDouble(value.doubleValue());
         } else {
-            throw new IllegalArgumentException("can not serialize object: " + value.getClass().getName());
+            throw new BSONException("can not serialize object: " + value.getClass().getName());
         }
     }
 
diff --git a/jejdb/src/java/org/ejdb/bson/BSONException.java b/jejdb/src/java/org/ejdb/bson/BSONException.java
new file mode 100644 (file)
index 0000000..b3186b1
--- /dev/null
@@ -0,0 +1,23 @@
+package org.ejdb.bson;
+
+/**
+ * @author Tyutyunkov Vyacheslav (tve@softmotions.com)
+ * @version $Id$
+ */
+public class BSONException extends RuntimeException {
+    public BSONException() {
+        super();
+    }
+
+    public BSONException(String message) {
+        super(message);
+    }
+
+    public BSONException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public BSONException(Throwable cause) {
+        super(cause);
+    }
+}
index 065db8a..48af376 100644 (file)
@@ -1,5 +1,6 @@
 package org.ejdb.bson.io;
 
+import org.ejdb.bson.BSONException;
 import org.ejdb.bson.BSONObject;
 
 import java.io.UnsupportedEncodingException;
@@ -71,7 +72,7 @@ public class InputBuffer {
         if (length > 0) {
             ensure(length);
             if ((byte) 0x00 != data[offset + position + length - 1]) {
-                throw new IllegalArgumentException("unexpected end of string");
+                throw new BSONException("unexpected end of string");
             }
 
             try {
@@ -79,7 +80,7 @@ public class InputBuffer {
                 position += length;
                 return s;
             } catch (UnsupportedEncodingException e) {
-                throw new RuntimeException("can not decode string", e);
+                throw new BSONException("can not decode string", e);
             }
         }
 
@@ -89,7 +90,7 @@ public class InputBuffer {
         }
 
         if (position + length > limit) {
-            throw new IllegalArgumentException("unexpected end of string");
+            throw new BSONException("unexpected end of string");
         }
 
         String s = new String(data, offset + position, length);
@@ -112,7 +113,7 @@ public class InputBuffer {
 
     protected void ensure(int size) {
         if (size > limit - position) {
-            throw new IllegalArgumentException("can not allocate sub buffer: not enought bytes");
+            throw new BSONException("can not allocate sub buffer: not enought bytes");
         }
     }
 
index 2387a92..549b8bf 100644 (file)
@@ -1,5 +1,7 @@
 package org.ejdb.bson.io;
 
+import org.ejdb.bson.BSONException;
+
 import java.io.UnsupportedEncodingException;
 
 /**
@@ -92,7 +94,7 @@ public class OutputBuffer {
         try {
             this.write(value.getBytes("UTF-8"));
         } catch (UnsupportedEncodingException e) {
-            throw new RuntimeException(e);
+            throw new BSONException("can not encode string", e);
         }
         this.write((byte) 0x00);