Java ByteBuffer Write writeVarint(int value, ByteBuffer buffer)

Here you can find the source of writeVarint(int value, ByteBuffer buffer)

Description

Write the given integer following the variable-length zig-zag encoding from <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol Buffers</a> into the buffer.

License

Apache License

Parameter

Parameter Description
value The value to write
buffer The output to write to

Declaration

public static void writeVarint(int value, ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/*//from ww  w  .java 2  s . c o 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.DataOutput;
import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Write the given integer following the variable-length zig-zag encoding from
     * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol Buffers</a>
     * into the output.
     *
     * @param value The value to write
     * @param out The output to write to
     */
    public static void writeVarint(int value, DataOutput out)
            throws IOException {
        int v = (value << 1) ^ (value >> 31);
        while ((v & 0xffffff80) != 0L) {
            out.writeByte((v & 0x7f) | 0x80);
            v >>>= 7;
        }
        out.writeByte((byte) v);
    }

    /**
     * Write the given integer following the variable-length zig-zag encoding from
     * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol Buffers</a>
     * into the buffer.
     *
     * @param value The value to write
     * @param buffer The output to write to
     */
    public static void writeVarint(int value, ByteBuffer buffer) {
        int v = (value << 1) ^ (value >> 31);
        while ((v & 0xffffff80) != 0L) {
            byte b = (byte) ((v & 0x7f) | 0x80);
            buffer.put(b);
            v >>>= 7;
        }
        buffer.put((byte) v);
    }
}

Related

  1. writeUnsignedInt(final ByteBuffer buffer, final long value)
  2. writeUnsignedVarInt(int value, ByteBuffer dest)
  3. writeUTF8StringToByteBuffer(String str, ByteBuffer bb)
  4. writeUUID(ByteBuffer buffer, UUID uuid)
  5. writeV(ByteBuffer byteBuffer, List vint)
  6. writeVarLong(ByteBuffer buff, long x)
  7. writeVarLong(ByteBuffer buff, long x)
  8. writeVarLong(long n, ByteBuffer buff)
  9. writeVarLong(long value, ByteBuffer buf)