Example usage for java.nio ByteBuffer capacity

List of usage examples for java.nio ByteBuffer capacity

Introduction

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

Prototype

public final int capacity() 

Source Link

Document

Returns the capacity of this buffer.

Usage

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

private void readEntries(LedgerHandle lh, List<byte[]> entries) throws InterruptedException, BKException {
    ls = lh.readEntries(0, numEntriesToWrite - 1);
    int index = 0;
    while (ls.hasMoreElements()) {
        ByteBuffer origbb = ByteBuffer.wrap(entries.get(index++));
        Integer origEntry = origbb.getInt();
        ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
        LOG.debug("Length of result: " + result.capacity());
        LOG.debug("Original entry: " + origEntry);
        Integer retrEntry = result.getInt();
        LOG.debug("Retrieved entry: " + retrEntry);
        assertTrue("Checking entry " + index + " for equality", origEntry.equals(retrEntry));
    }/*w w w. jav  a 2  s.c  o  m*/
}

From source file:com.turbospaces.spaces.SpaceReceiveAdapter.java

@Override
public void receive(final Message msg) {
    final byte[] data = msg.getBuffer();
    final ObjectBuffer objectBuffer = new ObjectBuffer(jSpace.getSpaceConfiguration().getKryo());
    final Address nodeRaised = msg.getSrc();

    final MethodCall methodCall = (MethodCall) objectBuffer.readClassAndObject(data);
    final short id = methodCall.getMethodId();

    logger.debug("received {} from {}", methodCall, nodeRaised);

    if (id == SpaceMethodsMapping.BEGIN_TRANSACTION.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override//from   w  ww. j  ava2 s  .  co m
            public void run() {
                /**
                 * 1. read transaction timeout
                 * 2. create local durable transaction for remote client and assign id
                 * 3. propagate remote transaction timeout and apply to local transaction
                 * 4. send transaction id back to client
                 */
                BeginTransactionMethodCall beginTransactionMethodCall = (BeginTransactionMethodCall) methodCall;
                long transactionTimeout = beginTransactionMethodCall.getTransactionTimeout();
                SpaceTransactionHolder spaceTransactionHolder = new SpaceTransactionHolder();
                spaceTransactionHolder.setSynchronizedWithTransaction(true);
                spaceTransactionHolder.setTimeoutInMillis(transactionTimeout);
                TransactionModificationContext mc = new TransactionModificationContext();
                mc.setProxyMode(true);
                spaceTransactionHolder.setModificationContext(mc);
                modificationContextFor(nodeRaised).put(mc.getTransactionId(), spaceTransactionHolder);
                methodCall.setResponseBody(objectBuffer.writeObjectData(mc.getTransactionId()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.COMMIT_TRANSACTION.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                CommitRollbackMethodCall commitMethodCall = (CommitRollbackMethodCall) methodCall;
                try {
                    SpaceTransactionHolder th = modificationContextFor(nodeRaised)
                            .getIfPresent(commitMethodCall.getTransactionId());
                    Preconditions.checkState(th != null, "unable to find transaction with id = %s for commit",
                            commitMethodCall.getTransactionId());
                    jSpace.syncTx(th.getModificationContext(), true);
                } finally {
                    durableTransactions.remove(commitMethodCall.getTransactionId());
                }
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.ROLLBACK_TRANSACTION.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                CommitRollbackMethodCall commitMethodCall = (CommitRollbackMethodCall) methodCall;
                try {
                    SpaceTransactionHolder th = modificationContextFor(nodeRaised)
                            .getIfPresent(commitMethodCall.getTransactionId());
                    Preconditions.checkState(th != null, "unable to find transaction with id = %s for rollback",
                            commitMethodCall.getTransactionId());
                    jSpace.syncTx(th.getModificationContext(), false);
                } finally {
                    durableTransactions.remove(commitMethodCall.getTransactionId());
                }
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.WRITE.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                WriteMethodCall writeMethodCall = (WriteMethodCall) methodCall;
                byte[] entityAndClassData = writeMethodCall.getEntity();
                ByteBuffer byteBuffer = ByteBuffer.wrap(entityAndClassData);

                int modifiers = writeMethodCall.getModifiers();
                int timeout = writeMethodCall.getTimeout();
                int timeToLive = writeMethodCall.getTimeToLive();

                /**
                 * 1. read registered class (without actual data)
                 * 2. get the actual type of the remote entry
                 * 3. copy serialized entry state from the byte buffer, omit redundant serialization later
                 * 4. find appropriate transaction modification context if any
                 * 5. call write method itself
                 */
                RegisteredClass entryClass = jSpace.getSpaceConfiguration().getKryo().readClass(byteBuffer);
                Class<?> entryType = entryClass.getType();
                byte[] entityData = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(),
                        byteBuffer.capacity());
                Object entry = jSpace.getSpaceConfiguration().getKryo().readObjectData(byteBuffer, entryType);

                SpaceTransactionHolder holder = null;
                if (writeMethodCall.getTransactionId() != 0)
                    holder = modificationContextFor(nodeRaised)
                            .getIfPresent(writeMethodCall.getTransactionId());

                jSpace.write(holder, entry, entityData, timeToLive, timeout, modifiers);
                writeMethodCall.reset();
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.FETCH.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                FetchMethodCall fetchMethodCall = (FetchMethodCall) methodCall;
                byte[] entityData = fetchMethodCall.getEntity();

                int originalModifiers = fetchMethodCall.getModifiers();
                int timeout = fetchMethodCall.getTimeout();
                int maxResults = fetchMethodCall.getMaxResults();
                Object template = objectBuffer.readClassAndObject(entityData);
                int modifiers = originalModifiers | JSpace.RETURN_AS_BYTES;

                SpaceTransactionHolder holder = null;
                if (fetchMethodCall.getTransactionId() != 0)
                    holder = modificationContextFor(nodeRaised)
                            .getIfPresent(fetchMethodCall.getTransactionId());

                ByteBuffer[] buffers = (ByteBuffer[]) jSpace.fetch(holder, template, timeout, maxResults,
                        modifiers);
                if (buffers != null) {
                    byte[][] response = new byte[buffers.length][];
                    for (int i = 0; i < buffers.length; i++) {
                        ByteBuffer buffer = buffers[i];
                        response[i] = buffer.array();
                    }
                    fetchMethodCall.setResponseBody(objectBuffer.writeObjectData(response));
                }
                fetchMethodCall.reset();
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.NOTIFY.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                NotifyListenerMethodCall registerMethodCall = (NotifyListenerMethodCall) methodCall;
                byte[] entityData = registerMethodCall.getEntity();

                int originalModifiers = registerMethodCall.getModifiers();
                Object template = objectBuffer.readClassAndObject(entityData);
                int modifiers = originalModifiers | JSpace.RETURN_AS_BYTES;
                jSpace.notify(template, new SpaceNotificationListener() {

                    @Override
                    public void handleNotification(final Object entity, final SpaceOperation operation) {
                        ObjectBuffer innerObjectBuffer = new ObjectBuffer(
                                jSpace.getSpaceConfiguration().getKryo());
                        sendResponseBackAfterExecution(methodCall, new Runnable() {
                            @Override
                            public void run() {
                                NotifyListenerMethodCall methodCall = new NotifyListenerMethodCall();
                                methodCall.setEntity(((ByteBuffer) entity).array());
                                methodCall.setOperation(operation);
                            }
                        }, nodeRaised, innerObjectBuffer);
                    }
                }, modifiers);
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.SIZE.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(objectBuffer.writeObjectData(jSpace.size()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.MB_USED.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(objectBuffer.writeObjectData(jSpace.mbUsed()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.EVICT_ELEMENTS.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                EvictElementsMethodCall evictElementsMethodCall = (EvictElementsMethodCall) methodCall;
                methodCall.setResponseBody(objectBuffer
                        .writeObjectData(jSpace.evictElements(evictElementsMethodCall.getElements())));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.EVICT_PERCENTAGE.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                EvictPercentageMethodCall evictPercentageMethodCall = (EvictPercentageMethodCall) methodCall;
                methodCall.setResponseBody(objectBuffer
                        .writeObjectData(jSpace.evictPercentage(evictPercentageMethodCall.getPercentage())));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.EVICT_ALL.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(objectBuffer.writeObjectData(jSpace.evictAll()));
            }
        }, nodeRaised, objectBuffer);
    else if (id == SpaceMethodsMapping.SPACE_TOPOLOGY.ordinal())
        sendResponseBackAfterExecution(methodCall, new Runnable() {
            @Override
            public void run() {
                methodCall.setResponseBody(
                        objectBuffer.writeObjectData(jSpace.getSpaceConfiguration().getTopology()));
            }
        }, nodeRaised, objectBuffer);
}

From source file:com.koda.integ.hbase.storage.FileExtStorage.java

/**
 * Stores multiple objects in one transaction
 * Format of a buffer://from   w ww .  jav a2 s .  c  om
 * 0..3 - total size of a batch
 * 4.. - batch of blocks
 *
 * @param buf the buf
 * @return the list
 */
public List<StorageHandle> storeDataBatch(ByteBuffer buf) {
    List<StorageHandle> handles = storeDataNoReleaseLock(buf);
    if (handles == null) {

        handles = new ArrayList<StorageHandle>();

        int size = buf.getInt(0);
        buf.position(4);

        while (buf.position() < size + 4) {
            buf.limit(buf.capacity());
            StorageHandle fsh = storeData(buf);
            handles.add(fsh);
        }
    }
    return handles;

}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

private void writeIndexMap() throws IOException {
    //Write 4 byte header, 4 byte number of entries, and 20 bytes for each entry
    int numMappings = indexMap_.size();
    ByteBuffer buffer = ByteBuffer.allocate(8 + 20 * numMappings).order(BYTE_ORDER);
    buffer.putInt(0, INDEX_MAP_HEADER);/*from  ww  w. j  a  va 2  s  .c o m*/
    buffer.putInt(4, numMappings);
    int position = 2;
    for (String label : indexMap_.keySet()) {
        String[] indecies = label.split("_");
        for (String index : indecies) {
            buffer.putInt(4 * position, Integer.parseInt(index));
            position++;
        }
        buffer.putInt(4 * position, indexMap_.get(label).intValue());
        position++;
    }
    fileChannel_.write(buffer, filePosition_);

    ByteBuffer header = ByteBuffer.allocate(8).order(BYTE_ORDER);
    header.putInt(0, INDEX_MAP_OFFSET_HEADER);
    header.putInt(4, (int) filePosition_);
    fileChannel_.write(header, 8);
    filePosition_ += buffer.capacity();
}

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

/**
 * LedgerHandleAdv out of order writers with ensemble changes.
 * Verify that entry that was written to old ensemble will be
 * written to new enseble too after ensemble change.
 *
 * @throws Exception/*  w  w w  .java2 s  .co m*/
 */
@Test
public void testLedgerHandleAdvOutOfOrderWriteAndFrocedEnsembleChange() throws Exception {
    // Create a ledger
    long ledgerId = 0xABCDEF;
    SyncObj syncObj1 = new SyncObj();
    ByteBuffer entry;
    lh = bkc.createLedgerAdv(ledgerId, 3, 3, 3, digestType, ledgerPassword, null);
    entry = ByteBuffer.allocate(4);
    // Add entries 0-4
    for (int i = 0; i < 5; i++) {
        entry.rewind();
        entry.putInt(rng.nextInt(maxInt));
        lh.addEntry(i, entry.array());
    }

    // Add 10 as Async Entry, which goes to first ensemble
    ByteBuffer entry1 = ByteBuffer.allocate(4);
    entry1.putInt(rng.nextInt(maxInt));
    lh.asyncAddEntry(10, entry1.array(), 0, entry1.capacity(), this, syncObj1);

    // Make sure entry-10 goes to the bookies and gets response.
    java.util.Queue<PendingAddOp> myPendingAddOps = Whitebox.getInternalState(lh, "pendingAddOps");
    PendingAddOp addOp = null;
    boolean pendingAddOpReceived = false;

    while (!pendingAddOpReceived) {
        addOp = myPendingAddOps.peek();
        if (addOp.entryId == 10 && addOp.completed) {
            pendingAddOpReceived = true;
        } else {
            Thread.sleep(1000);
        }
    }

    CountDownLatch sleepLatch1 = new CountDownLatch(1);
    List<BookieSocketAddress> ensemble;

    ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet().iterator().next().getValue();

    // Put all 3 bookies to sleep and start 3 new ones
    sleepBookie(ensemble.get(0), sleepLatch1);
    sleepBookie(ensemble.get(1), sleepLatch1);
    sleepBookie(ensemble.get(2), sleepLatch1);
    startNewBookie();
    startNewBookie();
    startNewBookie();

    // Original bookies are in sleep, new bookies added.
    // Now add entries 5-9 which forces ensemble changes
    // So at this point entries 0-4, 10 went to first
    // ensemble, 5-9 will go to new ensemble.
    for (int i = 5; i < 10; i++) {
        entry.rewind();
        entry.putInt(rng.nextInt(maxInt));
        lh.addEntry(i, entry.array());
    }

    // Wakeup all 3 bookies that went to sleep
    sleepLatch1.countDown();

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

    // Close write handle
    lh.close();

    // Open read handle
    lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);

    // Make sure to read all 10 entries.
    for (int i = 0; i < 11; i++) {
        lh.readEntries(i, i);
    }
    lh.close();
    bkc.deleteLedger(ledgerId);
}

From source file:x10.x10rt.yarn.ApplicationMaster.java

protected void handleX10() {
    // handle X10 place requests
    Iterator<SelectionKey> events = null;
    while (running) {
        try {//w  ww . j  a  v  a2s . co m
            SelectionKey key;
            // check for previously unhandled events
            if (events != null && events.hasNext()) {
                key = events.next();
                events.remove();
            } else if (selector.select() == 0) // check for new events
                continue; // nothing to process, go back and block on select again
            else { // select returned some events
                events = selector.selectedKeys().iterator();
                key = events.next();
                events.remove();
            }

            // process the selectionkey
            if (key.isAcceptable()) {
                LOG.info("New connection from X10 detected");
                // accept any connections on the server socket, and look for things to read from it
                ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                SocketChannel sc = ssc.accept();
                sc.configureBlocking(false);
                sc.register(selector, SelectionKey.OP_READ);
            }
            if (key.isReadable()) {
                SocketChannel sc = (SocketChannel) key.channel();

                ByteBuffer incomingMsg;
                if (pendingReads.containsKey(sc))
                    incomingMsg = pendingReads.remove(sc);
                else
                    incomingMsg = ByteBuffer.allocateDirect(headerLength).order(ByteOrder.nativeOrder());

                LOG.info("Reading message from X10");
                try {
                    if (sc.read(incomingMsg) == -1) {
                        // socket closed
                        sc.close();
                        key.cancel();
                        pendingReads.remove(sc);
                    } else if (incomingMsg.hasRemaining()) {
                        LOG.info("Message header partially read. " + incomingMsg.remaining()
                                + " bytes remaining");
                        pendingReads.put(sc, incomingMsg);
                    } else { // buffer is full
                        if (incomingMsg.capacity() == headerLength) {
                            // check to see if there is a body associated with this message header
                            int datalen = incomingMsg.getInt(headerLength - 4);
                            //System.err.println("Byte order is "+incomingMsg.order()+" datalen="+datalen);
                            if (datalen == 0)
                                processMessage(incomingMsg, sc);
                            else { // create a larger array to hold the header+body
                                ByteBuffer newBuffer = ByteBuffer.allocateDirect(headerLength + datalen)
                                        .order(ByteOrder.nativeOrder());
                                incomingMsg.rewind();
                                newBuffer.put(incomingMsg);
                                incomingMsg = newBuffer;
                                sc.read(incomingMsg); // read in the body, if available
                                if (incomingMsg.hasRemaining()) {
                                    LOG.info("Message partially read. " + incomingMsg.remaining()
                                            + " bytes remaining");
                                    pendingReads.put(sc, incomingMsg);
                                } else
                                    processMessage(incomingMsg, sc);
                            }
                        }
                    }
                } catch (IOException e) {
                    LOG.warn("Error reading in message from socket channel", e);
                }
            }
        } catch (IOException e) {
            LOG.warn("Error handling X10 links", e);
        }
    }
}

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

/**
 * Verify the functionality LedgerHandleAdv asyncAddEntry with duplicate
 * entryIds.//w w  w.  j ava  2s .c o  m
 *
 * @throws Exception
 */
@Test
public void testLedgerCreateAdvSyncAsyncAddDuplicateEntryIds() throws Exception {
    long ledgerId;
    SyncObj syncObj1 = new SyncObj();
    SyncObj syncObj2 = new SyncObj();

    // Create a ledger
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    // Save ledgerId to reopen the ledger
    ledgerId = lh.getId();
    LOG.info("Ledger ID: " + ledgerId);
    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());
        } catch (Exception e) {
            e.printStackTrace();
        }
        lh.asyncAddEntry(i, entry.array(), 0, entry.capacity(), this, syncObj1);
        if (rng.nextBoolean()) {
            // Attempt to write the same entry
            lh.asyncAddEntry(i, entry.array(), 0, entry.capacity(), this, syncObj2);
            synchronized (syncObj2) {
                while (syncObj2.counter < 1) {
                    syncObj2.wait();
                }
                assertEquals(BKException.Code.DuplicateEntryIdException, syncObj2.rc);
            }
        }
    }
    // 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);
    }
    // Close the ledger
    lh.close();
}

