Example usage for java.nio ByteBuffer order

List of usage examples for java.nio ByteBuffer order

Introduction

In this page you can find the example usage for java.nio ByteBuffer order.

Prototype

Endianness order

To view the source code for java.nio ByteBuffer order.

Click Source Link

Document

The byte order of this buffer, default is BIG_ENDIAN .

Usage

From source file:Main.java

private static final FloatBuffer createDebugColors(int drawMode, int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(size * 4 * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer colors = bb.asFloatBuffer();
    if (drawMode == GL10.GL_TRIANGLES) {
        for (int i = 0; i < size * 4; i++) {
            colors.put(triangleColors[i % triangleColors.length]);
        }//w w  w  .  j  a  va2 s .  c o m
    } else {
        for (int i = 0; i < size; i++) {
            colors.put(1f);
            colors.put(0f);
            colors.put(1f);
            colors.put(1f);
        }
    }
    colors.rewind();
    return colors;
}

From source file:Main.java

/** create AdvertiseDate for iBeacon */
public static AdvertiseData createEmployeeIDAdvertiseData(byte[] data, int length) {
    byte[] manufacturerData = new byte[23];
    ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.put((byte) 0x4d);
    bb.put((byte) 0x44);
    bb.put((byte) 0x43);
    bb.put(data);/*from  ww  w.  ja  v a  2 s.  c om*/
    for (int i = 0; i < length; i++) {
        bb.put((byte) 0x00);
    }

    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    builder.addManufacturerData(0x006d, manufacturerData);
    AdvertiseData adv = builder.build();
    return adv;
}

From source file:Main.java

public static ByteBuffer makeByteBuffer(byte[] array) {
    final int SIZE = Byte.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * SIZE);
    byteBuffer.order(ByteOrder.nativeOrder());
    byteBuffer.put(array);/*from   www  .  ja  v  a2s.c  om*/
    byteBuffer.position(0);
    return byteBuffer;
}

From source file:Main.java

/**
 * Returns new byte buffer whose content is a shared subsequence of this buffer's content
 * between the specified start (inclusive) and end (exclusive) positions. As opposed to
 * {@link ByteBuffer#slice()}, the returned buffer's byte order is the same as the source
 * buffer's byte order.//  w  w w .  j ava 2 s  .  c om
 */
private static ByteBuffer sliceFromTo(final ByteBuffer source, final int start, final int end) {
    if (start < 0) {
        throw new IllegalArgumentException("start: " + start);
    }
    if (end < start) {
        throw new IllegalArgumentException("end < start: " + end + " < " + start);
    }
    final int capacity = source.capacity();
    if (end > source.capacity()) {
        throw new IllegalArgumentException("end > capacity: " + end + " > " + capacity);
    }
    final int originalLimit = source.limit();
    final int originalPosition = source.position();
    try {
        source.position(0);
        source.limit(end);
        source.position(start);
        final ByteBuffer result = source.slice();
        result.order(source.order());
        return result;
    } finally {
        source.position(0);
        source.limit(originalLimit);
        source.position(originalPosition);
    }
}

From source file:Main.java

/**
 * Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that
 * preceeds it as a ByteBuffer slice/*from   w ww .j av  a  2s  .  c  o  m*/
 * 
 * Segment byte order is always little endian
 * 
 * @param buf
 * @return
 */
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
    if (!buf.hasRemaining())
        return null;

    int from = buf.position();
    ByteBuffer result = buf.slice();
    result.order(ByteOrder.BIG_ENDIAN);

    int val = 0xffffffff;
    while (buf.hasRemaining()) {
        val = (val << 8) | (buf.get() & 0xff);
        if (val >= mmin && val <= mmax) {
            if (n == 0) {
                buf.position(buf.position() - 4);
                result.limit(buf.position() - from);
                break;
            }
            --n;
        }
    }
    return result;
}

From source file:Main.java

/**
 * Creates a {@link IntBuffer} based on the given data.
 *
 * @param data the data for the buffer// w ww . ja  va 2  s.  co m
 * @return the int buffer
 */
public static IntBuffer createIntBuffer(final float[] data) {
    final int[] tmpData = new int[data.length];
    for (int i = 0; i < tmpData.length; i++) {
        tmpData[i] = Float.floatToRawIntBits(data[i]);
    }
    final ByteBuffer bbVertices = ByteBuffer.allocateDirect(tmpData.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    final IntBuffer intBuffer = bbVertices.asIntBuffer();
    intBuffer.put(tmpData);
    intBuffer.flip();
    return intBuffer;
}

From source file:Main.java

/**
 * Allocates a direct float buffer, and populates it with the float array data.
 *///  www. j av a2  s.  c  om
public static FloatBuffer createFloatBuffer(float[] coords) {
    // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it.
    ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZEOF_FLOAT);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(coords);
    fb.position(0);
    return fb;
}

From source file:Main.java

/**buffer methods*/

public static IntBuffer makeIntBuffer(int[] array) {
    final int integerSize = Integer.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * integerSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(array);//w ww  .j  av a2  s  . c  o m
    intBuffer.position(0);
    return intBuffer;
}

From source file:Main.java

public static FloatBuffer makeFloatBuffer(int length) {
    final int floatSize = Float.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length * floatSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.position(0);/*from   w w  w.  j  a v  a2s  .  c  o m*/
    return floatBuffer;
}

From source file:Main.java

/**
 * Convert from singed 16-bit PCM to 32-bit float PCM.
 * @param byteArray byte array/*from w w  w  . j a v a2 s .c om*/
 * @return byte array
 */
public static byte[] convert16BitTo32Bit(final byte[] byteArray) {
    float[] audioDataF = shortToFloat(byteToShort(byteArray));
    for (int i = 0; i < audioDataF.length; i++) {
        audioDataF[i] /= 32768.0;
    }

    FloatBuffer fb = FloatBuffer.wrap(audioDataF);
    ByteBuffer byteBuffer = ByteBuffer.allocate(fb.capacity() * 4);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.asFloatBuffer().put(fb);
    return byteBuffer.array();
}