Here you can find the source of search(ByteBuffer buffer, int n, byte[] param)
public static ByteBuffer search(ByteBuffer buffer, int n, byte[] param)
//package com.java2s; /**// ww w. j a va 2s. co m * This class is part of JCodec ( www.jcodec.org ) This software is distributed * under FreeBSD License * * @author The JCodec project * */ import java.nio.ByteBuffer; public class Main { public static ByteBuffer search(ByteBuffer buffer, int n, byte[] param) { ByteBuffer result = buffer.duplicate(); int step = 0, rem = buffer.position(); while (buffer.hasRemaining()) { int b = buffer.get(); if (b == param[step]) { ++step; if (step == param.length) { if (n == 0) { buffer.position(rem); result.limit(buffer.position()); break; } n--; step = 0; } } else { if (step != 0) { step = 0; ++rem; buffer.position(rem); } else rem = buffer.position(); } } return result; } public static ByteBuffer duplicate(ByteBuffer bb) { ByteBuffer out = ByteBuffer.allocate(bb.remaining()); out.put(bb.duplicate()); out.flip(); return out; } }