Java ByteBuffer to Int readVarint(ByteBuffer buffer)

Here you can find the source of readVarint(ByteBuffer buffer)

Description

Read an integer stored in variable-length format using zig-zag decoding from <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol Buffers</a>.

License

Apache License

Parameter

Parameter Description
buffer The buffer to read from

Exception

Parameter Description
IllegalArgumentException if variable-length value does not terminate after 5 bytes have been read

Return

The integer read

Declaration

public static int readVarint(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .  j a  v  a  2s . co  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.io.DataInput;

import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Read an integer stored in variable-length format using zig-zag decoding from
     * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol Buffers</a>.
     *
     * @param buffer The buffer to read from
     * @return The integer read
     *
     * @throws IllegalArgumentException if variable-length value does not terminate after 5 bytes have been read
     */
    public static int readVarint(ByteBuffer buffer) {
        int value = 0;
        int i = 0;
        int b;
        while (((b = buffer.get()) & 0x80) != 0) {
            value |= (b & 0x7f) << i;
            i += 7;
            if (i > 28)
                throw illegalVarintException(value);
        }
        value |= b << i;
        return (value >>> 1) ^ -(value & 1);
    }

    /**
     * Read an integer stored in variable-length format using zig-zag decoding from
     * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol Buffers</a>.
     *
     * @param in The input to read from
     * @return The integer read
     *
     * @throws IllegalArgumentException if variable-length value does not terminate after 5 bytes have been read
     * @throws IOException              if {@link DataInput} throws {@link IOException}
     */
    public static int readVarint(DataInput in) throws IOException {
        int value = 0;
        int i = 0;
        int b;
        while (((b = in.readByte()) & 0x80) != 0) {
            value |= (b & 0x7f) << i;
            i += 7;
            if (i > 28)
                throw illegalVarintException(value);
        }
        value |= b << i;
        return (value >>> 1) ^ -(value & 1);
    }

    private static IllegalArgumentException illegalVarintException(int value) {
        throw new IllegalArgumentException(
                "Varint is too long, the most significant bit in the 5th byte is set, "
                        + "converted value: " + Integer.toHexString(value));
    }
}

Related

  1. readUnsignedShort(ByteBuffer buffer)
  2. readUnsignedTriByte(ByteBuffer buffer)
  3. readUnsignedVarint(ByteBuffer buffer)
  4. readVarInt(ByteBuffer buff)
  5. readVarInt(ByteBuffer buff)
  6. readVarInt(ByteBuffer buffer)
  7. readVarInt(ByteBuffer stream)
  8. readVarIntRest(ByteBuffer buff, int b)
  9. toInt(ByteBuffer buffer)