Here you can find the source of byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)
public static int byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)
//package com.java2s; /***// ww w. j av a 2 s . c om * The content of this file or document is CONFIDENTIAL and PROPRIETARY * to ChengZhen(anyou@msn.com). It is subject to the terms of a * License Agreement between Licensee and ChengZhen(anyou@msn.com). * restricting among other things, the use, reproduction, distribution * and transfer. Each of the embodiments, including this information and * any derivative work shall retain this copyright notice. * * Copyright (c) 2005-2014 ChengZhen(anyou@msn.com) All Rights Reserved. * */ import java.nio.ByteBuffer; public class Main { public static int byteIndexOf(ByteBuffer buffer, byte[] temp, int offset) { int i; if (temp.length == 0) { return 0; } int end = buffer.limit() - temp.length; if (end < 0) { return -1; } int start = buffer.position() + offset; if (start > end) { return -1; } if (start < 0) { start = 0; } byte[] b = buffer.array(); Search: for (i = start; i < end; i++) { if (b[i] != temp[0]) { continue; } int k = 1; while (k < temp.length) { if (b[k + i] != temp[k]) { continue Search; } k++; } return i; } return -1; } }