Java ByteBuffer Set find(ByteBuffer buffer, int offset, byte searchKey)

Here you can find the source of find(ByteBuffer buffer, int offset, byte searchKey)

Description

find

License

Apache License

Declaration

public static int find(ByteBuffer buffer, int offset, byte searchKey) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    public static int find(ByteBuffer buffer, int offset, byte searchKey) {
        for (int i = buffer.position() + offset; i < buffer.limit(); i++) {
            byte nextByte = buffer.get(i);
            if (nextByte == searchKey) {
                return i;
            }/*w w  w.  j a va 2  s.  co m*/
        }
        return -1;
    }

    public static int find(ByteBuffer buffer, int offset, byte[] searchKey) {
        int searchIndex = 0;
        for (int i = buffer.position() + offset; i < buffer.limit() - searchKey.length; i++) {
            byte nextByte = buffer.get(i);
            if (nextByte != searchKey[searchIndex]) {
                searchIndex = 0;
            } else {
                searchIndex++;
                if (searchIndex == searchKey.length) {
                    return i + 1 - searchKey.length;
                }
            }
        }
        return -1;
    }
}

Related

  1. byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)
  2. c_memset(ByteBuffer b, int c_, int size)
  3. charSequence2ByteBuffer(CharSequence cs, Charset charset)
  4. charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)
  5. equals(final ByteBuffer keyValue, final int offset, final int keyLen, final byte[] otherKey)
  6. findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)
  7. from(ByteBuffer buffer, int offset)
  8. fromListToSetByteArray(List list)
  9. get(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length)