Java ByteBuffer to Byte Array convertVarIntByteBufferToByteArray(ByteBuffer byteBuffer)

Here you can find the source of convertVarIntByteBufferToByteArray(ByteBuffer byteBuffer)

Description

Converts a variable length integer (https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) from a ByteBuffer to byte array

License

Apache License

Parameter

Parameter Description
byteBuffer Bytebuffer where to read from the variable length integer

Return

byte[] of the variable length integer (including marker)

Declaration

public static byte[] convertVarIntByteBufferToByteArray(ByteBuffer byteBuffer) 

Method Source Code

//package com.java2s;
/**//  ww w .  j  a va  2 s.  c o  m
* Copyright 2016 ZuInnoTe (J?rn Franke) <zuinnote@gmail.com>
*
* 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.nio.ByteBuffer;

public class Main {
    /**
    * Converts a variable length integer (https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) from a ByteBuffer to byte array
    *
    * @param byteBuffer Bytebuffer where to read from the variable length integer
    * 
    * @return byte[] of the variable length integer (including marker)
    *
    */
    public static byte[] convertVarIntByteBufferToByteArray(ByteBuffer byteBuffer) {
        // get the size
        byte originalVarIntSize = byteBuffer.get();
        byte varIntSize = getVarIntSize(originalVarIntSize);
        // reserveBuffer
        byte[] varInt = new byte[varIntSize];
        varInt[0] = originalVarIntSize;
        byteBuffer.get(varInt, 1, varIntSize - 1);
        return varInt;
    }

    /**
    * Determines size of a variable length integer (https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer)
    *
    * @param firstByteVarInt first byte of the variable integeer
    * 
    * @return byte with the size of the variable int (either 2, 3, 5 or 9) - does include the marker!
    *
    */

    public static byte getVarIntSize(byte firstByteVarInt) {
        int unsignedByte = firstByteVarInt & 0xFF;
        if (unsignedByte == 0xFD)
            return 3;
        if (unsignedByte == 0xFE)
            return 5;
        if (unsignedByte == 0xFF)
            return 9;
        return 1; //<0xFD
    }
}

Related

  1. byteBufferToByteArray(List byteBufferList)
  2. byteBufferToBytes(ByteBuffer buffer)
  3. bytes(ByteBuffer bb)
  4. bytes(ByteBuffer buf)
  5. convertToBytes(@Nonnull ByteBuffer buffer)
  6. deserializeBytes(ByteBuffer buf)
  7. getByteArray(ByteBuffer buff)
  8. getByteArray(ByteBuffer byteBuffer)
  9. getByteArray(ByteBuffer byteBuffer)