Java ByteBuffer Search indexOf(ByteBuffer buffer, ByteBuffer pattern)

Here you can find the source of indexOf(ByteBuffer buffer, ByteBuffer pattern)

Description

index Of

License

Open Source License

Declaration

public static final int indexOf(ByteBuffer buffer, ByteBuffer pattern) 

Method Source Code


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

Related

  1. binarySearch(ByteBuffer[] cells, ByteBuffer key, Comparator order)
  2. indexOf(ByteBuffer buf, byte b)
  3. indexOf(ByteBuffer buf, byte value)
  4. indexOf(ByteBuffer buffer, byte b)
  5. indexOf(ByteBuffer buffer, ByteBuffer pattern)
  6. indexOf(final ByteBuffer buf, final int c, final int start, final int end)
  7. indexOf(final ByteBuffer haystack, byte[] needle)
  8. search(ByteBuffer buffer, int n, byte[] param)