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:de.hpi.fgis.hdrs.Triple.java

public static void writeTripleHeader(ByteBuffer out, Triple t) {
    out.putShort(t.getSubjectLength());
    out.putShort(t.getPredicateLength());
    out.putInt(t.getObjectLength());/*from   w w  w  .  ja  va2 s .c om*/
    out.putInt(t.getMultiplicity());
}

From source file:com.sm.connector.server.ServerStore.java

public void writeExistBlock(CacheBlock<byte[]> block) throws IOException {
    ByteBuffer dataBuf = ByteBuffer.wrap(block.getData());
    // don't use flip() before it write when use wrap
    //dataBuf.flip();
    dataChannel.write(dataBuf, block.getDataOffset());
    long pos = OFFSET + (long) block.getRecordNo() * RECORD_SIZE;
    int off = 9;/*from w w w  . j  a v a  2s.c  o m*/
    ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE - off);
    buf.putLong(block.getDataOffset2Len());
    buf.putLong(block.getBlock2Version());
    buf.putShort(block.getNode());
    // must flip() before it write
    buf.flip();
    indexChannel.write(buf, pos + off);
}

From source file:de.hpi.fgis.hdrs.Triple.java

public static void writeTriple(ByteBuffer buffer, Triple t) {
    buffer.putShort(t.getSubjectLength());
    buffer.putShort(t.getPredicateLength());
    buffer.putInt(t.getObjectLength());/*from   w w  w.j  a v  a  2 s  . co  m*/
    buffer.putInt(t.getMultiplicity());
    if (0 < t.getSubjectLength())
        buffer.put(t.getBuffer(), t.getOffset(), t.getSubjectLength());
    if (0 < t.getPredicateLength())
        buffer.put(t.getBuffer(), t.getPredicateOffset(), t.getPredicateLength());
    if (0 < t.getObjectLength())
        buffer.put(t.getBuffer(), t.getObjectOffset(), t.getObjectLength());
}

From source file:voldemort.store.cachestore.impl.LogChannel.java

private void writeIndexBlock(long keyOffset2Len, int record, long dataOffset2Len, short nodeId,
        long block2Version, byte status) throws IOException {
    long pos = OFFSET + (long) record * RECORD_SIZE;
    checkFileSize(pos, RECORD_SIZE);/* w  ww.  j a  va 2  s  . c o  m*/
    ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
    buf.put(status);
    buf.putLong(keyOffset2Len);
    buf.putLong(dataOffset2Len);
    buf.putLong(block2Version);
    buf.putShort(nodeId);
    buf.flip();
    getIndexChannel().write(buf, pos);
}

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

@Override
protected void addData(final ByteBuffer buffer) {
    BigInteger initFlags = BigInteger.valueOf(0);

    if (this.reset) {
        initFlags = initFlags.setBit(15);
        initFlags = initFlags.setBit(14);
        initFlags = initFlags.setBit(13);
        initFlags = initFlags.setBit(12);
    }/*w  w  w. j  ava2  s.co  m*/
    if (this.initRequest) {
        initFlags = initFlags.setBit(6);
    }
    if (this.timeRequest) {
        initFlags = initFlags.setBit(5);
    }

    buffer.putShort(initFlags.shortValue());
    buffer.putShort((short) 0x5AA5);
}

From source file:au.org.ala.delta.io.BinFile.java

public void writeShort(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort(value);
    writeBytes(buffer);/*from  www . ja va  2  s .  c o  m*/
}

From source file:com.sm.connector.server.ServerStore.java

private void writeIndexBlock(CacheBlock<byte[]> block, long keyOffset2Len, FileChannel channel)
        throws IOException {
    long pos = OFFSET + (long) block.getRecordNo() * RECORD_SIZE;
    checkFileSize(pos, RECORD_SIZE);/*from  ww  w  . j a v a2  s  . c o  m*/
    ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
    buf.put(block.getStatus());
    buf.putLong(keyOffset2Len);
    buf.putLong(block.getDataOffset2Len());
    buf.putLong(block.getBlock2Version());
    buf.putShort(block.getNode());
    buf.flip();
    channel.write(buf, pos);
}

From source file:org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.java

