Example usage for java.nio ShortBuffer capacity

List of usage examples for java.nio ShortBuffer capacity

Introduction

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

Prototype

public final int capacity() 

Source Link

Document

Returns the capacity of this buffer.

Usage

From source file:Main.java

public static ShortBuffer setupShortBuffer(ShortBuffer preBuffer, short[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createShortBuffer(array.length * 2);
    } else {// ww w . ja  v a  2s  . c  om
        preBuffer.clear();
    }

    preBuffer.clear();
    preBuffer.put(array);
    preBuffer.position(0);

    return preBuffer;
}

From source file:Main.java

private static String formatShorts(byte[] data, boolean unsigned) {
    ShortBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        if (unsigned) {
            sb.append(bb.get() & 0xffff);
        } else {/* w ww  .  j  a v a  2  s. com*/
            sb.append(bb.get());
        }
        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

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 .  j  a  va2s.  c o 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:org.nuras.mcpha.Client.java

/**
 * // w ww .  j a  va2s .c o m
 * @param user
 * @param channels
 * @param trigger_mode
 * @param trigger_level
 * @param trigger_slope
 * @param trigger_source
 * @throws IOException 
 */
synchronized public static void acquireOscilloscopeData(Session user, int channels, String trigger_mode,
        int trigger_level, String trigger_slope, int trigger_source) throws IOException {
    mcphaResetOscilloscope();

    // Set number of samples to skip before trigger
    mcphaSetNumberOfSamplesBeforeTrigger(5000);

    // Set total number of samples to acquire for this run
    mcphaSetTotalNumberOfSamplesToAcquire(65536);

    // Start oscilloscope
    mcphaStartOscilloscope();

    // wait for 200 milliseconds
    try {
        Thread.sleep(200);
    } catch (InterruptedException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Read oscilloscope status
    for (int i = 0; i < 5; i++) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Oscilloscope status:" + mcphaReadOscilloscopeStatus());
    }

    // get oscillsocope data
    ShortBuffer data = mcphaGetOsilloscopeData();

    boolean channel_1_requested = (channels & 0x01) != 0;
    boolean channel_2_requested = (channels & 0x02) != 0;

    // push data
    JSONObject json = createJSONResponseObject();
    json.put("command", "get_oscilloscope_data");
    json.put("message", "");
    json.put("status", 0);

    JSONArray arr1 = new JSONArray();
    JSONArray arr2 = new JSONArray();
    data.rewind();
    for (int i = 0, n1 = 0, n2 = 0; i < data.capacity(); i += 2) {
        // channel 1 data
        JSONArray xy = new JSONArray();
        short d = data.get();
        if (channel_1_requested) {
            arr1.put(xy.put(n1++).put(d));
        }
        // channel 2 data
        xy = new JSONArray();
        d = data.get();
        if (channel_2_requested) {
            arr2.put(xy.put(n2++).put(d));
        }
    }
    json.put("data1", arr1);
    json.put("label1", channel_1_requested ? "Channel 1" : "");

    json.put("data2", arr2);
    json.put("label2", channel_2_requested ? "Channel 2" : "");

    sendJSONObjectMessage(user.getRemote(), json);

}