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:Main.java

public final static ByteBuffer toNALFileFormat(final ByteBuffer buffer) {
    ByteBuffer result = ByteBuffer.allocate(buffer.remaining());
    result.put(buffer);/*from www  .j  a va 2s .c  o  m*/
    result.flip();
    int length = 0;
    int position = -1;
    int remaining = result.remaining() - 3;
    for (int i = 0; i < remaining; ++i) {
        if (result.get(i) == 0x00 && result.get(i + 1) == 0x00 && result.get(i + 2) == 0x00
                && result.get(i + 3) == 0x01) {
            if (0 <= position) {
                result.putInt(position, length - 3);
            }
            position = i;
            length = 0;
        } else {
            ++length;
        }
    }
    result.putInt(position, length);
    return result;
}

From source file:com.tera.common.util.ConsolePrinter.java

/**
 * Gets last <tt>cnt</tt> read bytes from the <tt>data</tt> buffer and puts
 * into <tt>result</tt> buffer in special format:
 * <ul>/*  w  ww.  j  a  va2 s  .  com*/
 * <li>if byte represents char from partition 0x1F to 0x80 (which are normal
 * ascii chars) then it's put into buffer as it is</li>
 * <li>otherwise dot is put into buffer</li>
 * </ul>
 * 
 * @param data
 * @param result
 * @param cnt
 */
private static void toText(ByteBuffer data, StringBuilder result, int cnt) {
    int charPos = data.position() - cnt;
    for (int a = 0; a < cnt; a++) {
        int c = data.get(charPos++);
        if (c > 0x1f && c < 0x80)
            result.append((char) c);
        else
            result.append('.');
    }
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer doubleSize(ByteBuffer buffer) {
    if (buffer == null) {
        return null;
    }//www.  j av a 2  s  . com
    byte[] content = new byte[buffer.position()];
    buffer.flip();
    buffer.get(content);
    ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2);
    newBuffer.put(content);
    return newBuffer;
}

From source file:net.sf.jml.util.DigestUtils.java

