private void beginComplex() { if (index > 0) { output.write(separators[currentLevel]); } indexStack.push(index); // Always use index 0 so the write methods don't write a separator. index = 0; // Set "global" separator member to next level. currentLevel++; }
@Override public void writeDouble(double v) throws IOException { beginPrimitive(); ByteBuffer b = Text.encode(String.valueOf(v)); output.write(b.array(), 0, b.limit()); finishPrimitive(); }
@Override public void writeFloat(float vf) throws IOException { beginPrimitive(); ByteBuffer b = Text.encode(String.valueOf(vf)); output.write(b.array(), 0, b.limit()); finishPrimitive(); }
private void writeVLong(long v) { final int len = LazyBinaryUtils.writeVLongToByteArray(vLongBytes, v); output.write(vLongBytes, 0, len); }
private void writeVInt(int v) { final int len = LazyBinaryUtils.writeVLongToByteArray(vLongBytes, v); output.write(vLongBytes, 0, len); }
@Override public void writeBinary(byte[] v, int start, int length) throws IOException { beginPrimitive(); byte[] toEncode = new byte[length]; System.arraycopy(v, start, toEncode, 0, length); byte[] toWrite = Base64.encodeBase64(toEncode); output.write(toWrite, 0, toWrite.length); finishPrimitive(); }
@Override public void writeString(byte[] v) throws IOException { beginElement(); final int length = v.length; writeVInt(length); output.write(v, 0, length); finishElement(); }
@Override public void writeBoolean(boolean v) throws IOException { beginElement(); output.write((byte) (v ? 1 : 0)); finishElement(); }
@Override public byte[] serializeKey(Object object, StructField field) throws IOException { ObjectInspector inspector = field.getFieldObjectInspector(); if (inspector.getCategory() != ObjectInspector.Category.STRUCT) { throw new IllegalStateException("invalid type value " + inspector.getTypeName()); } output.reset(); for (Object element : ((StructObjectInspector)inspector).getStructFieldsDataAsList(object)) { output.write(toBinary(String.valueOf(element).getBytes(), FIXED_LENGTH, false, false)); } return output.getLength() > 0 ? output.toByteArray() : null; }
@Override public void writeString(byte[] v, int start, int length) throws IOException { beginElement(); writeVInt(length); output.write(v, start, length); finishElement(); }
@Override public void writeByte(byte v) throws IOException { beginElement(); output.write(v); finishElement(); }
@Override public void beginUnion(int tag) throws IOException { beginComplex(); writeInt(tag); output.write(separators[currentLevel]); index = 0; }
@Override public void writeNull() throws IOException { beginPrimitive(); output.write(nullSequenceBytes); finishPrimitive(); }
@Override public void separateKeyValuePair() { index = 0; output.write(separators[currentLevel - 1]); }
@Override public void separateKey() { index = 0; output.write(separators[currentLevel]); }
@Override public void writeBinary(byte[] v) throws IOException { beginPrimitive(); byte[] toEncode = new byte[v.length]; System.arraycopy(v, 0, toEncode, 0, v.length); byte[] toWrite = Base64.encodeBase64(toEncode); output.write(toWrite, 0, toWrite.length); finishPrimitive(); }
private void beginPrimitive() { if (index > 0) { output.write(separators[currentLevel]); } }