.../org/apache/hadoop/hive/serde2/ByteStream.java | 87 +++++++++++++--------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java b/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java index 7916a6f..534bc99 100644 --- a/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java +++ b/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java @@ -23,9 +23,6 @@ import org.apache.hadoop.hive.common.io.NonSyncByteArrayInputStream; import org.apache.hadoop.hive.common.io.NonSyncByteArrayOutputStream; -import org.apache.hadoop.hive.serde2.binarysortable.BinarySortableSerDe; -import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; -import org.apache.hadoop.hive.serde2.ByteStream.Output; /** * Extensions to bytearrayinput/output streams. @@ -36,26 +33,27 @@ * */ public static class Input extends NonSyncByteArrayInputStream { - public byte[] getData() { - return buf; + + public Input() { + super(); } - public int getCount() { - return count; + public Input(byte[] buf) { + super(buf); } - public void reset(byte[] argBuf, int argCount) { - buf = argBuf; - mark = pos = 0; - count = argCount; + public byte[] getData() { + return buf; } - public Input() { - super(new byte[1]); + public int getCount() { + return count; } - public Input(byte[] buf) { - super(buf); + public void reset(byte[] buf, int count) { + super.buf = buf; + super.count = count; + super.mark = super.pos = 0; } public Input(byte[] buf, int offset, int length) { @@ -69,10 +67,8 @@ public Input(byte[] buf, int offset, int length) { */ public static final class Output extends NonSyncByteArrayOutputStream implements RandomAccessOutput { - @Override - public byte[] getData() { - return buf; - } + + private static final byte[] RESERVE_INT = { 0x00, 0x00, 0x00, 0x00 }; public Output() { super(); @@ -83,46 +79,67 @@ public Output(int size) { } @Override + public byte[] getData() { + return buf; + } + + @Override public void writeInt(long offset, int value) { - int offset2 = (int)offset; - getData()[offset2++] = (byte) (value >> 24); - getData()[offset2++] = (byte) (value >> 16); - getData()[offset2++] = (byte) (value >> 8); - getData()[offset2] = (byte) (value); + int i = (int) offset; + buf[i + 0] = (byte) (value >> 24); + buf[i + 1] = (byte) (value >> 16); + buf[i + 2] = (byte) (value >> 8); + buf[i + 3] = (byte) (value); } @Override public void writeByte(long offset, byte value) { - getData()[(int) offset] = value; + buf[(int) offset] = value; } + /** + * Optimize for the common cases: + * + */ @Override public void reserve(int byteCount) { - for (int i = 0; i < byteCount; ++i) { + switch (byteCount) { + case 0: + break; + case 1: write(0); + break; + case 4: + write(RESERVE_INT, 0, 4); + break; + default: + for (int i = 0; i < byteCount; ++i) { + write(0); + } } } public boolean arraysEquals(Output output) { - if (count != output.count) { - return false; - } - for (int i = 0; i < count; i++) { - if (buf[i] != output.buf[i]) { - return false; - } - } - return true; + return Arrays.equals(super.buf, output.buf); } } public static interface RandomAccessOutput { public void writeByte(long offset, byte value); + public void writeInt(long offset, int value); + public void reserve(int byteCount); + public void write(int b); + public void write(byte b[]) throws IOException; + public void write(byte b[], int off, int len); + public int getLength(); } }