From source file:de.rwhq.btree.LeafNode.java

@Override
public int remove(final K key, final V value) {
    final int offset = offsetOfKey(key);
    if (offset == NOT_FOUND)
        return 0;

    final int numberOfValues = get(key).size();

    final ByteBuffer buffer = rawPage().bufferForWriting(offset);
    final byte[] buf1 = new byte[keySerializer.getSerializedLength()];
    final byte[] buf2 = new byte[valueSerializer.getSerializedLength()];
    int removed = 0;

    for (int i = 0; i < numberOfValues; i++) {
        buffer.get(buf1);//from ww w  .  ja va2 s.c  om
        buffer.get(buf2); // load only the value
        final V val = valueSerializer.deserialize(buf2);

        if (val == null)
            throw new IllegalStateException("value retrieved from a value page should not be null");

        // we cant use a comparator here since we have none for values (its the only case we need it)
        if (val.equals(value)) {
            // also free key page
            // move pointers forward and reset buffer
            final int startingPos = buffer.position() - buf1.length - buf2.length;
            System.arraycopy(buffer.array(), buffer.position(), buffer.array(), startingPos,
                    buffer.capacity() - buffer.position());

            buffer.position(startingPos);

            removed++;
        }
    }

    setNumberOfEntries(getNumberOfEntries() - removed);
    return removed;
}

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

