private void startBlock(int block) throws IOException { this.block = block; this.row = column.firstRows[block]; in.seek(column.blockStarts[block]); int end = column.blocks[block].compressedSize; byte[] raw = new byte[end+checksum.size()]; in.readFully(raw); ByteBuffer data = codec.decompress(ByteBuffer.wrap(raw, 0, end)); if (!checksum.compute(data).equals (ByteBuffer.wrap(raw, end, checksum.size()))) throw new IOException("Checksums mismatch."); values = new InputBuffer(new InputBytes(data)); }
public InputBuffer(Input in, long position) throws IOException { this.in = in; this.inLength = in.length(); this.offset = position; if (in instanceof InputBytes) { // use buffer directly this.buf = ((InputBytes)in).getBuffer(); this.limit = (int)in.length(); this.offset = limit; this.pos = (int)position; } else { // create new buffer this.buf = new byte[8192]; // big enough for primitives } }
@Override public synchronized int read(long pos, byte[] b, int start, int len) throws IOException { this.pos = (int)pos; return read(b, start, len); }
@Test public void testRandomReads() throws Exception { Random random = new Random(19820210); int length = random.nextInt(SIZE) + 1; byte[] data = new byte[length]; random.nextBytes(data); Input in = new InputBytes(data); for (int i = 0; i < COUNT; i++) { int p = random.nextInt(length); int l = Math.min(random.nextInt(SIZE/10), length-p); byte[] buffer = new byte[l]; in.read(p, buffer, 0, l); Assert.assertArrayEquals(Arrays.copyOfRange(data, p, p+l), buffer); } } }
@Override public synchronized int read(long pos, byte[] b, int start, int len) throws IOException { this.pos = (int)pos; return read(b, start, len); }
public InputBuffer(Input in, long position) throws IOException { this.in = in; this.inLength = in.length(); this.offset = position; if (in instanceof InputBytes) { // use buffer directly this.buf = ((InputBytes)in).getBuffer(); this.limit = (int)in.length(); this.offset = limit; } else { // create new buffer this.buf = new byte[8192]; // big enough for primitives } }
@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()); } }
@Test public void testLong() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeLong(random.nextLong()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextLong(), in.readLong()); }
@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 testFixed64() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeFixed64(random.nextLong()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextLong(), in.readFixed64()); }
@Test public void testFloat() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeFloat(random.nextFloat()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextFloat(), in.readFloat(), 0); }
@Test public void testBoolean() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeValue(random.nextBoolean(), ValueType.BOOLEAN); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextBoolean(), in.readValue(ValueType.BOOLEAN)); }
@Test public void testFixed32() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeFixed32(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.readFixed32()); }
@Test public void testEmpty() throws Exception { OutputBuffer out = new OutputBuffer(); ByteArrayOutputStream temp = new ByteArrayOutputStream(); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); Assert.assertEquals(0, in.tell()); Assert.assertEquals(0, in.length()); }
@Test public void testDouble() throws Exception { OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeDouble(Double.MIN_VALUE); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); for (int i = 0; i < COUNT; i++) Assert.assertEquals(Double.MIN_VALUE, in.readDouble(), 0); }
@Test public void testSkipBytes() throws Exception { long sentinel = Long.MAX_VALUE; OutputBuffer out = new OutputBuffer(); out.writeValue("trevni".getBytes(), ValueType.BYTES); out.writeLong(sentinel); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); in.skipValue(ValueType.BYTES); Assert.assertEquals(sentinel, in.readLong()); } @Test public void testInitPos() throws Exception {
@Test public void testSkipBoolean() throws Exception { long sentinel = Long.MAX_VALUE; OutputBuffer out = new OutputBuffer(); out.writeValue(false, ValueType.BOOLEAN); out.writeLong(sentinel); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); in.skipValue(ValueType.BOOLEAN); Assert.assertEquals(sentinel, in.readLong()); } @Test public void testSkipInt() throws Exception {
@Test public void testSkipDouble() throws Exception { long sentinel = Long.MAX_VALUE; OutputBuffer out = new OutputBuffer(); out.writeValue(Double.MAX_VALUE, ValueType.DOUBLE); out.writeLong(sentinel); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); in.skipValue(ValueType.DOUBLE); Assert.assertEquals(sentinel, in.readLong()); } @Test public void testSkipString() throws Exception {
@Test public void testSkipLong() throws Exception { long sentinel = Long.MAX_VALUE; OutputBuffer out = new OutputBuffer(); out.writeValue(Long.MAX_VALUE, ValueType.LONG); out.writeLong(sentinel); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); in.skipValue(ValueType.LONG); Assert.assertEquals(sentinel, in.readLong()); } @Test public void testSkipFixed32() throws Exception {
@Test public void testSkipFixed64() throws Exception { long sentinel = Long.MAX_VALUE; OutputBuffer out = new OutputBuffer(); out.writeValue(Long.MAX_VALUE, ValueType.FIXED64); out.writeLong(sentinel); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); in.skipValue(ValueType.LONG); Assert.assertEquals(sentinel, in.readLong()); } @Test public void testSkipFloat() throws Exception {