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 the functionality Ledgers with different digests.
 *
 * @throws Exception/* w w w  .j  a  va  2s. c  o  m*/
 */
@Test
public void testLedgerDigestTest() throws Exception {
    for (DigestType type : DigestType.values()) {
        lh = bkc.createLedger(5, 3, 2, type, ledgerPassword);

        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());
        }

        readEntries(lh, entries1);

        long lid = lh.getId();
        lh.close();
        bkc.deleteLedger(lid);
        entries1.clear();
    }
}

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

/**
 * Verify the functionality LedgerHandleAdv addEntry with duplicate entryIds.
 *
 * @throws Exception//from   ww w  .  j a v  a 2 s.  c  o m
 */
@Test
public void testLedgerCreateAdvSyncAddDuplicateEntryIds() throws Exception {
    // Create a ledger
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    LOG.info("Ledger ID: " + lh.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());
        lh.addEntry(i, entry.array());
        entry.position(0);
    }
    readEntries(lh, entries1);

    int dupEntryId = rng.nextInt(numEntriesToWrite - 1);

    try {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        lh.addEntry(dupEntryId, entry.array());
        fail("Expected exception not thrown");
    } catch (BKException e) {
        // This test expects DuplicateEntryIdException
        assertEquals(e.getCode(), BKException.Code.DuplicateEntryIdException);
    }
    lh.close();
}

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

/**
 * Verify the functionality of Advanced Ledger which returns
 * LedgerHandleAdv. LedgerHandleAdv takes entryId for addEntry, and let
 * user manage entryId allocation.// ww  w  .  j  a v  a 2 s.co m
 *
 * @throws Exception
 */
@Test
public void testLedgerCreateAdv() throws Exception {
    // Create a ledger
    lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
    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(i, entry.array());
    }
    // Start one more bookies
    startNewBookie();

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

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

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

    readEntries(lh, entries1);
    lh.close();
}

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

/**
 * Verify the functionality of Advanced Ledger which accepts ledgerId as input and returns
 * LedgerHandleAdv. LedgerHandleAdv takes entryId for addEntry, and let
 * user manage entryId allocation.//from  ww  w .  j  av a 2s. co  m
 *
 * @throws Exception
 */
@Test
public void testLedgerCreateAdvWithLedgerId() throws Exception {
    // Create a ledger
    long ledgerId = 0xABCDEF;
    lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);
    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(i, entry.array());
    }
    // Start one more bookies
    startNewBookie();

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

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

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

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

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

/**
 * Verify write when few bookie failures in last ensemble and forcing
 * ensemble reformation./*from w w w  . j  a  va2  s.  co  m*/
 */
@Test
public void testWithMultipleBookieFailuresInLastEnsemble() throws Exception {
    // Create a ledger
    lh = bkc.createLedger(5, 4, digestType, ledgerPassword);
    LOG.info("Ledger ID: " + lh.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());
        lh.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));

    int i = numEntriesToWrite;
    numEntriesToWrite = numEntriesToWrite + 50;
    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);
    lh.close();
}

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

/**
 * Verify the functionality of Ledger create which accepts customMetadata as input.
 * Also verifies that the data written is read back properly.
 *
 * @throws Exception/*  w w  w .j a  v  a 2  s. c o m*/
 */
@Test
public void testLedgerCreateWithCustomMetadata() throws Exception {
    // Create a ledger
    long ledgerId;
    int maxLedgers = 10;
    for (int i = 0; i < maxLedgers; i++) {
        Map<String, byte[]> inputCustomMetadataMap = new HashMap<String, byte[]>();
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);

        // each ledger has different number of key, value pairs.
        for (int j = 0; j < i; j++) {
            inputCustomMetadataMap.put("key" + j, UUID.randomUUID().toString().getBytes());
        }

        if (i < maxLedgers / 2) {
            // 0 to 4 test with createLedger interface
            lh = bkc.createLedger(5, 3, 2, digestType, ledgerPassword, inputCustomMetadataMap);
            ledgerId = lh.getId();
            lh.addEntry(entry.array());
        } else {
            // 5 to 9 test with createLedgerAdv interface
            lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword, inputCustomMetadataMap);
            ledgerId = lh.getId();
            lh.addEntry(0, entry.array());
        }
        lh.close();

        // now reopen the ledger; this should fetch all the metadata stored on zk
        // and the customMetadata written and read should match
        lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
        Map<String, byte[]> outputCustomMetadataMap = lh.getCustomMetadata();
        assertTrue("Can't retrieve proper Custom Data",
                areByteArrayValMapsEqual(inputCustomMetadataMap, outputCustomMetadataMap));
        lh.close();
        bkc.deleteLedger(ledgerId);
    }
}

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 w  w w. j a  v a2s . c om*/
 */
@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();
}

From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java

