Java ByteBuffer to Long readVarlong(ByteBuffer buffer)

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

Description

Read a long 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 10 bytes have been read

Return

The long value read

Declaration

public static long readVarlong(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/*//from www.  j av a2s.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.io.DataInput;

import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Read a long 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 long value read
     *
     * @throws IllegalArgumentException if variable-length value does not terminate after 10 bytes have been read
     * @throws IOException              if {@link DataInput} throws {@link IOException}
     */
    public static long readVarlong(DataInput in) throws IOException {
        long value = 0L;
        int i = 0;
        long b;
        while (((b = in.readByte()) & 0x80) != 0) {
            value |= (b & 0x7f) << i;
            i += 7;
            if (i > 63)
                throw illegalVarlongException(value);
        }
        value |= b << i;
        return (value >>> 1) ^ -(value & 1);
    }

    /**
     * Read a long 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 long value read
     *
     * @throws IllegalArgumentException if variable-length value does not terminate after 10 bytes have been read
     */
    public static long readVarlong(ByteBuffer buffer) {
        long value = 0L;
        int i = 0;
        long b;
        while (((b = buffer.get()) & 0x80) != 0) {
            value |= (b & 0x7f) << i;
            i += 7;
            if (i > 63)
                throw illegalVarlongException(value);
        }
        value |= b << i;
        return (value >>> 1) ^ -(value & 1);
    }

    private static IllegalArgumentException illegalVarlongException(
            long value) {
        throw new IllegalArgumentException(
                "Varlong is too long, most significant bit in the 10th byte is set, "
                        + "converted value: " + Long.toHexString(value));
    }
}

Related

  1. readUInt32(ByteBuffer buf, int length)
  2. readUInt64(ByteBuffer bb)
  3. readUInt64(ByteBuffer byteBuffer)
  4. readUInt8(ByteBuffer bb)
  5. readVarLong(ByteBuffer buff)
  6. toLong(ByteBuffer buffer)
  7. toLong(ByteBuffer buffer)
  8. toLong(ByteBuffer bytes)
  9. toLong(ByteBuffer value)