Java ByteBuffer to Int getUnsignedShort(ByteBuffer buf)

Here you can find the source of getUnsignedShort(ByteBuffer buf)

Description

This is a relative method for getting 2 unsigned bytes.

License

Open Source License

Parameter

Parameter Description
buf The ByteBuffer to get bytes from.

Exception

Parameter Description
BufferUnderflowException if there are fewer than two bytesremaining in the buffer.

Return

Returns an unsigned integer in the range 0-65536.

Declaration

public static int getUnsignedShort(ByteBuffer buf) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    /**//from   ww w .j  a  va  2 s .  c  o  m
     * This is a relative method for getting 2 unsigned bytes.  Unlike
     * {@link ByteBuffer#getShort()}, this method won't do sign-extension.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @return Returns an unsigned integer in the range 0-65536.
     * @throws BufferUnderflowException if there are fewer than two bytes
     * remaining in the buffer.
     */
    public static int getUnsignedShort(ByteBuffer buf) {
        return buf.getShort() & 0x0000FFFF;
    }

    /**
     * This is an absolute method for getting 2 unsigned bytes.  Unlike
     * {@link ByteBuffer#getShort(int)}, this method won't do sign-extension.
     *
     * @param buf The {@link ByteBuffer} to get bytes 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 in the range 0-65536.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit minus 1.
     */
    public static int getUnsignedShort(ByteBuffer buf, int offset) {
        try {
            return buf.getShort(offset) & 0x0000FFFF;
        } catch (IndexOutOfBoundsException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. getUnsignedLong(ByteBuffer buf)
  2. getUnsignedLong(final ByteBuffer src)
  3. getUnsignedShort(ByteBuffer bb)
  4. getUnsignedShort(ByteBuffer bb)
  5. getUnsignedShort(ByteBuffer bb)
  6. getUnsignedShort(ByteBuffer buffer)
  7. getUnsignedShort(ByteBuffer buffer, int offset)
  8. getUnsignedShort(ByteBuffer bytes)
  9. getUnsignedSmart(ByteBuffer buf)