@Override public void encode(long[] value, OutputStream outStream) throws IOException { VarInt.encode(value[0], outStream); }
/** Decodes an integer value from the given stream. */ public static int decodeInt(InputStream stream) throws IOException { long r = decodeLong(stream); if (r < 0 || r >= 1L << 32) { throw new IOException("varint overflow " + r); } return (int) r; }
/** Returns the length of the encoding of the given value (in bytes). */ public static int getLength(int v) { return getLength(convertIntToLongNoSignExtend(v)); }
/** Encodes the given value onto the stream. */ public static void encode(int v, OutputStream stream) throws IOException { encode(convertIntToLongNoSignExtend(v), stream); }
@Test public void decodeParsesEncodedValues() throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); for (int i = 10; i < Integer.MAX_VALUE; i = (int) (i * 1.1)) { VarInt.encode(i, outStream); VarInt.encode(-i, outStream); } for (long i = 10; i < Long.MAX_VALUE; i = (long) (i * 1.1)) { VarInt.encode(i, outStream); VarInt.encode(-i, outStream); } ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray()); for (int i = 10; i < Integer.MAX_VALUE; i = (int) (i * 1.1)) { assertEquals(i, VarInt.decodeInt(inStream)); assertEquals(-i, VarInt.decodeInt(inStream)); } for (long i = 10; i < Long.MAX_VALUE; i = (long) (i * 1.1)) { assertEquals(i, VarInt.decodeLong(inStream)); assertEquals(-i, VarInt.decodeLong(inStream)); } }
@Override protected long getEncodedElementByteSize(long[] value) { return VarInt.getLength(value[0]); } };
@Test public void decodeValues() throws IOException { assertEquals(LONG_VALUES.length, LONG_ENCODED.length); for (int i = 0; i < LONG_ENCODED.length; ++i) { ByteArrayInputStream stream = new ByteArrayInputStream(LONG_ENCODED[i]); long parsed = VarInt.decodeLong(stream); assertEquals(LONG_VALUES[i], parsed); assertEquals(-1, stream.read()); } assertEquals(INT_VALUES.length, INT_ENCODED.length); for (int i = 0; i < INT_ENCODED.length; ++i) { ByteArrayInputStream stream = new ByteArrayInputStream(INT_ENCODED[i]); int parsed = VarInt.decodeInt(stream); assertEquals(INT_VALUES[i], parsed); assertEquals(-1, stream.read()); } }
@Override protected long getEncodedElementByteSize(byte[] value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null byte[]"); } return (long) VarInt.getLength(value.length) + value.length; } }
private static void writeString(String value, OutputStream dos) throws IOException { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); VarInt.encode(bytes.length, dos); dos.write(bytes); }
@Override public Integer decode(InputStream inStream) throws IOException, CoderException { try { return VarInt.decodeInt(inStream); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
@Override public Long decode(InputStream inStream) throws IOException, CoderException { try { return VarInt.decodeLong(inStream); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
@Override protected long getEncodedElementByteSize(Integer value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null Integer"); } return VarInt.getLength(value.longValue()); } }
private void outputBuffer() throws IOException { if (count > 0) { VarInt.encode(count, os); // We are using a heap based buffer and not a direct buffer so it is safe to access // the underlying array. os.write(buffer.array(), buffer.arrayOffset(), buffer.position()); buffer.clear(); // The buffer has been flushed so we must write to the underlying stream until // we learn of the next element. We reset the count to zero marking that we should // not use the buffer. count = 0; } } }
@Override public Integer decode(InputStream inStream) throws IOException { return VarInt.decodeInt(inStream); }
@Override public long[] decode(InputStream inStream) throws IOException, CoderException { try { return new long[] {VarInt.decodeLong(inStream)}; } catch (EOFException | UTFDataFormatException exn) { throw new CoderException(exn); } }
@Override protected long getEncodedElementByteSize(RandomAccessData value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null in memory stream"); } return (long) VarInt.getLength(value.size) + value.size; } }
@Override public void encode(Integer value, OutputStream outStream) throws IOException { VarInt.encode(value, outStream); } }
@Override public RawUnionValue decode(InputStream inStream, Context context) throws IOException, CoderException { int index = VarInt.decodeInt(inStream); Object value = elementCoders.get(index).decode(inStream, context); return new RawUnionValue(index, value); }
@Override public MultiStepAccumulator decode(InputStream inStream) throws CoderException, IOException { return MultiStepAccumulator.of(VarInt.decodeLong(inStream), true); } }