Java examples for java.nio:ByteBuffer
This is a relative method for getting a single unsigned byte from ByteBuffer.
//package com.java2s; import java.nio.ByteBuffer; public class Main { /**/*from ww w .j a v a2s.com*/ * This is a relative method for getting a single unsigned byte. Unlike * {@link ByteBuffer#get()}, this method won't do sign-extension. * * @param buf The {@link ByteBuffer} to get the byte from. * @return Returns an unsigned integer in the range 0-256. * @throws BufferUnderflowException if the buffer's current position is not * smaller than its limit. */ public static int getUnsignedByte(ByteBuffer buf) { return buf.get() & 0x000000FF; } /** * This is an absolute method for getting a single unsigned byte. Unlike * {@link ByteBuffer#get(int)}, 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 in the range 0-256. * @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. */ public static int getUnsignedByte(ByteBuffer buf, int offset) { try { return buf.get(offset) & 0x000000FF; } catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException(e); } } }