Example usage for java.nio ByteBuffer putInt

List of usage examples for java.nio ByteBuffer putInt

Introduction

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

Prototype

public abstract ByteBuffer putInt(int value);

Source Link

Document

Writes the given int to the current position and increases the position by 4.

Usage

From source file:com.github.ambry.commons.BlobId.java

@Override
public byte[] toBytes() {
    ByteBuffer idBuf = ByteBuffer.allocate(sizeInBytes());
    idBuf.putShort(version);//from ww  w . j a v  a 2s . co m
    idBuf.put(partitionId.getBytes());
    idBuf.putInt(uuid.getBytes().length);
    idBuf.put(uuid.getBytes());
    return idBuf.array();
}

From source file:org.apache.bookkeeper.replication.BookieLedgerIndexTest.java

/**
 * Verify indexing with multiple ensemble reformation
 *///from   w w w.  ja v a2 s  . co  m
@Test(timeout = 60000)
public void testEnsembleReformation() throws Exception {
    try {
        LedgerHandle lh1 = createAndAddEntriesToLedger();
        LedgerHandle lh2 = createAndAddEntriesToLedger();

        startNewBookie();
        shutdownBookie(bs.size() - 2);

        // add few more entries after ensemble reformation
        for (int i = 0; i < 10; i++) {
            ByteBuffer entry = ByteBuffer.allocate(4);
            entry.putInt(rng.nextInt(Integer.MAX_VALUE));
            entry.position(0);

            entries.add(entry.array());
            lh1.addEntry(entry.array());
            lh2.addEntry(entry.array());
        }

        BookieLedgerIndexer bookieLedgerIndex = new BookieLedgerIndexer(ledgerManager);

        Map<String, Set<Long>> bookieToLedgerIndex = bookieLedgerIndex.getBookieToLedgerIndex();
        assertEquals("Missed few bookies in the bookie-ledger mapping!", 4, bookieToLedgerIndex.size());
        Collection<Set<Long>> bk2ledgerEntry = bookieToLedgerIndex.values();
        for (Set<Long> ledgers : bk2ledgerEntry) {
            assertEquals("Missed few ledgers in the bookie-ledger mapping!", 2, ledgers.size());
            for (Long ledgerNode : ledgers) {
                assertTrue("Unknown ledger-bookie mapping", ledgerList.contains(ledgerNode));
            }
        }
    } catch (BKException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to BookKeeper exception");
    } catch (InterruptedException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to interruption");
    }
}

From source file:com.sm.store.utils.FileStore.java

