ByteBufferUtils { public static boolean containsPrefix(final ByteBuffer buffer, final ByteBuffer prefixBuffer) { boolean result = true; if (buffer.remaining() < prefixBuffer.remaining()) { result = false; } else { for (int i = 0; i < prefixBuffer.remaining(); i++) { if (prefixBuffer.get(i) != buffer.get(i)) { result = false; break; } } } boolean result2 = result; LOGGER.trace(() -> LogUtil.message("containsPrefix({} {}) returns {}", ByteBufferUtils.byteBufferInfo(buffer), ByteBufferUtils.byteBufferInfo(prefixBuffer), result2)); return result; } private  ByteBufferUtils(); static String byteBufferToString(final ByteBuffer byteBuffer); static String byteBufferToHex(final ByteBuffer byteBuffer); static String byteBufferToHexAll(final ByteBuffer byteBuffer); static String byteBufferInfo(final ByteBuffer byteBuffer); static String byteBufferToAllForms(final ByteBuffer byteBuffer); static int compare(final ByteBuffer left, final ByteBuffer right); static int compareAsLong(final ByteBuffer left, final ByteBuffer right); static int compareAsLong(final ByteBuffer left, final int leftPos,
                                   final ByteBuffer right, final int rightPos); static int compareAsInt(final ByteBuffer left, final ByteBuffer right); static int compareAsInt(final ByteBuffer left, final int leftPos,
                                   final ByteBuffer right, final int rightPos); static boolean containsPrefix(final ByteBuffer buffer, final ByteBuffer prefixBuffer); static void copy(final ByteBuffer sourceBuffer, final ByteBuffer destBuffer); static ByteBuffer copyToDirectBuffer(final ByteBuffer input); static long xxHash(final ByteBuffer byteBuffer); static int basicHashCode(final ByteBuffer byteBuffer);  }
@Test void testContainsPrefix_match() { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5}); ByteBuffer prefixByteBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4}); boolean result = ByteBufferUtils.containsPrefix(byteBuffer, prefixByteBuffer); assertThat(result).isTrue(); }
@Test void testContainsPrefix_match2() { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5}); ByteBuffer prefixByteBuffer = ByteBuffer.wrap(new byte[]{0}); boolean result = ByteBufferUtils.containsPrefix(byteBuffer, prefixByteBuffer); assertThat(result).isTrue(); }
@Test void testContainsPrefix_bufferTooShort() { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2}); ByteBuffer prefixByteBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4}); boolean result = ByteBufferUtils.containsPrefix(byteBuffer, prefixByteBuffer); assertThat(result).isFalse(); }
@Test void testContainsPrefix_noMatch() { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5}); ByteBuffer prefixByteBuffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4}); boolean result = ByteBufferUtils.containsPrefix(byteBuffer, prefixByteBuffer); assertThat(result).isFalse(); }