public ArrayList<File> mediaSerialize(Cursor cursor) {
    logger.debug("::mediaSerialize");
    ArrayList<File> paths = new ArrayList<File>();
    if (1 > cursor.getCount())
        return paths;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream eos = new DataOutputStream(baos);

    for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) {
        MediaWrapper iw = new MediaWrapper();
        iw.setEventId(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.EVENT_ID)));
        iw.setDataType(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.DATA_TYPE)));
        iw.setData(cursor.getString(cursor.getColumnIndex(MediaTableSchemaBase.DATA)));
        iw.setCreatedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase.CREATED_DATE)));
        iw.setModifiedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase.MODIFIED_DATE)));
        iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(MediaTableSchemaBase._RECEIVED_DATE)));
        iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(MediaTableSchemaBase._DISPOSITION)));

        Gson gson = new Gson();

        try {/*from  ww  w .j  a v  a 2 s.com*/
            eos.writeBytes(gson.toJson(iw));
            eos.writeByte(0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        // not a reference field name :event id eventId event_id\n 
        try {
            String fileName = iw.getData();
            File dataFile = new File(fileName);
            int dataSize = (int) dataFile.length();
            byte[] buffData = new byte[dataSize];
            FileInputStream fileStream = new FileInputStream(dataFile);
            int ret = 0;
            for (int position = 0; (ret > -1 && dataSize > position); position += ret) {
                ret = fileStream.read(buffData, position, dataSize - position);
            }
            fileStream.close();

            eos.writeBytes("data");
            eos.writeByte(0);

            ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
            dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN);
            dataSizeBuf.putInt(dataSize);

            // write the media back out
            eos.write(dataSizeBuf.array());
            eos.write(buffData);
            eos.write(dataSizeBuf.array());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // not a reference field name :created date createdDate created_date\n 
        // not a reference field name :modified date modifiedDate modified_date\n 
        // MediaTableSchemaBase._DISPOSITION;

        //           try {
        // TODO write to content provider using openFile
        // if (!applCacheMediaDir.exists() ) applCacheMediaDir.mkdirs();

        // File outfile = new File(applCacheMediaDir, Integer.toHexString((int) System.currentTimeMillis())); 
        //              BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192);
        //              bufferedOutput.write(baos.toByteArray());
        //              bufferedOutput.flush();
        //              bufferedOutput.close();

        //           } catch (FileNotFoundException e) {
        //              e.printStackTrace();
        //           } catch (IOException e) {
        //              e.printStackTrace();
        //           }
    }
    return paths;
}

From source file:edu.vu.isis.ammo.dash.provider.IncidentSyncAdaptor.java

public ArrayList<File> categorySerialize(Cursor cursor) {
    logger.debug("::categorySerialize");
    ArrayList<File> paths = new ArrayList<File>();
    if (1 > cursor.getCount())
        return paths;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream eos = new DataOutputStream(baos);

    for (boolean more = cursor.moveToFirst(); more; more = cursor.moveToNext()) {
        CategoryWrapper iw = new CategoryWrapper();
        iw.setMainCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.MAIN_CATEGORY)));
        iw.setSubCategory(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.SUB_CATEGORY)));
        iw.setTigrId(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.TIGR_ID)));
        iw.setIconType(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON_TYPE)));
        iw.setIcon(cursor.getString(cursor.getColumnIndex(CategoryTableSchemaBase.ICON)));
        iw.set_ReceivedDate(cursor.getLong(cursor.getColumnIndex(CategoryTableSchemaBase._RECEIVED_DATE)));
        iw.set_Disposition(cursor.getInt(cursor.getColumnIndex(CategoryTableSchemaBase._DISPOSITION)));

        Gson gson = new Gson();

        try {/* w  ww. j a va2s  . c  o  m*/
            eos.writeBytes(gson.toJson(iw));
            eos.writeByte(0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        // not a reference field name :main category mainCategory main_category\n 
        // not a reference field name :sub category subCategory sub_category\n 
        // not a reference field name :tigr id tigrId tigr_id\n 
        try {
            String fileName = iw.getIcon();
            File dataFile = new File(fileName);
            int dataSize = (int) dataFile.length();
            byte[] buffData = new byte[dataSize];
            FileInputStream fileStream = new FileInputStream(dataFile);
            int ret = 0;
            for (int position = 0; (ret > -1 && dataSize > position); position += ret) {
                ret = fileStream.read(buffData, position, dataSize - position);
            }
            fileStream.close();

            eos.writeBytes("icon");
            eos.writeByte(0);

            ByteBuffer dataSizeBuf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
            dataSizeBuf.order(ByteOrder.LITTLE_ENDIAN);
            dataSizeBuf.putInt(dataSize);

            // write the category back out
            eos.write(dataSizeBuf.array());
            eos.write(buffData);
            eos.write(dataSizeBuf.array());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // CategoryTableSchemaBase._DISPOSITION;

        //           try {
        //              if (!applCacheCategoryDir.exists() ) applCacheCategoryDir.mkdirs();
        //              
        //              File outfile = new File(applCacheCategoryDir, Integer.toHexString((int) System.currentTimeMillis())); 
        //              BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(outfile), 8192);
        //              bufferedOutput.write(baos.toByteArray());
        //              bufferedOutput.flush();
        //              bufferedOutput.close();
        //           
        //              paths.add(outfile);
        //           } catch (FileNotFoundException e) {
        //              e.printStackTrace();
        //           } catch (IOException e) {
        //              e.printStackTrace();
        //           }
    }
    return paths;
}

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

/**
 * Verify the functionality LedgerHandleAdv asyncAddEntry with duplicate
 * entryIds.//from   w  w  w .j  a v  a  2s  . com
 *
 * @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();
}