private boolean checkSignature(FileChannel channel) throws IOException {
    ByteBuffer intBytes = ByteBuffer.allocate(OFFSET);
    if (channel.size() == 0) {
        intBytes.putInt(MAGIC);
        intBytes.flip();/*from   w  w w  .ja v a2 s  .  co m*/
        channel.write(intBytes);
        return true;
    } else {
        channel.read(intBytes);
        intBytes.rewind();
        if (intBytes.getInt() != MAGIC)
            throw new StoreException("Header mismatch expect " + MAGIC + " read " + intBytes.getInt());
    }
    return true;
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6FlowMod.java

/**
 * This method forms the Vendor extension IPv6 Flow Mod message.It uses the
 * fields in V6FlowMod class, and writes the data according to vendor 
 * extension format. The fields include flow properties (cookie, timeout,
 * priority, etc), flow match, and action list. It also takes care of 
 * required padding.// w  ww .  j  ava  2 s . c  om
 */

@Override
public void writeTo(ByteBuffer data) {
    super.writeTo(data);
    data.putInt(getIPv6ExtensionFlowModAddSubType());
    data.putLong(this.cookie);
    data.putShort(command); /* should be OFPFC_ADD, OFPFC_DELETE_STRICT, etc*/
    data.putShort(this.idleTimeout);
    data.putShort(this.hardTimeout);
    data.putShort(this.priority);
    data.putInt(OFPacketOut.BUFFER_ID_NONE);
    data.putShort(outPort); /* output_port */
    data.putShort(flags); /* flags */
    match_len = this.match.getIPv6MatchLen();
    data.putShort(match_len);
    byte[] pad = new byte[6];
    data.put(pad);
    this.match.writeTo(data);

    pad_size = (short) (((match_len + 7) / 8) * 8 - match_len);

    /*
     * action list should be preceded by a padding of 0 to 7 bytes based upon
     * above formula.
     */

    byte[] pad2 = new byte[pad_size];
    data.put(pad2);
    if (actions != null) {
        for (OFAction action : actions) {
            actions_len += action.getLength();
            action.writeTo(data);
        }
    }
    logger.trace("{}", this);
}

From source file:org.apache.hadoop.hbase.filter.TestFuzzyRowAndColumnRangeFilter.java

@Test
public void Test() throws Exception {
    String cf = "f";
    String table = "TestFuzzyAndColumnRangeFilterClient";
    HTable ht = TEST_UTIL.createTable(Bytes.toBytes(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i1 = 0; i1 < 2; i1++) {
        for (int i2 = 0; i2 < 5; i2++) {
            byte[] rk = new byte[10];

            ByteBuffer buf = ByteBuffer.wrap(rk);
            buf.clear();//from   w w w  . j a va  2s .  com
            buf.putShort((short) 2);
            buf.putInt(i1);
            buf.putInt(i2);

            for (int c = 0; c < 5; c++) {
                byte[] cq = new byte[4];
                Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                Put p = new Put(rk);
                p.setDurability(Durability.SKIP_WAL);
                p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                ht.put(p);
                LOG.info("Inserting: rk: " + Bytes.toStringBinary(rk) + " cq: " + Bytes.toStringBinary(cq));
            }
        }
    }

    TEST_UTIL.flush();

    // test passes
    runTest(ht, 0, 10);

    // test fails
    runTest(ht, 1, 8);
}

From source file:com.offbynull.portmapper.natpmp.TcpMappingNatPmpRequest.java

@Override
protected void dumpOpCodeSpecificInformation(ByteBuffer dst) {
    dst.putShort((short) 0);
    dst.putShort((short) internalPort);
    dst.putShort((short) suggestedExternalPort);
    dst.putInt((int) lifetime);
}

From source file:org.mule.util.queue.QueueControlDataFile.java

/**
 * Updates the control data//ww w. j av  a  2  s .  c  o m
 *
 * @param writeFile file that is used for writing
 * @param readFile  file that is used for reading
 */
public void writeControlData(File writeFile, File readFile) {
    try {
        queueFileProvider.getRandomAccessFile().seek(0);
        final String writeFilePath = writeFile.getAbsolutePath();
        final String readFilePath = readFile.getAbsolutePath();
        final ByteBuffer controlDataBuffer = ByteBuffer.allocate(
                writeFilePath.length() + INTEGER_SIZE_IN_BYTES + readFilePath.length() + INTEGER_SIZE_IN_BYTES);
        controlDataBuffer.putInt(writeFilePath.length());
        controlDataBuffer.put(writeFilePath.getBytes());
        controlDataBuffer.putInt(readFilePath.length());
        controlDataBuffer.put(readFilePath.getBytes());
        queueFileProvider.getRandomAccessFile().write(controlDataBuffer.array());
        this.currentReadFilePath = readFile;
        this.currentWriteFilePath = writeFile;
    } catch (IOException e) {
        throw new MuleRuntimeException(e);
    }
}

From source file:org.apache.asterix.experiment.builder.AbstractExperiment8Builder.java

protected String getPointLookUpAQL(int round) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.put((byte) 0);
    bb.put((byte) randGen.nextInt(N_PARTITIONS));
    bb.putShort((short) 0);
    bb.putInt(randGen.nextInt((int) (((1 + round) * dataInterval) / 1000)));
    bb.flip();// w w  w  . j  ava  2s  .c  om
    long key = bb.getLong();
    return pointQueryTemplate.replaceAll("\\$KEY\\$", Long.toString(key));
}

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

/**
 * (non-Javadoc)//from   w w w.java  2  s.c om
 * 
 * @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:net.jenet.CommandHeader.java

public void toBuffer(ByteBuffer buffer) {
    buffer.put(command);/*from   w w  w  .  j  a va2s . co m*/
    buffer.put(channelID);
    buffer.put(flags);
    buffer.put(reserved);
    buffer.putInt(commandLength);
    buffer.putInt(reliableSequenceNumber);
}