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:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verify Advanced asynchronous writing with entryIds in reverse order.
 *//*w w  w .j  av a2  s . c o  m*/
@Test
public void testLedgerCreateAdvWithAsyncWritesWithBookieFailures() throws Exception {
    // Create ledgers
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    lh2 = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);

    LOG.info("Ledger ID-1: " + lh.getId());
    LOG.info("Ledger ID-2: " + lh2.getId());
    SyncObj syncObj1 = new SyncObj();
    SyncObj syncObj2 = new SyncObj();
    for (int i = numEntriesToWrite - 1; i >= 0; i--) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        try {
            entries1.add(0, entry.array());
            entries2.add(0, entry.array());
        } catch (Exception e) {
            e.printStackTrace();
        }
        lh.asyncAddEntry(i, entry.array(), 0, entry.capacity(), this, syncObj1);
        lh2.asyncAddEntry(i, entry.array(), 0, entry.capacity(), this, syncObj2);
    }
    // Start One more bookie and shutdown one from last ensemble before reading
    startNewBookie();
    List<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet().iterator().next()
            .getValue();
    killBookie(ensemble.get(0));

    // Wait for all entries to be acknowledged for the first ledger
    synchronized (syncObj1) {
        while (syncObj1.counter < numEntriesToWrite) {
            syncObj1.wait();
        }
        assertEquals(BKException.Code.OK, syncObj1.rc);
    }
    // Wait for all entries to be acknowledged for the second ledger
    synchronized (syncObj2) {
        while (syncObj2.counter < numEntriesToWrite) {
            syncObj2.wait();
        }
        assertEquals(BKException.Code.OK, syncObj2.rc);
    }

    // Reading ledger till the last entry
    readEntries(lh, entries1);
    readEntries(lh2, entries2);
    lh.close();
    lh2.close();
}

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

private byte[] getIPv6ExtensionProtocolMatchMsg(byte protocol) {
    ByteBuffer ipv6ext_proto_msg = ByteBuffer.allocate(5);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_IP_PROTO.getValue(), 0, 1);
    if (protocol == 0) {
        return null;
    }/*from   w  w w  .j  a  va 2s.c  o  m*/
    ipv6ext_proto_msg.putInt(nxm_header);
    if (protocol == IPProtocols.ICMP.getValue()) {
        /*
         * The front end  passes the same protocol type values for IPv4
         * and IPv6 flows. For the Protocol types we allow in our GUI
         * (ICMP, TCP, UDP), ICMP is the only one which is different for
         * IPv6. It is 1 for v4 and 58 for v6 Therefore, we overwrite it
         * here.
         */
        protocol = IPProtocols.ICMPV6.getValue();
    }
    ipv6ext_proto_msg.put(protocol);
    return (ipv6ext_proto_msg.array());
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verify that LedgerHandleAdv cannnot handle addEntry without the entryId.
 *
 * @throws Exception/*from ww w . ja v  a  2 s . co m*/
 */
@Test
public void testNoAddEntryLedgerCreateAdv() throws Exception {

    ByteBuffer entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);

    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    assertTrue(lh instanceof LedgerHandleAdv);

    try {
        lh.addEntry(entry.array());
        fail("using LedgerHandleAdv addEntry without entryId is forbidden");
    } catch (BKException e) {
        assertEquals(e.getCode(), BKException.Code.IllegalOpException);
    }

    try {
        lh.addEntry(entry.array(), 0, 4);
        fail("using LedgerHandleAdv addEntry without entryId is forbidden");
    } catch (BKException e) {
        assertEquals(e.getCode(), BKException.Code.IllegalOpException);
    }

    try {
        CompletableFuture<Object> done = new CompletableFuture<>();
        lh.asyncAddEntry(Unpooled.wrappedBuffer(entry.array()),
                (int rc, LedgerHandle lh1, long entryId, Object ctx) -> {
                    SyncCallbackUtils.finish(rc, null, done);
                }, null);
        done.get();
    } catch (ExecutionException ee) {
        assertTrue(ee.getCause() instanceof BKException);
        BKException e = (BKException) ee.getCause();
        assertEquals(e.getCode(), BKException.Code.IllegalOpException);
    }

    try {
        CompletableFuture<Object> done = new CompletableFuture<>();
        lh.asyncAddEntry(entry.array(), (int rc, LedgerHandle lh1, long entryId, Object ctx) -> {
            SyncCallbackUtils.finish(rc, null, done);
        }, null);
        done.get();
    } catch (ExecutionException ee) {
        assertTrue(ee.getCause() instanceof BKException);
        BKException e = (BKException) ee.getCause();
        assertEquals(e.getCode(), BKException.Code.IllegalOpException);
    }

    try {
        CompletableFuture<Object> done = new CompletableFuture<>();
        lh.asyncAddEntry(entry.array(), 0, 4, (int rc, LedgerHandle lh1, long entryId, Object ctx) -> {
            SyncCallbackUtils.finish(rc, null, done);
        }, null);
        done.get();
    } catch (ExecutionException ee) {
        assertTrue(ee.getCause() instanceof BKException);
        BKException e = (BKException) ee.getCause();
        assertEquals(e.getCode(), BKException.Code.IllegalOpException);
    }
    lh.close();
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verty delayedWriteError causes ensemble changes.
 *//*from   w ww.j  a va 2  s  .  c  o  m*/
