public int readLength() throws IOException { bitCount = 0; if (runLength > 0) { runLength--; // in run return runValue; } int length = readInt(); if (length >= 0) // not a run return length; runLength = (1-length)>>>1; // start of run runValue = (length+1) & 1; return runValue; }
public byte[] readBytes() throws IOException { byte[] result = new byte[readInt()]; readFully(result); return result; }
public ByteBuffer readBytes(ByteBuffer old) throws IOException { int length = readInt(); ByteBuffer result; if (old != null && length <= old.capacity()) { result = old; result.clear(); } else { result = ByteBuffer.allocate(length); } readFully(result.array(), result.position(), length); result.limit(length); return result; }
public String readString() throws IOException { int length = readInt(); if (length <= (limit - pos)) { // in buffer String result = utf8.decode(ByteBuffer.wrap(buf, pos, length)).toString(); pos += length; return result; } byte[] bytes = new byte[length]; readFully(bytes, 0, length); return utf8.decode(ByteBuffer.wrap(bytes, 0, length)).toString(); }
public void skipBytes() throws IOException { skip(readInt()); }
static void read(InputBuffer in, MetaData<?> metaData) throws IOException { int size = in.readInt(); for (int i = 0; i < size; i++) metaData.put(in.readString(), in.readBytes()); }
public <T extends Comparable> T readValue(ValueType type) throws IOException { switch (type) { case NULL: return (T)null; case BOOLEAN: return (T)Boolean.valueOf(readBoolean()); case INT: return (T)Integer.valueOf(readInt()); case LONG: return (T)Long.valueOf(readLong()); case FIXED32: return (T)Integer.valueOf(readFixed32()); case FIXED64: return (T)Long.valueOf(readFixed64()); case FLOAT: return (T)Float.valueOf(readFloat()); case DOUBLE: return (T)Double.valueOf(readDouble()); case STRING: return (T)readString(); case BYTES: return (T)readBytes(null); default: throw new TrevniRuntimeException("Unknown value type: "+type); } }
public void skipValue(ValueType type) throws IOException { switch (type) { case NULL: break; case BOOLEAN: readBoolean(); break; case INT: readInt(); break; case LONG: readLong(); break; case FIXED32: case FLOAT: skip(4); break; case FIXED64: case DOUBLE: skip(8); break; case STRING: case BYTES: skipBytes(); break; default: throw new TrevniRuntimeException("Unknown value type: "+type); } }
@Test public void testInt() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeInt(random.nextInt()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextInt(), in.readInt()); }
@Test public void testZero() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); out.writeInt(0); byte[] bytes = out.toByteArray(); Assert.assertEquals(1, bytes.length); Assert.assertEquals(0, bytes[0]); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); Assert.assertEquals(0, in.readInt()); }
@Test public void testInitPos() throws Exception { long sentinel = Long.MAX_VALUE; OutputBuffer out = new OutputBuffer(); out.writeValue(Integer.MAX_VALUE, ValueType.INT); out.writeLong(sentinel); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); in.readInt(); long pos = in.tell(); in = new InputBuffer(new InputBytes(out.toByteArray()), pos); Assert.assertEquals(sentinel, in.readLong()); } }
public String readString() throws IOException { int length = readInt(); if (length <= (limit - pos)) { // in buffer String result = new String(buf, pos, length, UTF8); // read directly pos += length; return result; } byte[] bytes = new byte[length]; readFully(bytes, 0, length); return new String(bytes, 0, length, UTF8); }
public byte[] readBytes() throws IOException { byte[] result = new byte[readInt()]; readFully(result); return result; }
public ByteBuffer readBytes(ByteBuffer old) throws IOException { int length = readInt(); ByteBuffer result; if (old != null && length <= old.capacity()) { result = old; result.clear(); } else { result = ByteBuffer.allocate(length); } readFully(result.array(), result.position(), length); result.limit(length); return result; }
public void skipBytes() throws IOException { skip(readInt()); }
/** Expert: Returns the next length in an array column. */ public int nextLength() throws IOException { if (!column.metaData.isArray()) throw new TrevniRuntimeException ("Column is not array: " +column.metaData.getName()); assert arrayLength == 0; return arrayLength = values.readInt(); }
static void read(InputBuffer in, MetaData<?> metaData) throws IOException { int size = in.readInt(); for (int i = 0; i < size; i++) metaData.put(in.readString(), in.readBytes()); }
public void skipValue(ValueType type) throws IOException { switch (type) { case NULL: break; case INT: readInt(); break; case LONG: readLong(); break; case FIXED32: case FLOAT: skip(4); break; case FIXED64: case DOUBLE: skip(8); break; case STRING: case BYTES: skipBytes(); break; default: throw new TrevniRuntimeException("Unknown value type: "+type); } }
@Test public void testInt() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeInt(random.nextInt()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextInt(), in.readInt()); }
@Test public void testZero() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); out.writeInt(0); byte[] bytes = out.toByteArray(); Assert.assertEquals(1, bytes.length); Assert.assertEquals(0, bytes[0]); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); Assert.assertEquals(0, in.readInt()); }