Android ByteBuffer Put 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 ww.  j ava  2  s  .com
 * 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. getUnsignedShort(final ByteBuffer pByteBuffer, final int pPosition)
  2. putUnsignedInt(final ByteBuffer pByteBuffer, final int pPosition, final long pValue)
  3. putUnsignedInt(final ByteBuffer pByteBuffer, final long pValue)
  4. putUnsignedShort(final ByteBuffer pByteBuffer, final int pPosition, final int pValue)
  5. putUnsignedShort(final ByteBuffer pByteBuffer, final int pValue)