/** * Test copying to stream from buffer. */ @Test public void testMoveBufferToStream() { final int arrayOffset = 7; final int initialPosition = 10; final int endPadding = 5; byte[] arrayWrapper = new byte[arrayOffset + initialPosition + array.length + endPadding]; System.arraycopy(array, 0, arrayWrapper, arrayOffset + initialPosition, array.length); ByteBuffer buffer = ByteBuffer.wrap(arrayWrapper, arrayOffset, initialPosition + array.length).slice(); assertEquals(initialPosition + array.length, buffer.limit()); assertEquals(0, buffer.position()); buffer.position(initialPosition); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ByteBufferUtils.moveBufferToStream(bos, buffer, array.length); } catch (IOException e) { fail("IOException in testCopyToStream()"); } assertArrayEquals(array, bos.toByteArray()); assertEquals(initialPosition + array.length, buffer.position()); }
@Override public void compressKeyValues(DataOutputStream out, ByteBuffer in, boolean includesMemstoreTS) throws IOException { in.rewind(); ByteBufferUtils.putInt(out, in.limit()); ByteBufferUtils.moveBufferToStream(out, in, in.limit()); }
private int addKV(int prevKeyOffset, DataOutputStream out, ByteBuffer in, int prevKeyLength) throws IOException { int keyLength = in.getInt(); int valueLength = in.getInt(); if (prevKeyOffset == -1) { // copy the key, there is no common prefix with none ByteBufferUtils.putCompressedInt(out, keyLength); ByteBufferUtils.putCompressedInt(out, valueLength); ByteBufferUtils.putCompressedInt(out, 0); ByteBufferUtils.moveBufferToStream(out, in, keyLength + valueLength); } else { // find a common prefix and skip it int common = ByteBufferUtils.findCommonPrefix( in, prevKeyOffset + KeyValue.ROW_OFFSET, in.position(), Math.min(prevKeyLength, keyLength)); ByteBufferUtils.putCompressedInt(out, keyLength - common); ByteBufferUtils.putCompressedInt(out, valueLength); ByteBufferUtils.putCompressedInt(out, common); ByteBufferUtils.skip(in, common); ByteBufferUtils.moveBufferToStream(out, in, keyLength - common + valueLength); } return keyLength; }
ByteBufferUtils.moveBufferToStream(out, in, keyLength + valueLength); } else { ByteBufferUtils.moveBufferToStream(out, in, currentState.rowLength + KeyValue.ROW_LENGTH_SIZE - commonPrefix); ByteBufferUtils.skip(in, currentState.familyLength + KeyValue.FAMILY_LENGTH_SIZE); ByteBufferUtils.moveBufferToStream(out, in, currentState.qualifierLength); } else { ByteBufferUtils.moveBufferToStream(out, in, restKeyLength); ByteBufferUtils.moveBufferToStream(out, in, KeyValue.TIMESTAMP_SIZE - commonTimestampPrefix);
ByteBufferUtils.skip(in, currentState.rowLength + KeyValue.ROW_LENGTH_SIZE); ByteBufferUtils.moveBufferToStream(out, in, currentState.familyLength + KeyValue.FAMILY_LENGTH_SIZE); in.reset(); int restRowLength = currentState.rowLength + KeyValue.ROW_LENGTH_SIZE - commonPrefix; ByteBufferUtils.moveBufferToStream(out, in, restRowLength); ByteBufferUtils.skip(in, currentState.familyLength + KeyValue.FAMILY_LENGTH_SIZE); ByteBufferUtils.moveBufferToStream(out, in, currentState.qualifierLength); } else { ByteBufferUtils.moveBufferToStream(out, in, keyLength - commonPrefix - KeyValue.TIMESTAMP_TYPE_SIZE); ByteBufferUtils.moveBufferToStream(out, in, valueLength);
/** * Test copying to stream from buffer. */ @Test public void testMoveBufferToStream() { final int arrayOffset = 7; final int initialPosition = 10; final int endPadding = 5; byte[] arrayWrapper = new byte[arrayOffset + initialPosition + array.length + endPadding]; System.arraycopy(array, 0, arrayWrapper, arrayOffset + initialPosition, array.length); ByteBuffer buffer = ByteBuffer.wrap(arrayWrapper, arrayOffset, initialPosition + array.length).slice(); assertEquals(initialPosition + array.length, buffer.limit()); assertEquals(0, buffer.position()); buffer.position(initialPosition); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ByteBufferUtils.moveBufferToStream(bos, buffer, array.length); } catch (IOException e) { fail("IOException in testCopyToStream()"); } assertArrayEquals(array, bos.toByteArray()); assertEquals(initialPosition + array.length, buffer.position()); }
/** * Test copying to stream from buffer. */ @Test public void testMoveBufferToStream() { final int arrayOffset = 7; final int initialPosition = 10; final int endPadding = 5; byte[] arrayWrapper = new byte[arrayOffset + initialPosition + array.length + endPadding]; System.arraycopy(array, 0, arrayWrapper, arrayOffset + initialPosition, array.length); ByteBuffer buffer = ByteBuffer.wrap(arrayWrapper, arrayOffset, initialPosition + array.length).slice(); assertEquals(initialPosition + array.length, buffer.limit()); assertEquals(0, buffer.position()); buffer.position(initialPosition); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ByteBufferUtils.moveBufferToStream(bos, buffer, array.length); } catch (IOException e) { fail("IOException in testCopyToStream()"); } assertArrayEquals(array, bos.toByteArray()); assertEquals(initialPosition + array.length, buffer.position()); }