Java Byte Array Convert To bytesToVar32(final byte[] buffer, final int offset)

Here you can find the source of bytesToVar32(final byte[] buffer, final int offset)

Description

bytes To Var

License

Apache License

Declaration

public static int bytesToVar32(final byte[] buffer, final int offset) 

Method Source Code

//package com.java2s;
/*/*from www. j a va 2s.c o m*/
 * Copyright 2014 NAVER Corp.
 *
 * Licensed 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.
 */

public class Main {
    public static int bytesToVar32(final byte[] buffer, final int offset) {
        if (buffer == null) {
            throw new NullPointerException("buffer must not be null");
        }
        checkBound(buffer.length, offset);

        // borrowing the protocol buffer's concept of variable-length encoding
        // copy https://github.com/google/protobuf 2.6.1
        // CodedInputStream.java -> int readRawVarint32()

        // See implementation notes for readRawVarint64
        fastpath: {
            int pos = offset;
            final int bufferSize = buffer.length;
            if (bufferSize == pos) {
                break fastpath;
            }

            int x;
            if ((x = buffer[pos++]) >= 0) {
                return x;
            } else if (bufferSize - pos < 9) {
                break fastpath;
            } else if ((x ^= (buffer[pos++] << 7)) < 0) {
                x ^= (~0 << 7);
            } else if ((x ^= (buffer[pos++] << 14)) >= 0) {
                x ^= (~0 << 7) ^ (~0 << 14);
            } else if ((x ^= (buffer[pos++] << 21)) < 0) {
                x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21);
            } else {
                int y = buffer[pos++];
                x ^= y << 28;
                x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28);
                if (y < 0 && buffer[pos++] < 0 && buffer[pos++] < 0 && buffer[pos++] < 0 && buffer[pos++] < 0
                        && buffer[pos] < 0) {
                    break fastpath; // Will throw malformedVarint()
                }
            }

            return x;
        }
        return (int) readVar64SlowPath(buffer, offset);
    }

    static void checkBound(final int bufferLength, final int offset) {
        if (offset < 0) {
            throw new IndexOutOfBoundsException("negative offset:" + offset);
        }
        if (offset >= bufferLength) {
            throw new IndexOutOfBoundsException("invalid offset:" + offset + " bufferLength:" + bufferLength);
        }
    }

    /** Variant of readRawVarint64 for when uncomfortably close to the limit. */
    /* Visible for testing */
    static long readVar64SlowPath(final byte[] buffer, int offset) {

        long result = 0;
        for (int shift = 0; shift < 64; shift += 7) {
            final byte b = buffer[offset++];
            result |= (long) (b & 0x7F) << shift;
            if ((b & 0x80) == 0) {
                return result;
            }
        }
        throw new IllegalArgumentException("invalid varLong. start offset:" + offset + " readOffset:" + offset);
    }
}

Related

  1. bytesToStringUTFCustom(byte[] bytes)
  2. bytesToSVar64(final byte[] buffer, final int offset)
  3. bytesToTagBE(byte[] bytes, int off)
  4. bytesToText(byte[] bytes)
  5. BytesToUInt16(byte[] pb)