public byte[] convertIpv6HeaderToByte(Ipv6Header ip6Pdu) {
    byte[] data = new byte[128];
    Arrays.fill(data, (byte) 0);

    ByteBuffer buf = ByteBuffer.wrap(data);
    long flowLabel = (((long) (ip6Pdu.getVersion() & 0x0f) << 28) | (ip6Pdu.getFlowLabel() & 0x0fffffff));
    buf.putInt((int) flowLabel);
    buf.putShort((short) ip6Pdu.getIpv6Length().intValue());
    buf.put((byte) ip6Pdu.getNextHeader().shortValue());
    buf.put((byte) ip6Pdu.getHopLimit().shortValue());
    try {/*from  w  ww .  ja v  a2s.c o  m*/
        byte[] baddr = InetAddress.getByName(ip6Pdu.getSourceIpv6().getValue()).getAddress();
        buf.put(baddr);
        baddr = InetAddress.getByName(ip6Pdu.getDestinationIpv6().getValue()).getAddress();
        buf.put(baddr);
    } catch (UnknownHostException e) {
        LOG.error("convertIpv6HeaderToByte: Failed to serialize src, dest address", e);
    }
    return data;
}

From source file:org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.java

public byte[] convertEthernetHeaderToByte(EthernetHeader ethPdu) {
    byte[] data = new byte[16];
    Arrays.fill(data, (byte) 0);

    ByteBuffer buf = ByteBuffer.wrap(data);
    buf.put(bytesFromHexString(ethPdu.getDestinationMac().getValue().toString()));
    buf.put(bytesFromHexString(ethPdu.getSourceMac().getValue().toString()));
    buf.putShort((short) ethPdu.getEthertype().intValue());
    return data;//from   w w w  .j a  v a 2s.c om
}

From source file:com.github.ambry.utils.UtilsTest.java

@Test
public void testReadStrings() throws IOException {
    // good case//  w w w . j a  va 2  s . c o m
    ByteBuffer buffer = ByteBuffer.allocate(10);
    buffer.putShort((short) 8);
    String s = getRandomString(8);
    buffer.put(s.getBytes());
    buffer.flip();
    String outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer)));
    Assert.assertEquals(s, outputString);
    // 0-length
    buffer.rewind();
    buffer.putShort(0, (short) 0);
    outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer)));
    Assert.assertTrue(outputString.isEmpty());

    // failed case
    buffer.rewind();
    buffer.putShort((short) 10);
    buffer.put(s.getBytes());
    buffer.flip();
    try {
        outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer)));
        Assert.assertTrue(false);
    } catch (IllegalArgumentException e) {
        Assert.assertTrue(true);
    }
    buffer.rewind();
    buffer.putShort(0, (short) -1);
    try {
        outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer)));
        Assert.fail("Should have encountered exception with negative length.");
    } catch (IllegalArgumentException e) {
    }

    // good case
    buffer = ByteBuffer.allocate(40004);
    buffer.putInt(40000);
    s = getRandomString(40000);
    buffer.put(s.getBytes());
    buffer.flip();
    outputString = Utils.readIntString(new DataInputStream(new ByteBufferInputStream(buffer)),
            StandardCharsets.UTF_8);
    Assert.assertEquals(s, outputString);
    // 0-length
    buffer.rewind();
    buffer.putInt(0, 0);
    outputString = Utils.readShortString(new DataInputStream(new ByteBufferInputStream(buffer)));
    Assert.assertTrue(outputString.isEmpty());

    // failed case
    buffer.rewind();
    buffer.putInt(50000);
    buffer.put(s.getBytes());
    buffer.flip();
    try {
        outputString = Utils.readIntString(new DataInputStream(new ByteBufferInputStream(buffer)),
                StandardCharsets.UTF_8);
        fail("Should have failed");
    } catch (IllegalArgumentException e) {
        // expected.
    }

    buffer.rewind();
    buffer.putInt(0, -1);
    try {
        Utils.readIntString(new DataInputStream(new ByteBufferInputStream(buffer)), StandardCharsets.UTF_8);
        Assert.fail("Should have encountered exception with negative length.");
    } catch (IllegalArgumentException e) {
        // expected.
    }
}