Example usage for java.nio ByteBuffer asShortBuffer

List of usage examples for java.nio ByteBuffer asShortBuffer

Introduction

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

Prototype

public abstract ShortBuffer asShortBuffer();

Source Link

Document

Returns a short buffer which is based on the remaining content of this byte buffer.

Usage

From source file:Main.java

/**
 * Creates a {@link ShortBuffer} based on the given data.
 *
 * @param data the data for the buffer//  w w  w .  j  ava2  s.c om
 * @return the short buffer
 */
public static ShortBuffer createShortBuffer(final short[] data) {
    if (data == null) {
        return null;
    }
    ByteBuffer bbVertices = ByteBuffer.allocateDirect(data.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    final ShortBuffer fBuffer = bbVertices.asShortBuffer();
    fBuffer.put(data);
    fBuffer.position(0);
    return fBuffer;
}

From source file:Main.java

public static ShortBuffer createShortBuffer(short[] shortData) {
    ShortBuffer drawListBuffer;//  w w w .j  a  va  2  s .c  o  m
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            shortData.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(shortData);
    drawListBuffer.position(0);
    return drawListBuffer;
}

From source file:org.bimserver.utils.BinUtils.java

public static byte[] shortToByteArray(short inShort) {
    byte[] bArray = new byte[2];
    ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
    ShortBuffer lBuffer = bBuffer.asShortBuffer();
    lBuffer.put(inShort);/* w  w w.  ja v a2s .  c om*/
    return bArray;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct short buffer with native byte order.
 *
 * @param size/*from ww  w  . j  a  v  a 2s  . c o m*/
 *            The data
 * @return The created direct short buffer
 */

public static ShortBuffer createDirectShortBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * SHORT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asShortBuffer();
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified short buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away./*from  w ww.jav a 2s.  co m*/
 *
 * @param buffer
 *            The short buffer to convert
 * @return The converted short buffer or the source buffer if no conversion
 *         is needed
 */

public static ShortBuffer convertToNativeEndian(final ShortBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final ShortBuffer shorts = bytes.asShortBuffer();
    shorts.put(buffer).rewind();
    return shorts;
}

From source file:com.google.android.apps.body.LayersLoader.java

private static ShortBuffer decodeIndexBuffer(DrawGroup drawGroup, char[] data, int start, int length) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    ShortBuffer indexData = byteBuffer.asShortBuffer();
    int prev = 0;
    for (int i = 0; i < length;) {
        int limit = Math.min(length - i, BUFSIZE);
        int s = start + i;
        for (int j = 0; j < limit; ++j) {
            int word = data[s + j];
            prev += (word >> 1) ^ (-(word & 1));
            mBuffer[j] = (short) prev;
        }/*  w w w. j  a  v  a2s  .  c o  m*/
        i += limit;
        indexData.put(mBuffer, 0, limit);
    }
    indexData.rewind();
    return indexData;
}

From source file:com.google.android.apps.body.LayersLoader.java

private static ShortBuffer decodeVertexBuffer(DrawGroup drawGroup, char[] data, int start, int length) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    ShortBuffer vertexData = byteBuffer.asShortBuffer();
    short prev0 = 0, prev1 = 0, prev2 = 0, prev3 = 0;
    short prev4 = 0, prev5 = 0, prev6 = 0, prev7 = 0;
    for (int i = 0; i < length;) {
        int limit = Math.min(length - i, BUFSIZE);
        int s = start + i;
        for (int j = 0; j < limit;) {
            short word = (short) data[s + j];
            prev0 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) (prev0 - 8192);
            word = (short) data[s + j];
            prev1 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) (prev1 - 4096);
            word = (short) data[s + j];
            prev2 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) (prev2 - 8192);
            word = (short) data[s + j];
            prev3 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) ((prev3 - 256) << 7);
            word = (short) data[s + j];
            prev4 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) ((prev4 - 256) << 7);
            word = (short) data[s + j];
            prev5 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) ((prev5 - 256) << 7);
            word = (short) data[s + j];
            prev6 += (word >> 1) ^ (-(word & 1));
            mBuffer[j++] = (short) prev6;
            word = (short) data[s + j];
            prev7 += (word >> 1) ^ (-(word & 1));
            // The web version flips the tex images instead.
            mBuffer[j++] = (short) (512 - prev7);
        }/* w  w w .  ja  v a  2  s .c  om*/
        i += limit;
        vertexData.put(mBuffer, 0, limit);
    }
    vertexData.rewind();

    return vertexData;
}

