Java ByteBuffer Set findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)

Here you can find the source of findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)

Description

Find length of common prefix of two parts in the buffer

License

Apache License

Parameter

Parameter Description
buffer Where parts are located.
offsetLeft Offset of the first part.
offsetRight Offset of the second part.
limit Maximal length of common prefix.

Return

Length of prefix.

Declaration

public static int findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit) 

Method Source Code

//package com.java2s;
/*/*from   w w w . j  a v a2  s  .c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Find length of common prefix of two parts in the buffer
     * @param buffer Where parts are located.
     * @param offsetLeft Offset of the first part.
     * @param offsetRight Offset of the second part.
     * @param limit Maximal length of common prefix.
     * @return Length of prefix.
     */
    public static int findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit) {
        int prefix = 0;

        for (; prefix < limit; ++prefix) {
            if (buffer.get(offsetLeft + prefix) != buffer.get(offsetRight + prefix)) {
                break;
            }
        }

        return prefix;
    }

    /**
     * Find length of common prefix in two arrays.
     * @param left Array to be compared.
     * @param leftOffset Offset in left array.
     * @param leftLength Length of left array.
     * @param right Array to be compared.
     * @param rightOffset Offset in right array.
     * @param rightLength Length of right array.
     */
    public static int findCommonPrefix(byte[] left, int leftOffset, int leftLength, byte[] right, int rightOffset,
            int rightLength) {
        int length = Math.min(leftLength, rightLength);
        int result = 0;

        while (result < length && left[leftOffset + result] == right[rightOffset + result]) {
            result++;
        }

        return result;
    }
}

Related

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