@Override public int readIntValue(String key) throws Exception { if (in == null) { throw new IllegalStateException("no InputStream provided"); } return IOUtils.readVarInt(in); }
/** * Read a byte array. This will first read the length as 4 bytes, and then * the actual bytes. * * @param in the data input stream * @return the bytes * @throws IOException if an IO exception occurred while reading from the stream. */ public static byte[] readBytes(InputStream in) throws IOException { int len = readVarInt(in); byte[] data = new byte[len]; for (int pos = 0; pos < len;) { int l = in.read(data, pos, data.length - pos); if (l < 0) { throw new EOFException(); } pos += l; } return data; }
/** * Read a byte array. This will first read the length as 4 bytes, and then * the actual bytes. * * @param in the data input stream * @return the bytes * @throws IOException if an IO exception occurred while reading from the stream. */ public static byte[] readBytes(InputStream in) throws IOException { int len = readVarInt(in); byte[] data = new byte[len]; for (int pos = 0; pos < len;) { int l = in.read(data, pos, data.length - pos); if (l < 0) { throw new EOFException(); } pos += l; } return data; }
@Override public StringEntryIterator readStringMap(String key) throws Exception { if (in == null) { throw new IllegalStateException("no InputStream provided"); } final int size = IOUtils.readVarInt(in); return new StringEntryIterator() { int count = size; public boolean hasNext() { return count > 0; } public StringEntry next() { if (count-- > 0) { try { String key = IOUtils.readString(in); String value = IOUtils.readString(in); return new StringEntry(key, value); } catch (IOException e) { throw new RuntimeException("deserialization failed", e); } } throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException(); } }; }
/** * Read a byte array. This will first read the length as 4 bytes, and then * the actual bytes. * * @param in the data input stream * @return the bytes * @throws IOException if an IO exception occurred while reading from the stream. */ public static byte[] readBytes(InputStream in) throws IOException { int len = readVarInt(in); byte[] data = new byte[len]; for (int pos = 0; pos < len;) { int l = in.read(data, pos, data.length - pos); if (l < 0) { throw new EOFException(); } pos += l; } return data; }
throw new IllegalStateException("no InputStream provided"); final int size = IOUtils.readVarInt(in); return new BytesEntryIterator() { int count = size;
int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); totalLength += len; } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); int digestLength = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, digestLength); } else {
int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); totalLength += len; } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); int digestLength = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, digestLength); } else {
streamsStack.pop(); } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); if (level > 0) {
return; } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); BlockId id = new BlockId(digest, 0);
int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); totalLength += len; } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); int digestLength = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, digestLength); } else {
return; } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); BlockId id = new BlockId(digest, 0);
streamsStack.pop(); } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); if (level > 0) {
streamsStack.pop(); } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); if (level > 0) {
return; } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); IOUtils.skipFully(idStream, len); } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); BlockId id = new BlockId(digest, 0);
public void testVarEOF() throws IOException { try { IOUtils.readVarInt(new ByteArrayInputStream(new byte[0])); fail(); } catch (EOFException e) { // expected } try { IOUtils.readVarLong(new ByteArrayInputStream(new byte[0])); fail(); } catch (EOFException e) { // expected } }
return -1; } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); if (pos < len) { IOUtils.skipFully(idStream, (int) pos); pos -= len; } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); if (pos >= totalLength) {
private static void testVarInt(int x, int expectedLen) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.writeVarInt(out, x); byte[] data = out.toByteArray(); assertTrue(data.length <= 5); if (expectedLen > 0) { assertEquals(expectedLen, data.length); } ByteArrayInputStream in = new ByteArrayInputStream(data); int x2 = IOUtils.readVarInt(in); assertEquals(x, x2); assertEquals(-1, in.read()); }
return -1; } else if (type == TYPE_DATA) { int len = IOUtils.readVarInt(idStream); if (pos < len) { IOUtils.skipFully(idStream, (int) pos); pos -= len; } else if (type == TYPE_HASH) { int level = IOUtils.readVarInt(idStream); byte[] digest = new byte[IOUtils.readVarInt(idStream)]; IOUtils.readFully(idStream, digest, 0, digest.length); if (pos >= totalLength) {
public void testVarInt() throws IOException { testVarInt(0, 1); testVarInt(0x7f, 1); testVarInt(0x80, 2); testVarInt(0x3fff, 2); testVarInt(0x4000, 3); testVarInt(0x1fffff, 3); testVarInt(0x200000, 4); testVarInt(0xfffffff, 4); testVarInt(0x10000000, 5); testVarInt(-1, 5); for (int x = 0; x < 0x20000; x++) { testVarInt(x, 0); testVarInt(Integer.MIN_VALUE + x, 0); testVarInt(Integer.MAX_VALUE - x, 5); testVarInt(0x200000 + x - 100, 0); testVarInt(0x10000000 + x - 100, 0); } Random r = new Random(1); for (int i = 0; i < 100000; i++) { testVarInt(r.nextInt(), 0); testVarInt(r.nextInt(10000000), 0); } // trailing 0s are never written, but are an alternative way to encode a value InputStream in = new ByteArrayInputStream(new byte[]{(byte) 0x80, 0}); assertEquals(0, IOUtils.readVarInt(in)); assertEquals(-1, in.read()); }