@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 Integer decode(InputStream inStream) throws IOException { return VarInt.decodeInt(inStream); }
@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); }
private static String readString(InputStream dis) throws IOException { int len = VarInt.decodeInt(dis); if (len < 0) { throw new CoderException("Invalid encoded string length: " + len); } byte[] bytes = new byte[len]; ByteStreams.readFully(dis, bytes); return new String(bytes, StandardCharsets.UTF_8); }
private static int decodeInt(byte[] encoded) throws IOException { ByteArrayInputStream stream = new ByteArrayInputStream(encoded); return VarInt.decodeInt(stream); }
@Override public byte[] decode(InputStream inStream, Context context) throws IOException, CoderException { if (context.isWholeStream) { return StreamUtils.getBytesWithoutClosing(inStream); } else { int length = VarInt.decodeInt(inStream); if (length < 0) { throw new IOException("invalid length " + length); } byte[] value = new byte[length]; ByteStreams.readFully(inStream, value); return value; } }
@Override public T decode(InputStream inStream) throws IOException { DataInputStream in = new DataInputStream(inStream); int len = VarInt.decodeInt(in); if (len < 0) { throw new CoderException("Invalid encoded SolrDocument length: " + len); } JavaBinCodec codec = new JavaBinCodec(); return (T) codec.unmarshal(new BoundedInputStream(in, len)); }
@Override public ByteString decode(InputStream inStream, Context context) throws IOException { if (context.isWholeStream) { return ByteString.readFrom(inStream); } int size = VarInt.decodeInt(inStream); // ByteString reads to the end of the input stream, so give it a limited stream of exactly // the right length. Also set its chunk size so that the ByteString will contain exactly // one chunk. return ByteString.readFrom(ByteStreams.limit(inStream, size), size); }
@Override public Message decode(InputStream inStream) throws CoderException, IOException { Message message = Message.Factory.create(); int bytesToRead = VarInt.decodeInt(inStream); byte[] encodedMessage = new byte[bytesToRead]; ByteStreams.readFully(inStream, encodedMessage); message.decode(encodedMessage, 0, encodedMessage.length); return message; } }
@Override public TestCheckpointMark decode(InputStream inStream) throws IOException { TestCheckpointMark decoded = new TestCheckpointMark(VarInt.decodeInt(inStream)); decoded.decoded = true; return decoded; } }
@Override public RandomAccessData decode(InputStream inStream, Coder.Context context) throws CoderException, IOException { RandomAccessData rval = new RandomAccessData(); if (!context.isWholeStream) { int length = VarInt.decodeInt(inStream); rval.readFrom(inStream, 0, length); } else { ByteStreams.copy(inStream, rval.asOutputStream()); } return rval; }
@Test public void endOfFileThrowsException() throws Exception { ByteArrayInputStream inStream = new ByteArrayInputStream(new byte[0]); thrown.expect(EOFException.class); VarInt.decodeInt(inStream); }
@Test public void unterminatedThrowsException() throws Exception { byte[] e = encodeLong(Long.MAX_VALUE); byte[] s = new byte[1]; s[0] = e[0]; ByteArrayInputStream inStream = new ByteArrayInputStream(s); thrown.expect(IOException.class); VarInt.decodeInt(inStream); } }
@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()); } }
@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)); } }