Java ByteBuffer Get getData(ByteBuffer buf, int index, int size)

Here you can find the source of getData(ByteBuffer buf, int index, int size)

Description

Get the int value of the given channel sample using the given size.

License

Open Source License

Parameter

Parameter Description
buf the buffer
index the index in the buffer
size the size of the data (1, 2, 3, or 4 byte)

Return

int

Declaration

private static final int getData(ByteBuffer buf, int index, int size) 

Method Source Code


//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**/*  w  w w. j  a  v a2s.  c  o m*/
     * Get the int value of the given channel sample using the given size.
     * <p>
     * Returns zero if the size is less than 1 or greater than 3.
     * @param buf the buffer
     * @param index the index in the buffer
     * @param size the size of the data (1, 2, 3, or 4 byte)
     * @return int
     */
    private static final int getData(ByteBuffer buf, int index, int size) {
        if (size == 1) {
            // unsigned 8bit
            return buf.get(index) & 0xFFFFFFFF;
        } else if (size == 2) {
            // signed 16bit
            return buf.getShort(index);
        } else if (size == 3) {
            // signed 24bit
            return get24BitInt(buf.get(index), buf.get(index + 1), buf.get(index + 2), buf.order());
        } else {
            // signed 32bit
            return buf.getInt(index);
        }
    }

    /**
     * Returns an integer for the given 3 bytes.
     * @param b1 top byte
     * @param b2 mid byte
     * @param b3 low byte
     * @param order the byte order
     * @return int
     */
    private static final int get24BitInt(byte b1, byte b2, byte b3, ByteOrder order) {
        if (order == ByteOrder.BIG_ENDIAN) {
            return ((b1 << 16) | ((b2 & 0xFF) << 8) | (b3 & 0xFF));
        } else {
            return ((b3 << 16) | ((b2 & 0xFF) << 8) | (b1 & 0xFF));
        }
    }
}

Related

  1. getCPCharacter(ByteBuffer buffer)
  2. getCrcChecksum(ByteBuffer buffer)
  3. getCRLFCRLFIndex(ByteBuffer buffer)
  4. getCRLFIndex(ByteBuffer buffer)
  5. getCRLFLine(ByteBuffer buf)
  6. getDataFromBuffer(ByteBuffer b)
  7. getDirectByteBuffer(ByteBuffer var0, boolean var1)
  8. getDouble(ByteBuffer buffer)
  9. getDoubleArray(ByteBuffer buffer, int numDoubles)