@Test
public void testDelayedWriteEnsembleChange() throws Exception {
    // Create a ledger
    lh = bkc.createLedger(3, 3, 2, digestType, ledgerPassword);
    baseClientConf.setAddEntryTimeout(1);

    int numEntriesToWrite = 10;
    // write-batch-1
    for (int i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }

    CountDownLatch sleepLatch1 = new CountDownLatch(1);

    // get bookie at index-0
    BookieSocketAddress bookie1 = lh.getCurrentEnsemble().get(0);
    sleepBookie(bookie1, sleepLatch1);

    int i = numEntriesToWrite;
    numEntriesToWrite = numEntriesToWrite + 10;

    // write-batch-2

    for (; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }
    // Sleep to receive delayed error on the write directed to the sleeping bookie
    Thread.sleep(baseClientConf.getAddEntryTimeout() * 1000 * 2);
    assertTrue("Stats should have captured a new UnderReplication during write",
            bkc.getTestStatsProvider().getCounter(CLIENT_SCOPE + "." + ADD_OP_UR).get() > 0);

    i = numEntriesToWrite;
    numEntriesToWrite = numEntriesToWrite + 10;

    // write-batch-3
    for (; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }

    sleepLatch1.countDown();
    // get the bookie at index-0 again, this must be different.
    BookieSocketAddress bookie2 = lh.getCurrentEnsemble().get(0);

    assertFalse("Delayed write error must have forced ensemble change", bookie1.equals(bookie2));
    lh.close();
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verify Advanced asynchronous writing with entryIds in pseudo random order.
 *///  ww w  . j  a v  a2s . c o  m
@Test
public void testLedgerCreateAdvWithRandomAsyncWritesWithBookieFailures() throws Exception {
    // Create ledgers
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    lh2 = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);

    LOG.info("Ledger ID-1: " + lh.getId());
    LOG.info("Ledger ID-2: " + lh2.getId());
    SyncObj syncObj1 = new SyncObj();
    SyncObj syncObj2 = new SyncObj();
    int batchSize = 5;
    int i, j;

    // Fill the result buffers first
    for (i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);

        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        try {
            entries1.add(0, entry.array());
            entries2.add(0, entry.array());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    for (i = 0; i < batchSize; i++) {
        for (j = i; j < numEntriesToWrite; j = j + batchSize) {
            byte[] entry1 = entries1.get(j);
            byte[] entry2 = entries2.get(j);
            lh.asyncAddEntry(j, entry1, 0, entry1.length, this, syncObj1);
            lh2.asyncAddEntry(j, entry2, 0, entry2.length, this, syncObj2);
        }
    }
    // Start One more bookie and shutdown one from last ensemble before reading
    startNewBookie();
    List<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet().iterator().next()
            .getValue();
    killBookie(ensemble.get(0));

    // Wait for all entries to be acknowledged for the first ledger
    synchronized (syncObj1) {
        while (syncObj1.counter < numEntriesToWrite) {
            syncObj1.wait();
        }
        assertEquals(BKException.Code.OK, syncObj1.rc);
    }
    // Wait for all entries to be acknowledged for the second ledger
    synchronized (syncObj2) {
        while (syncObj2.counter < numEntriesToWrite) {
            syncObj2.wait();
        }
        assertEquals(BKException.Code.OK, syncObj2.rc);
    }

    // Reading ledger till the last entry
    readEntries(lh, entries1);
    readEntries(lh2, entries2);
    lh.close();
    lh2.close();
}

From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCache.java

/**
 * Store external with codec./*from ww w.j av a 2  s. c om*/
 * Format:
 * 0..3  - total record size (-4)
 * 4..7  - size of a key in bytes (16 if use hash128)
 * 8 .. x - key data
 * x+1 ..x+1- IN_MEMORY flag ( 1- in memory, 0 - not)
 * x+2 ... block, serialized and compressed 
 *
 * @param blockName the block name
 * @param buf the buf
 * @param inMemory the in memory
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void storeExternalWithCodec(String blockName, Cacheable buf, boolean inMemory) throws IOException {
    // If external storage is disable - bail out
    if (overflowExtEnabled == false) {
        return;
    }
    byte[] hashed = Utils.hash128(blockName);

    ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();
    deserializer.set(buf.getDeserializer());

    SerDe serde = extStorageCache.getSerDe();
    Codec codec = extStorageCache.getCompressionCodec();
    buffer.clear();

    buffer.position(4);

    // Save key
    buffer.putInt(hashed.length);
    buffer.put(hashed);
    buffer.put(inMemory ? (byte) 1 : (byte) 0);

    if (buf != null) {
        serde.writeCompressed(buffer, buf, codec);
        int pos = buffer.position();
        buffer.putInt(0, pos - 4);
    }
    buffer.flip();
    StorageHandle handle = storage.storeData(buffer);

    try {
        // WE USE byte array as a key
        extStorageCache.put(hashed, handle.toBytes());
    } catch (Exception e) {
        throw new IOException(e);
    }

}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verify Advanced asynchronous writing with entryIds in pseudo random order with bookie failures between writes.
 *///from  w  w  w. ja va2  s.c o m
@Test
public void testLedgerCreateAdvWithRandomAsyncWritesWithBookieFailuresBetweenWrites() throws Exception {
    // Create ledgers
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    lh2 = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);

    LOG.info("Ledger ID-1: " + lh.getId());
    LOG.info("Ledger ID-2: " + lh2.getId());
    SyncObj syncObj1 = new SyncObj();
    SyncObj syncObj2 = new SyncObj();
    int batchSize = 5;
    int i, j;

    // Fill the result buffers first
    for (i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);

        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        try {
            entries1.add(0, entry.array());
            entries2.add(0, entry.array());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    for (i = 0; i < batchSize; i++) {
        for (j = i; j < numEntriesToWrite; j = j + batchSize) {
            byte[] entry1 = entries1.get(j);
            byte[] entry2 = entries2.get(j);
            lh.asyncAddEntry(j, entry1, 0, entry1.length, this, syncObj1);
            lh2.asyncAddEntry(j, entry2, 0, entry2.length, this, syncObj2);
            if (j == numEntriesToWrite / 2) {
                // Start One more bookie and shutdown one from last ensemble at half-way
                startNewBookie();
                List<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet()
                        .iterator().next().getValue();
                killBookie(ensemble.get(0));
            }
        }
    }

    // Wait for all entries to be acknowledged for the first ledger
    synchronized (syncObj1) {
        while (syncObj1.counter < numEntriesToWrite) {
            syncObj1.wait();
        }
        assertEquals(BKException.Code.OK, syncObj1.rc);
    }
    // Wait for all entries to be acknowledged for the second ledger
    synchronized (syncObj2) {
        while (syncObj2.counter < numEntriesToWrite) {
            syncObj2.wait();
        }
        assertEquals(BKException.Code.OK, syncObj2.rc);
    }

    // Reading ledger till the last entry
    readEntries(lh, entries1);
    readEntries(lh2, entries2);
    lh.close();
    lh2.close();
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

@Test
public void testLedgerHandleAdvFunctionality() throws Exception {
    // Create a ledger
    long ledgerId = 0xABCDEF;
    lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);
    numEntriesToWrite = 3;/*from  w ww. ja va  2  s. c om*/

    ByteBuffer entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    lh.addEntry(0, entry.array());

    // here asyncAddEntry(final long entryId, final byte[] data, final
    // AddCallback cb, final Object ctx) method is
    // called which is not covered in any other testcase
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    CountDownLatch latch = new CountDownLatch(1);
    final int[] returnedRC = new int[1];
    lh.asyncAddEntry(1, entry.array(), new AddCallback() {
        @Override
        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            returnedRC[0] = rc;
            latch.countDown();
        }
    }, latch);
    latch.await();
    assertTrue("Returned code is expected to be OK", returnedRC[0] == BKException.Code.OK);

    // here addEntry is called with incorrect offset and length
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    try {
        lh.addEntry(2, entry.array(), -3, 9);
        fail("AddEntry is called with negative offset and incorrect length,"
                + "so it is expected to throw RuntimeException/IndexOutOfBoundsException");
    } catch (RuntimeException exception) {
        // expected RuntimeException/IndexOutOfBoundsException
    }

    // here addEntry is called with corrected offset and length and it is
    // supposed to succeed
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    lh.addEntry(2, entry.array());

    // LedgerHandle is closed for write
    lh.close();

    // here addEntry is called even after the close of the LedgerHandle, so
    // it is expected to throw exception
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    try {
        lh.addEntry(3, entry.array());
        fail("AddEntry is called after the close of LedgerHandle,"
                + "so it is expected to throw BKLedgerClosedException");
    } catch (BKLedgerClosedException exception) {
    }

    readEntries(lh, entries1);
    bkc.deleteLedger(ledgerId);
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verify asynchronous writing when few bookie failures in last ensemble.
 *//*ww  w.ja v a2s .  c  om*/
@Test
public void testAsyncWritesWithMultipleFailuresInLastEnsemble() throws Exception {
    // Create ledgers
    lh = bkc.createLedger(5, 4, digestType, ledgerPassword);
    lh2 = bkc.createLedger(5, 4, digestType, ledgerPassword);

    LOG.info("Ledger ID-1: " + lh.getId());
    LOG.info("Ledger ID-2: " + lh2.getId());
    for (int i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        entries2.add(entry.array());
        lh.addEntry(entry.array());
        lh2.addEntry(entry.array());
    }
    // Start three more bookies
    startNewBookie();
    startNewBookie();
    startNewBookie();

    // Shutdown three bookies in the last ensemble and continue writing
    List<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet().iterator().next()
            .getValue();
    killBookie(ensemble.get(0));
    killBookie(ensemble.get(1));
    killBookie(ensemble.get(2));

    // adding one more entry to both the ledgers async after multiple bookie
    // failures. This will do asynchronously modifying the ledger metadata
    // simultaneously.
    numEntriesToWrite++;
    ByteBuffer entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries1.add(entry.array());
    entries2.add(entry.array());

    SyncObj syncObj1 = new SyncObj();
    SyncObj syncObj2 = new SyncObj();
    lh.asyncAddEntry(entry.array(), this, syncObj1);
    lh2.asyncAddEntry(entry.array(), this, syncObj2);

    // wait for all entries to be acknowledged for the first ledger
    synchronized (syncObj1) {
        while (syncObj1.counter < 1) {
            LOG.debug("Entries counter = " + syncObj1.counter);
            syncObj1.wait();
        }
        assertEquals(BKException.Code.OK, syncObj1.rc);
    }
    // wait for all entries to be acknowledged for the second ledger
    synchronized (syncObj2) {
        while (syncObj2.counter < 1) {
            LOG.debug("Entries counter = " + syncObj2.counter);
            syncObj2.wait();
        }
        assertEquals(BKException.Code.OK, syncObj2.rc);
    }

    // reading ledger till the last entry
    readEntries(lh, entries1);
    readEntries(lh2, entries2);
    lh.close();
    lh2.close();
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Verify write and Read durability stats.
 */// ww w. j  ava2s.com
@Test
public void testWriteAndReadStats() throws Exception {
    // Create a ledger
    lh = bkc.createLedger(3, 3, 2, digestType, ledgerPassword);

    // write-batch-1
    for (int i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }
    assertTrue("Stats should have captured a new writes",
            bkc.getTestStatsProvider().getOpStatsLogger(CLIENT_SCOPE + "." + ADD_OP).getSuccessCount() > 0);

    CountDownLatch sleepLatch1 = new CountDownLatch(1);
    CountDownLatch sleepLatch2 = new CountDownLatch(1);
    List<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet().iterator().next()
            .getValue();

    sleepBookie(ensemble.get(0), sleepLatch1);

    int i = numEntriesToWrite;
    numEntriesToWrite = numEntriesToWrite + 50;

    // write-batch-2

    for (; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }

    // Let the second bookie go to sleep. This forces write timeout and ensemble change
    // Which will be enough time to receive delayed write failures on the write-batch-2

    sleepBookie(ensemble.get(1), sleepLatch2);
    i = numEntriesToWrite;
    numEntriesToWrite = numEntriesToWrite + 50;

    // write-batch-3

    for (; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }

    assertTrue("Stats should have captured a new UnderReplication during write",
            bkc.getTestStatsProvider().getCounter(CLIENT_SCOPE + "." + ADD_OP_UR).get() > 0);

    sleepLatch1.countDown();
    sleepLatch2.countDown();

    // Replace the bookie with a fake bookie
    ServerConfiguration conf = killBookie(ensemble.get(0));
    BookieWriteLedgerTest.CorruptReadBookie corruptBookie = new BookieWriteLedgerTest.CorruptReadBookie(conf);
    bs.add(startBookie(conf, corruptBookie));
    bsConfs.add(conf);

    i = numEntriesToWrite;
    numEntriesToWrite = numEntriesToWrite + 50;

    // write-batch-4

    for (; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        entries1.add(entry.array());
        lh.addEntry(entry.array());
    }

    readEntries(lh, entries1);
    assertTrue("Stats should have captured DigestMismatch on Read",
            bkc.getTestStatsProvider().getCounter(CLIENT_SCOPE + "." + READ_OP_DM).get() > 0);
    lh.close();
}