@Test public void sliceByteBuffer() { final int size = 100; final ByteBuffer buf = BufferUtils.getIncreasingByteBuffer(size); for (int slicePosition : new int[] {0, 1, size / 2, size - 1}) { ByteBuffer slicedBuffer = BufferUtils.sliceByteBuffer(buf, slicePosition, 1); assertEquals(0, slicedBuffer.position()); assertEquals(1, slicedBuffer.limit()); assertTrue(BufferUtils.equalIncreasingByteBuffer(slicePosition, 1, slicedBuffer)); int slicedBufferLength = size - slicePosition; ByteBuffer slicedBuffer1 = BufferUtils.sliceByteBuffer(buf, slicePosition, slicedBufferLength); ByteBuffer slicedBuffer2 = BufferUtils.sliceByteBuffer(buf, slicePosition); assertEquals(0, slicedBuffer1.position()); assertEquals(0, slicedBuffer2.position()); assertEquals(slicedBufferLength, slicedBuffer1.limit()); assertEquals(slicedBufferLength, slicedBuffer2.limit()); assertTrue(BufferUtils.equalIncreasingByteBuffer(slicePosition, slicedBufferLength, slicedBuffer1)); assertTrue(BufferUtils.equalIncreasingByteBuffer(slicePosition, slicedBufferLength, slicedBuffer2)); } }
public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) { ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate().position(position)).slice(); slicedBuffer.limit(length); return slicedBuffer; }
BufferUtils { public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) { ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate().position(position)).slice(); slicedBuffer.limit(length); return slicedBuffer; } }
BufferUtils { public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) { ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate().position(position)).slice(); slicedBuffer.limit(length); return slicedBuffer; } private  BufferUtils(); }
BufferUtils { public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) { ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate().position(position)).slice(); slicedBuffer.limit(length); return slicedBuffer; } private  BufferUtils(); static int byteToInt(byte b); static synchronized void cleanDirectBuffer(ByteBuffer buffer); static ByteBuffer cloneByteBuffer(ByteBuffer buf); static List<ByteBuffer> cloneByteBufferList(List<ByteBuffer> source); static void putIntByteBuffer(ByteBuffer buf, int b); static byte[] getIncreasingByteArray(int len); static byte[] getIncreasingByteArray(int start, int len); static boolean equalConstantByteArray(byte value, int len, byte[] arr); static boolean equalIncreasingByteArray(int len, byte[] arr); static boolean equalIncreasingByteArray(int start, int len, byte[] arr); static ByteBuffer getIncreasingByteBuffer(int len); static ByteBuffer getIncreasingByteBuffer(int start, int len); static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf); static void writeBufferToFile(String path, byte[] buffer); static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest); static int intAsUnsignedByteValue(int i); static byte[] newByteArrayFromByteBuffer(ByteBuffer buf); static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length); static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position); }
BufferUtils { public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) { ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate().position(position)).slice(); slicedBuffer.limit(length); return slicedBuffer; } private  BufferUtils(); static int byteToInt(byte b); static synchronized void cleanDirectBuffer(ByteBuffer buffer); static ByteBuffer cloneByteBuffer(ByteBuffer buf); static List<ByteBuffer> cloneByteBufferList(List<ByteBuffer> source); static void putIntByteBuffer(ByteBuffer buf, int b); static byte[] getIncreasingByteArray(int len); static byte[] getIncreasingByteArray(int start, int len); static boolean equalConstantByteArray(byte value, int len, byte[] arr); static boolean equalIncreasingByteArray(int len, byte[] arr); static boolean equalIncreasingByteArray(int start, int len, byte[] arr); static ByteBuffer getIncreasingByteBuffer(int len); static ByteBuffer getIncreasingByteBuffer(int start, int len); static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf); static void writeBufferToFile(String path, byte[] buffer); static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest); static int intAsUnsignedByteValue(int i); static byte[] newByteArrayFromByteBuffer(ByteBuffer buf); static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length); static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position);  }