From source file:org.nuras.mcpha.Client.java

/**
 * Get oscilloscope data which are 16-bit signed integer values.
 * The channels are interleaved sample-by-sample (ch1, ch2, ch1, ch2, etc).
 * /*  w  ww. jav a2  s.  co  m*/
 * @return a ShortBuffer of channel data values.
 * @throws java.io.IOException 
 */
synchronized public static ShortBuffer mcphaGetOsilloscopeData() throws IOException {
    sendCommand(MCPHA_COMMAND_READ_OSCILLOSCOPE_DATA, 0L, 0L);

    DataInputStream in = new DataInputStream(deviceSocket.getInputStream());

    ByteBuffer data = ByteBuffer.allocate(65536);
    data.order(ByteOrder.nativeOrder());
    in.readFully(data.array());

    return data.asShortBuffer();
}

From source file:com.google.android.apps.body.LayersLoader.java

private void createColorBuffer(DrawGroup drawGroup) {
    int numVertices = drawGroup.vertexBufferData.capacity() / 8; // 3 pos, 3 norm, 2 texcoord

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(numVertices * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    drawGroup.colorBufferData = byteBuffer.asShortBuffer();

    for (Draw draw : drawGroup.draws) {
        short selectionColor = mMaxColorIndex++;
        mSelectionColorMap.put((int) selectionColor, draw);

        BodyJni.setColorForIndices(drawGroup.colorBufferData, selectionColor, drawGroup.indexBufferData,
                draw.offset, draw.count);
    }/*from  w  ww. ja v a  2  s.com*/
}

From source file:ome.io.bioformats.BfPixelsWrapper.java

/**
 * cgb - stolen from ImportLibrary - slightly modified
 *
 * Examines a byte array to see if it needs to be byte swapped and modifies
 * the byte array directly./*from   www.  jav  a2  s .c  om*/
 * @param bytes The byte array to check and modify if required.
 * @return the <i>byteArray</i> either swapped or not for convenience.
 * @throws IOException if there is an error read from the file.
 * @throws FormatException if there is an error during metadata parsing.
 */
public byte[] swapIfRequired(byte[] bytes) throws FormatException, IOException {
    // We've got nothing to do if the samples are only 8-bits wide.
    if (pixelSize == 1)
        return bytes;

    boolean isLittleEndian = reader.isLittleEndian();
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    int length;
    if (isLittleEndian) {
        if (pixelSize == 2) { // short/ushort
            ShortBuffer buf = buffer.asShortBuffer();
            length = buffer.limit() / 2;
            for (int i = 0; i < length; i++) {
                buf.put(i, DataTools.swap(buf.get(i)));
            }
        } else if (pixelSize == 4) { // int/uint/float
            IntBuffer buf = buffer.asIntBuffer();
            length = buffer.limit() / 4;
            for (int i = 0; i < length; i++) {
                buf.put(i, DataTools.swap(buf.get(i)));
            }
        } else if (pixelSize == 8) // long/double
        {
            LongBuffer buf = buffer.asLongBuffer();
            length = buffer.limit() / 8;
            for (int i = 0; i < length; i++) {
                buf.put(i, DataTools.swap(buf.get(i)));
            }
        } else {
            throw new FormatException(String.format("Unsupported sample bit width: %d", pixelSize));
        }
    }
    // We've got a big-endian file with a big-endian byte array.
    bytes = buffer.array();
    return bytes;
}