Here you can find the source of indexOf(ByteBuffer buffer, ByteBuffer pattern)
public static final int indexOf(ByteBuffer buffer, ByteBuffer pattern)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; public class Main { public static final int indexOf(ByteBuffer buffer, ByteBuffer pattern) { if (pattern == null || buffer == null) { return -1; }// w w w.j a v a 2 s . c o m int n = buffer.remaining(); int m = pattern.remaining(); int patternPos = pattern.position(); int bufferPos = buffer.position(); if (n < m) { return -1; } for (int s = 0; s <= n - m; s++) { boolean match = true; for (int i = 0; i < m; i++) { if (buffer.get(s + i + bufferPos) != pattern.get(patternPos + i)) { match = false; break; } } if (match) { return bufferPos + s; } } return -1; } public static final int indexOf(ByteBuffer buffer, ByteBuffer pattern, int offset) { if (offset < 0) { throw new IllegalArgumentException("offset must be greater than 0"); } if (pattern == null || buffer == null) { return -1; } int patternPos = pattern.position(); int n = buffer.remaining(); int m = pattern.remaining(); if (n < m) { return -1; } if (offset < buffer.position() || offset > buffer.limit()) { return -1; } for (int s = 0; s <= n - m; s++) { boolean match = true; for (int i = 0; i < m; i++) { if (buffer.get(s + i + offset) != pattern.get(patternPos + i)) { match = false; break; } } if (match) { return offset + s; } } return -1; } public static final int remaining(ByteBuffer[] buffers) { if (buffers == null) { return 0; } int remaining = 0; for (int i = 0; i < buffers.length; i++) { if (buffers[i] != null) { remaining += buffers[i].remaining(); } } return remaining; } }