getting a single unsigned integer. - Java java.lang

Java examples for java.lang:int Binary

Description

getting a single unsigned integer.

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    /**//w  w w.jav  a 2  s. com
     * This is a relative method for getting a single unsigned integer.  Unlike
     * {@link ByteBuffer#getInt()}, this method won't do sign-extension.
     *
     * @param buf The {@link ByteBuffer} to get the byte from.
     * @return Returns an unsigned integer.
     * @throws BufferUnderflowException if the buffer's current position is not
     * smaller than its limit.
     */
    public static long getUnsignedInt(ByteBuffer buf) {
        return buf.getInt() & 0x00000000FFFFFFFFL;
    }

    /**
     * This is a relative method for getting a single unsigned integer.  Unlike
     * {@link ByteBuffer#getInt()}, this method won't do sign-extension.
     *
     * @param buf The {@link ByteBuffer} to get the byte from.
     * @param offset The absolute offset within the {@link ByteBuffer} of the
     * first byte to read; must be non-negative.
     * @return Returns an unsigned integer.
     * @throws BufferUnderflowException if the buffer's current position is not
     * smaller than its limit.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit minus 1.
     */
    public static long getUnsignedInt(ByteBuffer buf, int offset) {
        try {
            return buf.getInt(offset) & 0x00000000FFFFFFFFL;
        } catch (IndexOutOfBoundsException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related Tutorials