Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

/**
 * Given an image reader, extracts the JPEG image bytes and then closes the
 * reader.//  ww  w.  ja  v  a2s .  c  om
 *
 * @param img the image from which to extract jpeg bytes or compress to
 *            jpeg.
 * @return The bytes of the JPEG image. Newly allocated.
 */
private byte[] acquireJpegBytes(Image img) {
    ByteBuffer buffer;

    if (img.getFormat() == ImageFormat.JPEG) {
        Image.Plane plane0 = img.getPlanes()[0];
        buffer = plane0.getBuffer();

        byte[] imageBytes = new byte[buffer.remaining()];
        buffer.get(imageBytes);
        buffer.rewind();
        return imageBytes;
    } else {
        throw new RuntimeException("Unsupported image format.");
    }
}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterInbound.java

private void parseUsingStream(ByteBuffer bb) {
    try {/* w  w  w  .  java  2s . com*/
        int remaining = bb.remaining();
        System.out.println("buf-copy remaining: " + bb.remaining());
        if (remaining <= 0)
            return;
        byte[] bytes = new byte[remaining];
        bb.get(bytes);

        saxParser.parse(new ByteArrayInputStream(bytes), messageParser);
        bytes = null;
    } catch (SAXException e) {
        log.error(e);
        log.error(e.getStackTrace());
    } catch (IOException e) {
        log.error(e);
        log.error(e.getStackTrace());
    }
}

From source file:net.dv8tion.jda.audio.AudioConnection.java

private byte[] encodeToOpus(byte[] rawAudio) {
    ShortBuffer nonEncodedBuffer = ShortBuffer.allocate(rawAudio.length / 2);
    ByteBuffer encoded = ByteBuffer.allocate(4096);
    for (int i = 0; i < rawAudio.length; i += 2) {
        int firstByte = (0x000000FF & rawAudio[i]); //Promotes to int and handles the fact that it was unsigned.
        int secondByte = (0x000000FF & rawAudio[i + 1]); //

        //Combines the 2 bytes into a short. Opus deals with unsigned shorts, not bytes.
        short toShort = (short) ((firstByte << 8) | secondByte);

        nonEncodedBuffer.put(toShort);/*w  ww  . j ava  2s. c  o m*/
    }
    nonEncodedBuffer.flip();

    //TODO: check for 0 / negative value for error.
    int result = Opus.INSTANCE.opus_encode(opusEncoder, nonEncodedBuffer, OPUS_FRAME_SIZE, encoded,
            encoded.capacity());

    //ENCODING STOPS HERE

    byte[] audio = new byte[result];
    encoded.get(audio);
    return audio;
}

From source file:au.org.ala.delta.intkey.model.IntkeyDatasetFileReader.java

/**
 * Read taxon data//w  w w  . j  a  va  2s . c  o  m
 * 
 * @param itemFileHeader
 *            Items (taxa) file header data
 * @param itemBinFile
 *            Items (taxa) file
 * @param taxa
 *            List of object representation of taxa, ordered by taxon
 *            number. The calling method must set this data on the object
 *            representation of the intkey dataset.
 */
private static void readTaxonData(ItemsFileHeader itemFileHeader, BinFile itemBinFile, List<Item> taxa) {

    int numItems = itemFileHeader.getNItem();

    for (int i = 0; i < numItems; i++) {
        ItemData itemData = new IntkeyItemData(i + 1);
        Item item = new Item(itemData);
        taxa.add(item);
    }

    // READ TAXON NAMES - rpTnam
    seekToRecord(itemBinFile, itemFileHeader.getRpTnam());

    List<Integer> taxonNameOffsets = readIntegerList(itemBinFile, numItems + 1);

    int recordsSpannedByOffsets = recordsSpannedByBytes(taxonNameOffsets.size() * Constants.SIZE_INT_IN_BYTES);

    seekToRecord(itemBinFile, itemFileHeader.getRpTnam() + recordsSpannedByOffsets);

    ByteBuffer nameBuffer = itemBinFile.readByteBuffer(taxonNameOffsets.get(taxonNameOffsets.size() - 1));
    nameBuffer.position(0);

    for (int i = 0; i < numItems; i++) {
        int start = taxonNameOffsets.get(i);
        int end = taxonNameOffsets.get(i + 1);
        int nameLength = end - start;
        byte[] nameArray = new byte[nameLength];
        nameBuffer.get(nameArray);
        taxa.get(i).setDescription(BinFileEncoding.decode(nameArray));
    }

    readTaxonLinksFiles(itemFileHeader, itemBinFile, taxa);
}

