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:Triangle.java

public Triangle() {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);/*  w ww .  j ava  2 s .  com*/

    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());

    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
}

From source file:tor.Cell.java

public byte[] getBytes(int protocolVersion) {
    byte cell[];//from  ww  w .  jav a 2  s. c o  m

    if (cmdId == 7 || cmdId >= 128)
        cell = new byte[(protocolVersion < 4 ? 3 : 5) + 2 + payload.length];
    else
        cell = new byte[protocolVersion < 4 ? 512 : 514];

    ByteBuffer buf = ByteBuffer.wrap(cell);
    buf.order(ByteOrder.BIG_ENDIAN);
    if (protocolVersion < 4)
        buf.putShort((short) circId);
    else
        buf.putInt((int) circId);
    buf.put((byte) cmdId);

    if (cmdId == 7 || cmdId >= 128)
        buf.putShort((short) payload.length);

    if (payload != null)
        buf.put(payload);
    //System.out.println("Sending:" + byteArrayToHex(cell));
    return cell;
}

From source file:org.openhab.binding.ulux.internal.ump.messages.AbstractUluxMessage.java

@Override
public final ByteBuffer getBuffer() {
    final ByteBuffer buffer = ByteBuffer.wrap(new byte[this.length]);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    buffer.put(this.length);
    buffer.put(this.messageId.toByte());
    buffer.putShort(this.actorId);

    addData(buffer);/* ww w  .ja  v  a 2  s  . c o m*/

    buffer.flip();

    return buffer;
}

From source file:io.pcp.parfait.dxm.FileByteBufferFactory.java

public ByteBuffer build(int length) throws IOException {
    RandomAccessFile fos = null;//ww  w .j a  v  a2 s.  com
    try {
        File parent = file.getParentFile();
        if (parent == null) {
            throw new RuntimeException("Could not find parent of output file " + file.getCanonicalPath());
        } else if (parent.exists()) {
            file.delete(); /* directory update visible to MMV PMDA */
            if (file.exists()) {
                throw new RuntimeException("Could not delete existing file " + file.getCanonicalPath());
            }
        } else if (!parent.mkdirs()) {
            throw new RuntimeException("Could not create output directory " + parent.getCanonicalPath());
        }
        fos = new RandomAccessFile(file, "rw");
        fos.setLength(length);
        ByteBuffer tempDataFile = fos.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
        tempDataFile.order(ByteOrder.nativeOrder());
        fos.close();

        return tempDataFile;
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}

From source file:io.github.dsheirer.sample.adapter.ChannelShortAdapter.java

@Override
public float[] convert(byte[] samples) {
    float[] processed = new float[samples.length / 4];

    int pointer = 0;

    /* Wrap byte array in a byte buffer so we can process them as shorts */
    ByteBuffer buffer = ByteBuffer.wrap(samples);

    /* Set endian to correct byte ordering */
    buffer.order(mByteOrder);

    while (buffer.hasRemaining()) {
        if (mMixerChannel == MixerChannel.LEFT) {
            processed[pointer] = mMap.get(buffer.getShort());

            /* Throw away the right channel */
            buffer.getShort();/* w ww . j a  v a 2 s  .c  o m*/
        } else {
            /* Throw away the left channel */
            buffer.getShort();

            processed[pointer] = mMap.get(buffer.getShort());
        }

        pointer++;
    }

    return processed;
}

From source file:com.koda.integ.hbase.util.CacheableSerializer.java

@Override
public Cacheable read(ByteBuffer buf) throws IOException {
    if (deserializer == null || deserializer.get() == null) {
        return null;
    }/*from www. java2  s  .  c  om*/
    ByteBuffer bbuf = buf.slice();
    bbuf.order(buf.order());
    return deserializer.get().deserialize(bbuf);
}

From source file:org.jmangos.sniffer.handler.PKTLogHandler.java

@Override
public void init() {
    final String date = String.format("%X", System.currentTimeMillis());
    final ByteBuffer buffer = ByteBuffer.allocate(66);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put("PKT".getBytes());
    buffer.put((byte) 1);
    buffer.put((byte) 3);
    buffer.put((byte) 12);
    buffer.putInt(this.build);
    buffer.put("xxXX".getBytes());
    buffer.put(this.keyReader.getKey());
    buffer.putInt((int) (System.currentTimeMillis() / 1000L));
    buffer.putInt(0);//from w w w  .  j a  v a 2 s  . c  om
    buffer.putInt(0);
    try {
        this.fous = new DataOutputStream(new FileOutputStream(new File(this.build + "_" + date + ".pkt")));
        this.fous.write(buffer.array());
        setInit(true);
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

From source file:org.ros.android.android_acm_serial.AcmDevice.java

public void setLineCoding(BitRate bitRate, StopBits stopBits, Parity parity, DataBits dataBits) {
    ByteBuffer buffer = ByteBuffer.allocate(7);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(bitRate.getBitRate());
    buffer.put(stopBits.getStopBits());// ww w. j a  v  a2  s  .c  o m
    buffer.put(parity.getParity());
    buffer.put(dataBits.getDataBits());
    setLineCoding(buffer.array());
}

From source file:org.jmangos.sniffer.handler.PKTLogHandler.java

/**
 * (non-Javadoc)/*w  w  w .j a v  a  2  s . c o  m*/
 * 
 * @see org.jmangos.sniffer.handler.PacketLogHandler#onDecodePacket(org.jmangos
 *      .sniffer.network.model.NetworkChannel,
 *      org.jmangos.sniffer.enums.Direction, java.lang.Integer,
 *      java.lang.Integer, byte[], int)
 */
@Override
public void onDecodePacket(final NetworkChannel channel, final Direction direction, final Integer size,
        final Integer opcode, final byte[] data, final int frame) {
    if (!isInit()) {
        init();
    }
    try {
        final ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + 4 + 4 + 4 + data.length + 4);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.put(direction.getValue());
        buffer.putInt(channel.hashCode());
        buffer.putInt(frame);
        buffer.putInt(0);
        buffer.putInt(data.length + 4);
        buffer.putInt(opcode);
        buffer.put(data);
        this.fous.write(buffer.array());
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.cambierr.lorawanpacket.semtech.PullResp.java

public PullResp(byte[] _randoms, ByteBuffer _raw) throws MalformedPacketException {
    super(_randoms, PacketType.PULL_RESP);
    _raw.order(ByteOrder.LITTLE_ENDIAN);

    if (_raw.remaining() < 1) {
        throw new MalformedPacketException("too short");
    }// ww  w.  j a v a2 s . co  m

    byte[] json = new byte[_raw.remaining()];
    _raw.get(json);
    JSONObject jo;

    try {
        jo = new JSONObject(new String(json));
    } catch (JSONException ex) {
        throw new MalformedPacketException("malformed json");
    }

    txpks = new ArrayList<>();

    if (!jo.has("txpk")) {
        throw new MalformedPacketException("missing json (txpk)");
    }

    if (!jo.get("txpk").getClass().equals(JSONArray.class)) {
        throw new MalformedPacketException("malformed json (txpk)");
    }
    JSONArray rxpk = jo.getJSONArray("txpk");

    for (int i = 0; i < rxpk.length(); i++) {
        txpks.add(new Txpk(rxpk.getJSONObject(i)));
    }
}