Java ByteBuffer Set byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)

Here you can find the source of byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)

Description

byte Index Of

License

Open Source License

Declaration

public static int byteIndexOf(ByteBuffer buffer, byte[] temp, int offset) 

Method Source Code

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

Related

  1. arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length)
  2. base64FormatOfBinarySet(Collection bs)
  3. blockCopy(ByteBuffer source, int srcOffset, byte[] dest, int destOffset, int count)
  4. byteBuffersToString(ByteBuffer[] bufs, Charset cs)
  5. c_memset(ByteBuffer b, int c_, int size)
  6. charSequence2ByteBuffer(CharSequence cs, Charset charset)
  7. charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)
  8. equals(final ByteBuffer keyValue, final int offset, final int keyLen, final byte[] otherKey)