From source file:io.warp10.continuum.gts.GTSDecoder.java

/**
 * Return an encoder with all data from the last value retrieved (post call to next())
 * onwards//from   w w  w .  j  ava2s  .  c  o m
 * 
 * @param safeMetadata Is it safe to reuse the Metadata?
 */
public GTSEncoder getEncoder(boolean safeMetadata) throws IOException {

    if (!nextCalled) {
        throw new IOException(
                "Can only get an encoder for a decoder on which 'next' was called at least once.");
    }

    //
    // Copy the remaining data into a new ByteBuffer
    //

    ByteBuffer bb = this.buffer.duplicate();
    bb.position(this.position);

    int offset = 0;
    int len = bb.remaining();
    byte[] bytes = null;

    if (bb.hasArray()) {
        bytes = bb.array();
        offset = bb.arrayOffset() + bb.position();
    } else {
        bytes = new byte[bb.remaining()];
        bb.get(bytes);
    }

    //
    // Create an encoder with the same base timestamp and wrapping key, providing a sizing hint
    //

    GTSEncoder encoder = new GTSEncoder(this.baseTimestamp, this.wrappingKey, bb.remaining());

    if (safeMetadata) {
        encoder.safeSetMetadata(this.getMetadata());
    } else {
        encoder.setMetadata(this.getMetadata());
    }

    //
    // Set initial values
    //

    encoder.initialize(this.previousLastTimestamp, this.previousLastGeoXPPoint, this.previousLastElevation,
            this.previousLastLongValue, this.previousLastDoubleValue, this.previousLastBDValue,
            this.previousLastStringValue);

    //
    // Copy the encoded data
    //

    encoder.stream.write(bytes, offset, len);

    //
    // Put the encoder into 'safe delta' mode, because we don't know what the last
    // value/ts/elevation/location were, we can't use delta encoding for now
    //

    encoder.safeDelta();

    encoder.setCount(this.count);

    return encoder;
}

From source file:com.android.camera.one.v2.OneCameraZslImpl.java

/**
 * Given an image reader, extracts the JPEG image bytes and then closes the
 * reader./*from  w  w  w .j a v  a 2  s.  c o m*/
 *
 * @param img the image from which to extract jpeg bytes or compress to
 *            jpeg.
 * @param degrees the angle to rotate the image clockwise, in degrees. Rotation is
 *            only applied to YUV images.
 * @return The bytes of the JPEG image. Newly allocated.
 */
private byte[] acquireJpegBytes(Image img, int degrees) {
    ByteBuffer buffer;

    if (img.getFormat() == ImageFormat.JPEG) {
        Image.Plane plane0 = img.getPlanes()[0];
        buffer = plane0.getBuffer();

        byte[] imageBytes = new byte[buffer.remaining()];
        buffer.get(imageBytes);
        buffer.rewind();
        return imageBytes;
    } else if (img.getFormat() == ImageFormat.YUV_420_888) {
        buffer = mJpegByteBufferPool.acquire();
        if (buffer == null) {
            buffer = ByteBuffer.allocateDirect(img.getWidth() * img.getHeight() * 3);
        }

        int numBytes = JpegUtilNative.compressJpegFromYUV420Image(new AndroidImageProxy(img), buffer,
                JPEG_QUALITY, degrees);

        if (numBytes < 0) {
            throw new RuntimeException("Error compressing jpeg.");
        }

        buffer.limit(numBytes);

        byte[] imageBytes = new byte[buffer.remaining()];
        buffer.get(imageBytes);

        buffer.clear();
        mJpegByteBufferPool.release(buffer);

        return imageBytes;
    } else {
        throw new RuntimeException("Unsupported image format.");
    }
}

From source file:edu.uci.ics.crawler4j.crawler.fetcher.PageFetcher.java

