summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/raw/DataDecoder.java
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2008-02-16 00:13:18 +0000
committerBrian S. O'Neill <bronee@gmail.com>2008-02-16 00:13:18 +0000
commit4226a7812c0bab11703bb5bb4c514e6618d0d8db (patch)
treeb7364eae1576e7e1d4c399b6c9e3a37eba6309dc /src/main/java/com/amazon/carbonado/raw/DataDecoder.java
parenta786db8ab53c879858268da6a0cbe4ba9956d160 (diff)
Merge serialization support.
Diffstat (limited to 'src/main/java/com/amazon/carbonado/raw/DataDecoder.java')
-rw-r--r--src/main/java/com/amazon/carbonado/raw/DataDecoder.java67
1 files changed, 66 insertions, 1 deletions
diff --git a/src/main/java/com/amazon/carbonado/raw/DataDecoder.java b/src/main/java/com/amazon/carbonado/raw/DataDecoder.java
index 485ce82..bffa7c2 100644
--- a/src/main/java/com/amazon/carbonado/raw/DataDecoder.java
+++ b/src/main/java/com/amazon/carbonado/raw/DataDecoder.java
@@ -18,6 +18,10 @@
package com.amazon.carbonado.raw;
+import java.io.EOFException;
+import java.io.InputStream;
+import java.io.IOException;
+
import com.amazon.carbonado.CorruptEncodingException;
import static com.amazon.carbonado.raw.DataEncoder.*;
@@ -399,7 +403,8 @@ public class DataDecoder {
valueLength = ((b & 0x0f) << 24) | ((src[srcOffset++] & 0xff) << 16) |
((src[srcOffset++] & 0xff) << 8) | (src[srcOffset++] & 0xff);
} else {
- valueLength = ((b & 0x07) << 24) | ((src[srcOffset++] & 0xff) << 16) |
+ valueLength = ((src[srcOffset++] & 0xff) << 24) |
+ ((src[srcOffset++] & 0xff) << 16) |
((src[srcOffset++] & 0xff) << 8) | (src[srcOffset++] & 0xff);
}
@@ -505,6 +510,66 @@ public class DataDecoder {
}
/**
+ * Decodes a length value which was encoded by {@link DataDecoder#encodeLength}.
+ *
+ * @return length value
+ * @since 1.2
+ */
+ public static int readLength(InputStream in) throws IOException, EOFException {
+ int b0 = in.read();
+ if (b0 < 0) {
+ throw new EOFException();
+ }
+ if (b0 <= 0x7f) {
+ return b0;
+ }
+ int b1 = in.read();
+ if (b1 < 0) {
+ throw new EOFException();
+ }
+ if (b0 <= 0xbf) {
+ return ((b0 & 0x3f) << 8) | b1;
+ }
+ int b2 = in.read();
+ if (b2 < 0) {
+ throw new EOFException();
+ }
+ if (b0 <= 0xdf) {
+ return ((b0 & 0x1f) << 16) | (b1 << 8) | b2;
+ }
+ int b3 = in.read();
+ if (b3 < 0) {
+ throw new EOFException();
+ }
+ if (b0 <= 0xef) {
+ return ((b0 & 0x0f) << 24) | (b1 << 16) | (b2 << 8) | b3;
+ }
+ int b4 = in.read();
+ if (b4 < 0) {
+ throw new EOFException();
+ }
+ return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
+ }
+
+ /**
+ * Reads as many bytes from the stream as is necessary to fill the given
+ * byte array. An EOFException is thrown if the stream end is encountered.
+ *
+ * @since 1.2
+ */
+ public static void readFully(InputStream in, byte[] b) throws IOException, EOFException {
+ final int length = b.length;
+ int total = 0;
+ while (total < length) {
+ int amt = in.read(b, total, length - total);
+ if (amt < 0) {
+ throw new EOFException();
+ }
+ total += amt;
+ }
+ }
+
+ /**
* Decodes the given byte array which was encoded by {@link
* DataEncoder#encodeSingle}.
*