Java Byte Array Decode decodeUnsignedVarInt(byte[] b, int offset)

Here you can find the source of decodeUnsignedVarInt(byte[] b, int offset)

Description

Decodes an integer as encoded by encodeUnsignedVarInt.

License

Apache License

Declaration

public static int decodeUnsignedVarInt(byte[] b, int offset) 

Method Source Code


//package com.java2s;
/*/*from www  .j a v a2  s  . c  o  m*/
 *  Copyright 2011-2015 Cojen.org
 *
 *  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.
 */

import java.io.EOFException;

public class Main {
    /**
     * Decodes an integer as encoded by encodeUnsignedVarInt.
     */
    public static int decodeUnsignedVarInt(byte[] b, int offset) {
        int v = b[offset];
        if (v >= 0) {
            return v;
        }
        switch ((v >> 4) & 0x07) {
        case 0x00:
        case 0x01:
        case 0x02:
        case 0x03:
            return (1 << 7) + (((v & 0x3f) << 8) | (b[offset + 1] & 0xff));
        case 0x04:
        case 0x05:
            return ((1 << 14) + (1 << 7))
                    + (((v & 0x1f) << 16) | ((b[++offset] & 0xff) << 8) | (b[offset + 1] & 0xff));
        case 0x06:
            return ((1 << 21) + (1 << 14) + (1 << 7)) + (((v & 0x0f) << 24) | ((b[++offset] & 0xff) << 16)
                    | ((b[++offset] & 0xff) << 8) | (b[offset + 1] & 0xff));
        default:
            return ((1 << 28) + (1 << 21) + (1 << 14) + (1 << 7)) + ((b[++offset] << 24)
                    | ((b[++offset] & 0xff) << 16) | ((b[++offset] & 0xff) << 8) | (b[offset + 1] & 0xff));
        }
    }

    /**
     * Decodes an integer as encoded by encodeUnsignedVarInt.
     */
    public static int decodeUnsignedVarInt(byte[] b, int start, int end) throws EOFException {
        if (start >= end) {
            throw new EOFException();
        }
        int v = b[start];
        if (v >= 0) {
            return v;
        }
        switch ((v >> 4) & 0x07) {
        case 0x00:
        case 0x01:
        case 0x02:
        case 0x03:
            if (++start >= end) {
                throw new EOFException();
            }
            return (1 << 7) + (((v & 0x3f) << 8) | (b[start] & 0xff));
        case 0x04:
        case 0x05:
            if (start + 2 >= end) {
                throw new EOFException();
            }
            return ((1 << 14) + (1 << 7))
                    + (((v & 0x1f) << 16) | ((b[++start] & 0xff) << 8) | (b[start + 1] & 0xff));
        case 0x06:
            if (start + 3 >= end) {
                throw new EOFException();
            }
            return ((1 << 21) + (1 << 14) + (1 << 7)) + (((v & 0x0f) << 24) | ((b[++start] & 0xff) << 16)
                    | ((b[++start] & 0xff) << 8) | (b[start + 1] & 0xff));
        default:
            if (start + 4 >= end) {
                throw new EOFException();
            }
            return ((1 << 28) + (1 << 21) + (1 << 14) + (1 << 7)) + ((b[++start] << 24)
                    | ((b[++start] & 0xff) << 16) | ((b[++start] & 0xff) << 8) | (b[start + 1] & 0xff));
        }
    }
}

Related

  1. decodeString(byte[] bytearr)
  2. decodeString(byte[] data)
  3. decodeString(byte[] data)
  4. decodeString(byte[] stringBytes)
  5. decodeToString(byte[] arr)