private boolean loadPage(final Page p, final InputStream in, final int totalsize, final boolean isBinary,
        String encoding) {//from   w w  w. j  a  v  a 2s  .c  om
    ByteBuffer bBuf;

    if (totalsize > 0) {
        bBuf = ByteBuffer.allocate(totalsize + 1024);
    } else {
        bBuf = ByteBuffer.allocate(maxDownloadSize);
    }
    final byte[] b = new byte[1024];
    int len;
    double finished = 0;
    try {
        while ((len = in.read(b)) != -1) {
            if (finished + b.length > bBuf.capacity()) {
                break;
            }
            bBuf.put(b, 0, len);
            finished += len;
        }
    } catch (final BufferOverflowException boe) {
        System.out.println("Page size exceeds maximum allowed.");
        return false;
    } catch (final Exception e) {
        System.err.println(e.getMessage());
        return false;
    }

    bBuf.flip();
    if (isBinary) {
        byte[] tmp = new byte[bBuf.limit()];
        bBuf.get(tmp);
        p.setBinaryData(tmp);
    } else {
        String html = "";
        if (encoding == null) {
            int pos = bBuf.position();
            html = Charset.forName("US-ASCII").decode(bBuf).toString();
            bBuf.position(pos);
            pos = html.toLowerCase().indexOf("<meta http-equiv=\"content-type\" content=\"");
            if (pos >= 0) {
                int end = html.indexOf("\"", pos + 41);
                if (end >= 0) {
                    String content = html.substring(pos, end);
                    if (content.contains("charset=")) {
                        encoding = content.substring(content.indexOf("charset=") + 8);
                    }
                }
            }
        }
        if (encoding == null || !Charset.isSupported(encoding))
            encoding = "UTF-8";

        if (!encoding.equals("UTF-8")) {
            html = Charset.forName(encoding).decode(bBuf).toString();
        }

        if (html.length() == 0) {
            return false;
        }
        p.setHTML(html);
    }
    return true;
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Decodes a GUID value./*  w  ww .j av  a2  s.c o  m*/
 */
private static String readGUIDValue(ByteBuffer buffer, ByteOrder order) {
    if (order != ByteOrder.BIG_ENDIAN) {
        byte[] tmpArr = new byte[16];
        buffer.get(tmpArr);

        // the first 3 guid components are integer components which need to
        // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int
        ByteUtil.swap4Bytes(tmpArr, 0);
        ByteUtil.swap2Bytes(tmpArr, 4);
        ByteUtil.swap2Bytes(tmpArr, 6);
        buffer = ByteBuffer.wrap(tmpArr);
    }

    StringBuilder sb = new StringBuilder(22);
    sb.append("{");
    sb.append(ByteUtil.toHexString(buffer, 0, 4, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 4, 2, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 6, 2, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 8, 2, false));
    sb.append("-");
    sb.append(ByteUtil.toHexString(buffer, 10, 6, false));
    sb.append("}");
    return (sb.toString());
}

From source file:com.jivesoftware.os.amza.service.storage.WALStorage.java

private void loadInternal(IoStats ioStats, File baseKey, long deltaWALId, long prevDeltaWALId, boolean recovery,
        boolean backwardScan, boolean truncateToEndOfMergeMarker, int maxValueSizeInIndex, int stripe)
        throws Exception {
    try {//from  w  w w.j  a va 2 s.c  o m

        long initialHighestTxId = highestTxId.get();
        if (!recovery && initialHighestTxId != -1) {
            throw new IllegalStateException(
                    "Load should have completed before highestTxId:" + initialHighestTxId + " is modified.");
        }

        walTx.open(baseKey, io -> {
            boolean[] endOfMergeMarker = { false };
            long[] lastTxId = { -1 };
            long[] fpOfLastLeap = { -1 };
            long[] updatesSinceLastMergeMarker = { 0 };
            long[] updatesSinceLastLeap = { 0 };

            long[] trailingDeltaWALId = { -1 };
            long[] loadOldestTimestamp = { -1 };
            long[] loadOldestVersion = { -1 };
            long[] loadOldestTombstonedTimestamp = { -1 };
            long[] loadOldestTombstonedVersion = { -1 };
            long[] loadKeyCount = { 0 };
            long[] loadClobberCount = { 0 };
            long[] loadKeyHighwaterTimestamps = versionedPartitionName.getPartitionName().isSystemPartition()
                    ? new long[numKeyHighwaterStripes]
                    : null;

            long[] truncate = { 0 };
            primaryRowMarshaller.fromRows(fpRowStream -> {
                io.validate(ioStats, backwardScan, truncateToEndOfMergeMarker,
                        (rowFP, rowTxId, rowType, row) -> {
                            // backward scan
                            if (rowType == RowType.end_of_merge) {
                                long[] marker = loadEndOfMergeMarker(deltaWALId, row);
                                if (marker == null) {
                                    return -1;
                                }

                                endOfMergeMarker[0] = true;
                                trailingDeltaWALId[0] = marker[EOM_DELTA_WAL_ID_INDEX];
                                lastTxId[0] = Math.max(lastTxId[0], marker[EOM_HIGHEST_TX_ID_INDEX]);
                                loadOldestTimestamp[0] = marker[EOM_OLDEST_TIMESTAMP_INDEX];
                                loadOldestVersion[0] = marker[EOM_OLDEST_VERSION_INDEX];
                                loadOldestTombstonedTimestamp[0] = marker[EOM_OLDEST_TOMBSTONED_TIMESTAMP_INDEX];
                                loadOldestTombstonedVersion[0] = marker[EOM_OLDEST_TOMBSTONED_VERSION_INDEX];
                                loadKeyCount[0] = marker[EOM_KEY_COUNT_INDEX];
                                loadClobberCount[0] = marker[EOM_CLOBBER_COUNT_INDEX];
                                //markerStripedTimestamps[0] = marker;

                                fpOfLastLeap[0] = marker[EOM_FP_OF_LAST_LEAP_INDEX];
                                updatesSinceLastLeap[0] = marker[EOM_UPDATES_SINCE_LAST_LEAP_INDEX];
                                return rowFP;
                            }
                            return -1;
                        }, (rowFP, rowTxId, rowType, row) -> {
                            // forward scan, only called if backward scan was unsuccessful
                            if (rowType.isPrimary()) {
                                lastTxId[0] = Math.max(lastTxId[0], rowTxId);
                                updatesSinceLastMergeMarker[0]++;
                                updatesSinceLastLeap[0]++;
                                try {
                                    Preconditions.checkState(fpRowStream.stream(rowFP, rowType, row),
                                            "Validation must accept all primary rows");
                                } catch (IOException e) {
                                    LOG.error(
                                            "Encountered I/O exception during forward validation for {}, WAL must be truncated",
                                            new Object[] { versionedPartitionName }, e);
                                    return rowFP;
                                }
                            } else if (rowType == RowType.end_of_merge) {
                                long[] marker = loadEndOfMergeMarker(deltaWALId, row);
                                if (marker == null) {
                                    LOG.error(
                                            "Encountered corrupt end of merge marker during forward validation for {}, WAL must be truncated",
                                            versionedPartitionName);
                                    return rowFP;
                                }

                                updatesSinceLastMergeMarker[0] = 0;

                                endOfMergeMarker[0] = true;
                                trailingDeltaWALId[0] = marker[EOM_DELTA_WAL_ID_INDEX];
                                lastTxId[0] = Math.max(lastTxId[0], marker[EOM_HIGHEST_TX_ID_INDEX]);
                                loadOldestTimestamp[0] = Math.min(loadOldestTimestamp[0],
                                        marker[EOM_OLDEST_TIMESTAMP_INDEX]);
                                loadOldestVersion[0] = Math.min(loadOldestVersion[0],
                                        marker[EOM_OLDEST_VERSION_INDEX]);
                                loadOldestTombstonedTimestamp[0] = Math.min(loadOldestTombstonedTimestamp[0],
                                        marker[EOM_OLDEST_TOMBSTONED_TIMESTAMP_INDEX]);
                                loadOldestTombstonedVersion[0] = Math.min(loadOldestTombstonedVersion[0],
                                        marker[EOM_OLDEST_TOMBSTONED_VERSION_INDEX]);
                                loadKeyCount[0] = marker[EOM_KEY_COUNT_INDEX];
                                loadClobberCount[0] = marker[EOM_CLOBBER_COUNT_INDEX];
                                //markerStripedTimestamps[0] = marker;

                                if (truncateToEndOfMergeMarker) {
                                    truncate[0] = rowFP;
                                }
                            } else if (rowType == RowType.system) {
                                ByteBuffer buf = ByteBuffer.wrap(row);
                                byte[] keyBytes = new byte[8];
                                buf.get(keyBytes);
                                long key = UIO.bytesLong(keyBytes);
                                if (key == RowType.LEAP_KEY) {
                                    fpOfLastLeap[0] = rowFP;
                                    updatesSinceLastLeap[0] = 0;
                                }
                            }
                            if (!truncateToEndOfMergeMarker) {
                                truncate[0] = rowFP;
                            }
                            return -(truncate[0] + 1);
                        }, null);

                // TODO!!!! handle the discontinguity of endOfMerge delta ids which prevents us from doing trailingDeltaWALId[0] != prevDeltaWALId
                if (truncateToEndOfMergeMarker) {
                    if (prevDeltaWALId > -1 && trailingDeltaWALId[0] > prevDeltaWALId) {
                        LOG.error(
                                "Inconsistency detected while loading delta:{} prev:{}, encountered tail:{} for {}",
                                deltaWALId, prevDeltaWALId, trailingDeltaWALId[0], versionedPartitionName);

                        // Since we added the ability to rebalance this IllegalStateException needs to be reworked so that it works.
                        /*throw new IllegalStateException("Delta mismatch, intervention is required. "
                        + "delta:" + deltaWALId
                        + " prev:" + prevDeltaWALId
                        + ", encountered tail:" + trailingDeltaWALId[0]
                        + " for " + versionedPartitionName);*/
                    }
                }
                return true;
            }, (fp, rowType, prefix, key, value, valueTimestamp, valueTombstoned, valueVersion) -> {
                loadOldestTimestamp[0] = Math.min(loadOldestTimestamp[0], valueTimestamp);
                loadOldestVersion[0] = Math.min(loadOldestVersion[0], valueVersion);
                if (valueTombstoned) {
                    loadOldestTombstonedTimestamp[0] = Math.min(loadOldestTombstonedTimestamp[0],
                            valueTimestamp);
                    loadOldestTombstonedVersion[0] = Math.min(loadOldestTombstonedVersion[0], valueVersion);
                }
                if (loadKeyHighwaterTimestamps != null) {
                    mergeStripedKeyHighwaters(prefix, key, valueTimestamp, loadKeyHighwaterTimestamps);
                }
                return true;
            });

            hasEndOfMergeMarker.set(endOfMergeMarker[0]);
            highestTxId.set(lastTxId[0]);
            mergedTxId = lastTxId[0];
            oldestTimestamp.set(loadOldestTimestamp[0]);
            oldestVersion.set(loadOldestVersion[0]);
            oldestTombstonedTimestamp.set(loadOldestTombstonedTimestamp[0]);
            oldestTombstonedVersion.set(loadOldestTombstonedVersion[0]);
            keyCount.set(loadKeyCount[0] + updatesSinceLastMergeMarker[0]);
            clobberCount.set(loadClobberCount[0]);

            io.initLeaps(ioStats, fpOfLastLeap[0], updatesSinceLastLeap[0]);

            if (loadKeyHighwaterTimestamps != null) {
                LOG.info("Loaded highwater timestamps for {}", versionedPartitionName);
                stripedKeyHighwaterTimestamps = loadKeyHighwaterTimestamps;
            }
            /*if (markerStripedTimestamps[0] != null) {
            for (int i = 0, offset = EOM_HIGHWATER_STRIPES_OFFSET; i < numKeyHighwaterStripes; i++, offset++) {
                stripedKeyHighwaterTimestamps[i] = Math.max(stripedKeyHighwaterTimestamps[i], markerStripedTimestamps[0][offset]);
            }
            }*/

            return null;
        });

        I index = walTx.openIndex(ioStats, baseKey, walIndexProvider, versionedPartitionName,
                maxValueSizeInIndex, stripe);
        walIndex.compareAndSet(null, index);

    } catch (Exception e) {
        LOG.error(
                "Partition {} could not be opened, intervention is required, partition will be parked, recovery:{}",
                new Object[] { versionedPartitionName, recovery }, e);
        sick.set(true);
        sickPartitions.sick(versionedPartitionName, e);
    }
}

From source file:com.healthmarketscience.jackcess.Table.java

/**
 * Returns the row count for the current page.  If the page is invalid
 * ({@code null}) or the page is not a DATA page, 0 is returned.
 *///from w  w  w  .  j a  v a  2  s.  c o m
private static int getRowsOnDataPage(ByteBuffer rowBuffer, JetFormat format) throws IOException {
    int rowsOnPage = 0;
    if ((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
        rowsOnPage = rowBuffer.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
    }
    return rowsOnPage;
}