Java ByteBuffer Search search(ByteBuffer buffer, int n, byte[] param)

Here you can find the source of search(ByteBuffer buffer, int n, byte[] param)

Description

search

License

BSD License

Declaration

public static ByteBuffer search(ByteBuffer buffer, int n, byte[] param) 

Method Source Code

//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;
    }
}

Related

  1. indexOf(ByteBuffer buffer, byte b)
  2. indexOf(ByteBuffer buffer, ByteBuffer pattern)
  3. indexOf(ByteBuffer buffer, ByteBuffer pattern)
  4. indexOf(final ByteBuffer buf, final int c, final int start, final int end)
  5. indexOf(final ByteBuffer haystack, byte[] needle)
  6. unsignedBinarySearch(final ByteBuffer array, int position, final int begin, final int end, final short k)