summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/raw/DataEncoder.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/DataEncoder.java
parenta786db8ab53c879858268da6a0cbe4ba9956d160 (diff)
Merge serialization support.
Diffstat (limited to 'src/main/java/com/amazon/carbonado/raw/DataEncoder.java')
-rw-r--r--src/main/java/com/amazon/carbonado/raw/DataEncoder.java44
1 files changed, 41 insertions, 3 deletions
diff --git a/src/main/java/com/amazon/carbonado/raw/DataEncoder.java b/src/main/java/com/amazon/carbonado/raw/DataEncoder.java
index 54846ce..1049644 100644
--- a/src/main/java/com/amazon/carbonado/raw/DataEncoder.java
+++ b/src/main/java/com/amazon/carbonado/raw/DataEncoder.java
@@ -18,6 +18,9 @@
package com.amazon.carbonado.raw;
+import java.io.IOException;
+import java.io.OutputStream;
+
/**
* A very low-level class that supports encoding of primitive data. For
* encoding data into keys, see {@link KeyEncoder}.
@@ -356,7 +359,7 @@ public class DataEncoder {
}
// Write the value length first, in a variable amount of bytes.
- int amt = writeLength(valueLength, dst, dstOffset);
+ int amt = encodeLength(valueLength, dst, dstOffset);
// Now write the value.
System.arraycopy(value, valueOffset, dst, dstOffset + amt, valueLength);
@@ -422,7 +425,7 @@ public class DataEncoder {
int valueLength = value.length();
// Write the value length first, in a variable amount of bytes.
- dstOffset += writeLength(valueLength, dst, dstOffset);
+ dstOffset += encodeLength(valueLength, dst, dstOffset);
for (int i = 0; i < valueLength; i++) {
int c = value.charAt(i);
@@ -501,7 +504,7 @@ public class DataEncoder {
return encodedLen;
}
- private static int writeLength(int valueLength, byte[] dst, int dstOffset) {
+ private static int encodeLength(int valueLength, byte[] dst, int dstOffset) {
if (valueLength < 128) {
dst[dstOffset] = (byte)valueLength;
return 1;
@@ -531,6 +534,41 @@ public class DataEncoder {
}
/**
+ * Writes a positive length value in up to five bytes.
+ *
+ * @return number of bytes written
+ * @since 1.2
+ */
+ public static int writeLength(int valueLength, OutputStream out) throws IOException {
+ if (valueLength < 128) {
+ out.write(valueLength);
+ return 1;
+ } else if (valueLength < 16384) {
+ out.write((valueLength >> 8) | 0x80);
+ out.write(valueLength);
+ return 2;
+ } else if (valueLength < 2097152) {
+ out.write((valueLength >> 16) | 0xc0);
+ out.write(valueLength >> 8);
+ out.write(valueLength);
+ return 3;
+ } else if (valueLength < 268435456) {
+ out.write((valueLength >> 24) | 0xe0);
+ out.write(valueLength >> 16);
+ out.write(valueLength >> 8);
+ out.write(valueLength);
+ return 4;
+ } else {
+ out.write(0xf0);
+ out.write(valueLength >> 24);
+ out.write(valueLength >> 16);
+ out.write(valueLength >> 8);
+ out.write(valueLength);
+ return 5;
+ }
+ }
+
+ /**
* Encodes the given byte array for use when there is only a single
* property, whose type is a byte array. The original byte array is
* returned if the padding lengths are zero.