Read an unsigned variable length int from a ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer Read

Description

Read an unsigned variable length int from a ByteBuffer

Demo Code

/*//  w  ww  .  j  av a 2  s.co m
Copyright (c) 2005 Health Market Science, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
USA

You can contact Health Market Science at info@healthmarketscience.com
or at the following address:

Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
 */
//package com.java2s;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**
     * Read an unsigned variable length int from a buffer
     * @param buffer Buffer containing the variable length int
     * @return The unsigned int
     */
    public static int getUnsignedVarInt(ByteBuffer buffer, int numBytes) {
        int pos = buffer.position();
        int rtn = getUnsignedVarInt(buffer, pos, numBytes);
        buffer.position(pos + numBytes);
        return rtn;
    }

    /**
     * Read an unsigned variable length int from a buffer
     * @param buffer Buffer containing the variable length int
     * @param offset Offset at which to read the value
     * @return The unsigned int
     */
    public static int getUnsignedVarInt(ByteBuffer buffer, int offset,
            int numBytes) {
        switch (numBytes) {
        case 1:
            return getUnsignedByte(buffer, offset);
        case 2:
            return getUnsignedShort(buffer, offset);
        case 3:
            return get3ByteInt(buffer, offset);
        case 4:
            return buffer.getInt(offset);
        default:
            throw new IllegalArgumentException("Invalid num bytes "
                    + numBytes);
        }
    }

    /**
     * Read an unsigned byte from a buffer
     * @param buffer Buffer containing the bytes
     * @return The unsigned byte as an int
     */
    public static int getUnsignedByte(ByteBuffer buffer) {
        int pos = buffer.position();
        int rtn = getUnsignedByte(buffer, pos);
        buffer.position(pos + 1);
        return rtn;
    }

    /**
     * Read an unsigned byte from a buffer
     * @param buffer Buffer containing the bytes
     * @param offset Offset at which to read the byte
     * @return The unsigned byte as an int
     */
    public static int getUnsignedByte(ByteBuffer buffer, int offset) {
        return asUnsignedByte(buffer.get(offset));
    }

    /**
     * Read an unsigned short from a buffer
     * @param buffer Buffer containing the short
     * @return The unsigned short as an int
     */
    public static int getUnsignedShort(ByteBuffer buffer) {
        int pos = buffer.position();
        int rtn = getUnsignedShort(buffer, pos);
        buffer.position(pos + 2);
        return rtn;
    }

    /**
     * Read an unsigned short from a buffer
     * @param buffer Buffer containing the short
     * @param offset Offset at which to read the short
     * @return The unsigned short as an int
     */
    public static int getUnsignedShort(ByteBuffer buffer, int offset) {
        return asUnsignedShort(buffer.getShort(offset));
    }

    /**
     * Read a 3 byte int from a buffer
     * @param buffer Buffer containing the bytes
     * @return The int
     */
    public static int get3ByteInt(ByteBuffer buffer) {
        return get3ByteInt(buffer, buffer.order());
    }

    /**
     * Read a 3 byte int from a buffer
     * @param buffer Buffer containing the bytes
     * @param order  the order of the bytes of the int
     * @return The int
     */
    public static int get3ByteInt(ByteBuffer buffer, ByteOrder order) {
        int pos = buffer.position();
        int rtn = get3ByteInt(buffer, pos, order);
        buffer.position(pos + 3);
        return rtn;
    }

    /**
     * Read a 3 byte int from a buffer
     * @param buffer Buffer containing the bytes
     * @param offset Offset at which to start reading the int
     * @return The int
     */
    public static int get3ByteInt(ByteBuffer buffer, int offset) {
        return get3ByteInt(buffer, offset, buffer.order());
    }

    /**
     * Read a 3 byte int from a buffer
     * @param buffer Buffer containing the bytes
     * @param offset Offset at which to start reading the int
     * @param order  the order of the bytes of the int
     * @return The int
     */
    public static int get3ByteInt(ByteBuffer buffer, int offset,
            ByteOrder order) {

        int offInc = 1;
        if (order == ByteOrder.BIG_ENDIAN) {
            offInc = -1;
            offset += 2;
        }

        int rtn = getUnsignedByte(buffer, offset);
        rtn += (getUnsignedByte(buffer, offset + (1 * offInc)) << 8);
        rtn += (getUnsignedByte(buffer, offset + (2 * offInc)) << 16);
        return rtn;
    }

    /**
     * @param buffer Buffer containing the bytes
     * @param order  the order of the bytes of the int
     * @return an int from the current position in the given buffer, read using
     *         the given ByteOrder
     */
    public static int getInt(ByteBuffer buffer, ByteOrder order) {
        int offset = buffer.position();
        int rtn = getInt(buffer, offset, order);
        buffer.position(offset + 4);
        return rtn;
    }

    /**
     * @param buffer Buffer containing the bytes
     * @param offset Offset at which to start reading the int
     * @param order  the order of the bytes of the int
     * @return an int from the given position in the given buffer, read using
     *         the given ByteOrder
     */
    public static int getInt(ByteBuffer buffer, int offset, ByteOrder order) {
        ByteOrder origOrder = buffer.order();
        try {
            return buffer.order(order).getInt(offset);
        } finally {
            buffer.order(origOrder);
        }
    }

    /**
     * @return the byte value converted to an unsigned int value
     */
    public static int asUnsignedByte(byte b) {
        return b & 0xFF;
    }

    /**
     * @return the short value converted to an unsigned int value
     */
    public static int asUnsignedShort(short s) {
        return s & 0xFFFF;
    }
}

Related Tutorials