Example usage for java.nio ByteBuffer putShort

List of usage examples for java.nio ByteBuffer putShort

Introduction

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

Prototype

public abstract ByteBuffer putShort(short value);

Source Link

Document

Writes the given short to the current position and increases the position by 2.

Usage

From source file:ConversionUtil.java

public static byte[] convertToByteArray(short value) {

    byte[] bytes = new byte[2];
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.putShort(value);
    return buffer.array();
    // buffer.get(bytes);
    /*/*from  w w w . j ava  2s  .c  o  m*/
    for (int i =0;i<bytes.length; ++i) {
    int offset = (bytes.length -i-1) *8;
    bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
     */
    // return bytes;

}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, short data) {
    if (buffer == null) {
        return buffer;
    }//from   www.  j  a va  2 s. c  o m
    // sizeof(int) == 2
    while (buffer.capacity() < buffer.position() + 2 - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.putShort(data);
    return buffer;
}

From source file:us.ihmc.idl.CDR.java

public static void writeEncapsulation(SerializedPayload payload) {
    ByteBuffer buf = payload.getData();
    //Write encapsulation
    buf.put((byte) 0x0);
    buf.put((byte) payload.getEncapsulation());
    buf.putShort(options);
}

From source file:tor.HiddenService.java

public static void sendIntroduce(TorSocket sock, String onion, TorCircuit rendz) throws IOException {
    log.debug("Fetching Hidden Service Descriptor");
    String hsdescTxt = fetchHSDescriptor(sock, onion);
    OnionRouter rendzOR = rendz.getLastHop().router;

    // parse the hidden service descriptor
    TorDocumentParser hsdesc = new TorDocumentParser(hsdescTxt);
    //decode the intro points
    String intopointsb64 = new String(Base64.decode(hsdesc.map.get("introduction-points")));
    // parse intro points document
    TorDocumentParser intros = new TorDocumentParser(intopointsb64);
    // get first intro point
    String introPointIdentities[] = intros.getArrayItem("introduction-point");

    int introPointNum = 0;

    String ip0 = Hex.encodeHexString(new Base32().decode(introPointIdentities[introPointNum].toUpperCase()));
    OnionRouter ip0or = Consensus.getConsensus().routers.get(ip0);
    byte[] serviceKey = Base64.decode(intros.getArrayItem("service-key")[introPointNum]);
    byte skHash[] = TorCrypto.getSHA1().digest(serviceKey);
    assert (skHash.length == 20);
    log.debug("Using Intro Point: {}, building circuit...", ip0or);

    TorCircuit ipcirc = sock.createCircuit(true);
    ipcirc.create();//from ww  w  .j  a  v a2s . co  m
    ipcirc.extend(ip0or);

    // outer packet
    ByteBuffer buf = ByteBuffer.allocate(1024);
    buf.put(skHash); // service PKhash

    // inner handshake
    ByteBuffer handshake = ByteBuffer.allocate(1024);
    handshake.put((byte) 2); //ver
    handshake.put(rendzOR.ip.getAddress()); // rendz IP addr
    handshake.putShort((short) rendzOR.orport);
    try {
        handshake.put(new Hex().decode(rendzOR.identityhash.getBytes()));
    } catch (DecoderException e) {
        e.printStackTrace();
    }
    handshake.putShort((short) rendzOR.onionKeyRaw.length); // rendz key len
    handshake.put(rendzOR.onionKeyRaw); // rendz key
    handshake.put(rendz.rendezvousCookie); //rend cookie

    // tap handshake / create handshake
    byte priv_x[] = new byte[128];
    TorCrypto.rnd.nextBytes(priv_x); // g^x
    rendz.temp_x = TorCrypto.byteToBN(priv_x);
    rendz.temp_r = null;

    BigInteger pubKey = TorCrypto.DH_G.modPow(rendz.temp_x, TorCrypto.DH_P);
    byte pubKeyByte[] = TorCrypto.BNtoByte(pubKey);
    handshake.put(pubKeyByte);

    handshake.flip();

    // convert to byte array
    byte handshakeBytes[] = new byte[handshake.remaining()];
    handshake.get(handshakeBytes);

    // encrypt handshake
    PublicKey skPK = TorCrypto.asn1GetPublicKey(serviceKey);
    buf.put(TorCrypto.hybridEncrypt(handshakeBytes, skPK));

    buf.flip();
    byte introcell[] = new byte[buf.remaining()];
    buf.get(introcell);

    ipcirc.send(introcell, TorCircuit.RELAY_COMMAND_INTRODUCE1, false, (short) 0);
    log.debug("waiting for introduce acknowledgement");
    ipcirc.waitForState(TorCircuit.STATES.INTRODUCED, false);

    log.debug("Now waiting for rendezvous connect");
    rendz.waitForState(TorCircuit.STATES.RENDEZVOUS_COMPLETE, false);

    ipcirc.destroy(); // no longer needed
    log.debug("Hidden Service circuit built");
}

From source file:io.github.dsheirer.record.wave.WaveWriter.java

/**
 * Creates an audio format chunk//w w w  . java 2  s  . c  om
 */
public static ByteBuffer getFormatChunk(AudioFormat format) {
    ByteBuffer header = ByteBuffer.allocate(24).order(ByteOrder.LITTLE_ENDIAN);

    //Format descriptor
    header.put(FORMAT_CHUNK_ID.getBytes());
    header.putInt(FORMAT_CHUNK_LENGTH);
    header.putShort(FORMAT_UNCOMPRESSED_PCM);
    header.putShort((short) format.getChannels());
    header.putInt((int) format.getSampleRate());

    //Byte Rate = sample rate * channels * bits per sample / 8
    int frameByteRate = format.getChannels() * format.getSampleSizeInBits() / 8;
    int byteRate = (int) (format.getSampleRate() * frameByteRate);
    header.putInt(byteRate);

    //Block Align
    header.putShort((short) frameByteRate);

    //Bits per Sample
    header.putShort((short) format.getSampleSizeInBits());

    //Reset the buffer pointer to 0
    header.position(0);

    return header;
}

From source file:com.DorsetEggs.waver.communicatorService.java

public static byte[] toByteArray(KeyframePacket obj) throws IOException {
    ByteBuffer bytes = ByteBuffer.allocate(4);
    bytes.order(ByteOrder.LITTLE_ENDIAN);
    bytes.put(obj.servoToApplyTo);/* ww  w  .  j  a  va2 s  . co  m*/
    bytes.put(obj.degreesToReach);
    bytes.putShort(obj.timeToPosition);
    return bytes.array();
}

From source file:org.midonet.netlink.rtnetlink.Link.java

static public ByteBuffer describeGetRequest(ByteBuffer buf, int index) {
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.putShort((short) 0);
    buf.putInt(index);//from   w w  w  .j  a  va  2  s  . c  o  m
    buf.putInt(0);
    buf.putInt(0xffffffff);

    return buf;
}

From source file:org.midonet.netlink.rtnetlink.Link.java

static public ByteBuffer describeGetRequest(ByteBuffer buf, String ifname) {
    buf.put((byte) 0);
    buf.put((byte) 0);
    buf.putShort((short) 0);
    buf.putInt(0);// w  ww  .j a va  2 s. com
    buf.putInt(0);
    buf.putInt(0xffffffff);

    NetlinkMessage.writeStringAttr(buf, Attr.IFLA_IFNAME, ifname);

    return buf;
}

From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java

private static byte[] addWavHeader(byte[] bytes) throws IOException {

    ByteBuffer bufferWithHeader = ByteBuffer.allocate(bytes.length + 44);
    bufferWithHeader.order(ByteOrder.LITTLE_ENDIAN);
    bufferWithHeader.put("RIFF".getBytes());
    bufferWithHeader.putInt(bytes.length + 36);
    bufferWithHeader.put("WAVE".getBytes());
    bufferWithHeader.put("fmt ".getBytes());
    bufferWithHeader.putInt(16);/* w ww  .j av  a2  s  .  c om*/
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putShort((short) 1);
    bufferWithHeader.putInt(16000);
    bufferWithHeader.putInt(32000);
    bufferWithHeader.putShort((short) 2);
    bufferWithHeader.putShort((short) 16);
    bufferWithHeader.put("data".getBytes());
    bufferWithHeader.putInt(bytes.length);
    bufferWithHeader.put(bytes);
    return bufferWithHeader.array();
}

From source file:org.midonet.netlink.rtnetlink.Link.java

static public ByteBuffer describeDelRequest(ByteBuffer buf, Link link) {
    IfinfoMsg ifi = link.ifi;/* w w w  .j  av a2  s .c o  m*/
    buf.put(ifi.family);
    buf.put((byte) 0);
    buf.putShort(ifi.type);
    buf.putInt(ifi.index);
    buf.putInt(ifi.flags);
    buf.putInt(ifi.change);

    NetlinkMessage.writeAttrNested(buf, Attr.IFLA_LINKINFO, link.info);

    if (link.ifname != null) {
        NetlinkMessage.writeStringAttr(buf, Attr.IFLA_IFNAME, link.ifname);
    }

    return buf;
}