@Test public void equalIncreasingByteBuffer() { class TestCase { boolean mExpected; ByteBuffer mBuffer; int mLength; int mStart; public TestCase(boolean expected, ByteBuffer buffer, int length, int start) { mExpected = expected; mBuffer = buffer; mLength = length; mStart = start; } } ArrayList<TestCase> testCases = new ArrayList<>(); testCases.add(new TestCase(false, null, 0, 0)); testCases.add(new TestCase(true, ByteBuffer.wrap(new byte[] {}), 0, 0)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {1}), 0, 0)); testCases.add(new TestCase(true, ByteBuffer.wrap(new byte[] {}), 0, 3)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {1}), 0, 3)); testCases.add(new TestCase(true, ByteBuffer.wrap(new byte[] {0}), 1, 0)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {1}), 1, 0)); testCases.add(new TestCase(true, ByteBuffer.wrap(new byte[] {0, 1, 2}), 3, 0)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {0, 1, 2, (byte) 0xFF}), 3, 0)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {1, 2, 3}), 3, 0)); testCases.add(new TestCase(true, ByteBuffer.wrap(new byte[] {3}), 1, 3)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {2}), 1, 3)); testCases.add(new TestCase(true, ByteBuffer.wrap(new byte[] {3, 4, 5}), 3, 3)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {3, 4, 5, (byte) 0xFF}), 3, 3)); testCases.add(new TestCase(false, ByteBuffer.wrap(new byte[] {2, 3, 4}), 3, 3)); for (TestCase testCase : testCases) { boolean result = BufferUtils.equalIncreasingByteBuffer(testCase.mStart, testCase.mLength, testCase.mBuffer); assertEquals(testCase.mExpected, result); } }
public static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf) { if (buf == null) { return false; } buf.rewind(); if (buf.remaining() != len) { return false; } for (int k = 0; k < len; k++) { if (buf.get() != (byte) (start + k)) { return false; } } return true; }
BufferUtils { public static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf) { if (buf == null) { return false; } buf.rewind(); if (buf.remaining() != len) { return false; } for (int k = 0; k < len; k++) { if (buf.get() != (byte) (start + k)) { return false; } } return true; } }
BufferUtils { public static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf) { if (buf == null) { return false; } buf.rewind(); if (buf.remaining() != len) { return false; } for (int k = 0; k < len; k++) { if (buf.get() != (byte) (start + k)) { return false; } } return true; } private  BufferUtils(); }
BufferUtils { public static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf) { if (buf == null) { return false; } buf.rewind(); if (buf.remaining() != len) { return false; } for (int k = 0; k < len; k++) { if (buf.get() != (byte) (start + k)) { return false; } } return true; } 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 boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf) { if (buf == null) { return false; } buf.rewind(); if (buf.remaining() != len) { return false; } for (int k = 0; k < len; k++) { if (buf.get() != (byte) (start + k)) { return false; } } return true; } 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);  }