Example usage for java.nio ShortBuffer put

List of usage examples for java.nio ShortBuffer put

Introduction

In this page you can find the example usage for java.nio ShortBuffer put.

Prototype

public abstract ShortBuffer put(int index, short s);

Source Link

Document

Writes a short to the specified index of this buffer; the position is not changed.

Usage

From source file:Main.java

public static void main(String[] args) {
    ShortBuffer bb = ShortBuffer.wrap(new short[] { 0, 1, 2, 3, 4, 5, 6 });
    bb.put(0, (short) 100);
    System.out.println(Arrays.toString(bb.array()));

}

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 w w w  .ja  v a  2  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;
}