/**
 * Verify Advanced asynchronous writing with entryIds in reverse order.
 *//* w  ww  .j av a  2s .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.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * Skips few entries before closing the ledger and assert that the
 * lastAddConfirmed is right before our skipEntryId.
 *
 * @throws Exception//from   ww  w .ja v  a2s. c o m
 */
@Test
public void testLedgerCreateAdvWithSkipEntries() throws Exception {
    long ledgerId;
    SyncObj syncObj1 = new SyncObj();

    // Create a ledger
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    // Save ledgerId to reopen the ledger
    ledgerId = lh.getId();
    LOG.info("Ledger ID: " + ledgerId);
    int skipEntryId = rng.nextInt(numEntriesToWrite - 1);
    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());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i == skipEntryId) {
            LOG.info("Skipping entry:{}", skipEntryId);
            continue;
        }
        lh.asyncAddEntry(i, entry.array(), 0, entry.capacity(), this, syncObj1);
    }
    // wait for all entries to be acknowledged for the first ledger
    synchronized (syncObj1) {
        while (syncObj1.counter < skipEntryId) {
            syncObj1.wait();
        }
        assertEquals(BKException.Code.OK, syncObj1.rc);
    }
    // Close the ledger
    lh.close();
    // Open the ledger
    lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
    assertEquals(lh.lastAddConfirmed, skipEntryId - 1);
    lh.close();
}