private static void update(MessageDigest digest, ByteBuffer buffer) {
    if (buffer.hasArray()) {
        digest.update(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    } else {//from  w  ww . j  a va 2  s.com
        byte[] b = new byte[buffer.remaining()];
        buffer.get(b);
        digest.update(b);
    }
}

From source file:com.sm.store.Utils.java

public static Value bytesToValue(byte[] bytes) {
    int len = bytes.length;
    ByteBuffer buf = ByteBuffer.wrap(bytes);
    long ver = buf.getLong();
    short node = buf.getShort();
    byte[] data = new byte[len - 10];
    buf.get(data);
    return CacheValue.createValue(data, ver, node);
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static byte[] getBytes(ByteBuffer byteBuffer) {

    byte[] bytes =
            // new byte[byteBuffer.limit()-byteBuffer.position()];
            new byte[byteBuffer.remaining()];

    byteBuffer.get(bytes);
    return bytes;

}

From source file:Main.java

public static final String convertStringCharset(String originalString, String sourceCharset,
        String targetCharset) {/*from  w  ww . j a  v  a 2  s  .  c om*/
    if (sourceCharset.equalsIgnoreCase(targetCharset)) {
        return originalString;
    }
    final Charset charset = Charset.forName(sourceCharset);
    final ByteBuffer byteBuffer = charset.encode(originalString);
    // byteBuffer.array() "may" return byte array which is larger than
    // byteBuffer.remaining(). Here, we keep on the safe side.
    final byte[] bytes = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytes);
    try {
        return new String(bytes, targetCharset);
    } catch (UnsupportedEncodingException e) {
        Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
        return null;
    }
}

From source file:org.eclipse.swt.snippets.Snippet341.java

static void capture(GLCanvas glCanvas) {
    final int PAD = 4;
    Display display = glCanvas.getDisplay();
    Rectangle bounds = glCanvas.getBounds();
    int size = bounds.width * PAD * bounds.height;

    ByteBuffer buffer = ByteBuffer.allocateDirect(size);
    GL11.glReadPixels(0, 0, bounds.width, bounds.height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
    byte[] bytes = new byte[size];
    buffer.get(bytes);
    /*/* w w w.  j a va  2  s .  c  o m*/
     * In OpenGL (0,0) is at the bottom-left corner, and y values ascend in the upward
     * direction.  This is opposite to SWT which defines (0,0) to be at the top-left
     * corner with y values ascendingin the downwards direction.  Re-order the OpenGL-
     * provided bytes to SWT's expected order so that the Image will not appear inverted.
     */
    byte[] temp = new byte[bytes.length];
    for (int i = 0; i < bytes.length; i += bounds.width * PAD) {
        System.arraycopy(bytes, bytes.length - i - bounds.width * PAD, temp, i, bounds.width * PAD);
    }
    bytes = temp;
    ImageData data = new ImageData(bounds.width, bounds.height, 32,
            new PaletteData(0xFF000000, 0xFF0000, 0xFF00), PAD, bytes);
    final Image image = new Image(display, data);

    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setLayoutData(new GridData(bounds.width, bounds.height));
    canvas.addListener(SWT.Paint, event -> event.gc.drawImage(image, 0, 0));
    shell.pack();
    shell.open();
    shell.addListener(SWT.Dispose, event -> image.dispose());
}

From source file:eu.pursuit.core.ItemName.java

public static ItemName parseSerializedName(byte[] array, int segmentSize) {
    if (array.length % segmentSize != 0) {
        throw new IllegalArgumentException(
                "array length (" + array.length + ") not a multiple of segmentSize (" + segmentSize + ")");
    }/*from  w  w w  .  java 2s  .  c om*/

    ByteBuffer bbuffer = ByteBuffer.wrap(array);
    List<ByteIdentifier> list = new ArrayList<ByteIdentifier>();
    int howmany = array.length / segmentSize;
    for (int i = 0; i < howmany - 1; i++) {
        byte[] arr = new byte[segmentSize];
        bbuffer.get(arr);
        list.add(new ByteIdentifier(arr));
    }
    ScopeID scope = new ScopeID(list);

    byte[] arr = new byte[segmentSize];
    bbuffer.get(arr);
    ByteIdentifier rid = new ByteIdentifier(arr);

    return new ItemName(scope, rid);
}

From source file:com.android.camera2.its.ItsUtils.java

public static byte[] getDataFromImage(Image image) throws ItsException {
    int format = image.getFormat();
    int width = image.getWidth();
    int height = image.getHeight();
    byte[] data = null;

    // Read image data
    Plane[] planes = image.getPlanes();/*w w w .  j  av  a  2s  . com*/

    // Check image validity
    if (!checkAndroidImageFormat(image)) {
        throw new ItsException("Invalid image format passed to getDataFromImage: " + image.getFormat());
    }

    if (format == ImageFormat.JPEG) {
        // JPEG doesn't have pixelstride and rowstride, treat it as 1D buffer.
        ByteBuffer buffer = planes[0].getBuffer();
        data = new byte[buffer.capacity()];
        buffer.get(data);
        return data;
    } else if (format == ImageFormat.YUV_420_888 || format == ImageFormat.RAW_SENSOR
            || format == ImageFormat.RAW10) {
        int offset = 0;
        data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8];
        byte[] rowData = new byte[planes[0].getRowStride()];
        for (int i = 0; i < planes.length; i++) {
            ByteBuffer buffer = planes[i].getBuffer();
            int rowStride = planes[i].getRowStride();
            int pixelStride = planes[i].getPixelStride();
            int bytesPerPixel = ImageFormat.getBitsPerPixel(format) / 8;
            Logt.i(TAG, String.format("Reading image: fmt %d, plane %d, w %d, h %d, rowStride %d, pixStride %d",
                    format, i, width, height, rowStride, pixelStride));
            // For multi-planar yuv images, assuming yuv420 with 2x2 chroma subsampling.
            int w = (i == 0) ? width : width / 2;
            int h = (i == 0) ? height : height / 2;
            for (int row = 0; row < h; row++) {
                if (pixelStride == bytesPerPixel) {
                    // Special case: optimized read of the entire row
                    int length = w * bytesPerPixel;
                    buffer.get(data, offset, length);
                    // Advance buffer the remainder of the row stride
                    buffer.position(buffer.position() + rowStride - length);
                    offset += length;
                } else {
                    // Generic case: should work for any pixelStride but slower.
                    // Use intermediate buffer to avoid read byte-by-byte from
                    // DirectByteBuffer, which is very bad for performance.
                    // Also need avoid access out of bound by only reading the available
                    // bytes in the bytebuffer.
                    int readSize = rowStride;
                    if (buffer.remaining() < readSize) {
                        readSize = buffer.remaining();
                    }
                    buffer.get(rowData, 0, readSize);
                    if (pixelStride >= 1) {
                        for (int col = 0; col < w; col++) {
                            data[offset++] = rowData[col * pixelStride];
                        }
                    } else {
                        // PixelStride of 0 can mean pixel isn't a multiple of 8 bits, for
                        // example with RAW10. Just copy the buffer, dropping any padding at
                        // the end of the row.
                        int length = (w * ImageFormat.getBitsPerPixel(format)) / 8;
                        System.arraycopy(rowData, 0, data, offset, length);
                        offset += length;
                    }
                }
            }
        }
        Logt.i(TAG, String.format("Done reading image, format %d", format));
        return data;
    } else {
        throw new ItsException("Unsupported image format: " + format);
    }
}