Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:Main.java

public static byte[] intToByteArray(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putInt(value);/*  w w  w. ja  v  a 2s .co m*/
    return buffer.array();
}

From source file:Main.java

public static byte[] shortToByteArrayLE(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort(value);//from   w ww. j a  v  a2s.c om
    return buffer.array();
}

From source file:Main.java

public final static int copy(final ByteBuffer from, final int offset1, final ByteBuffer to, final int offset2,
        final int len) {
    System.arraycopy(from.array(), offset1, to.array(), offset2, len);
    to.limit(offset2 + len);/*from   w w w  . j ava  2 s.c o  m*/
    return len;
}

From source file:Main.java

public static byte[] intToByteArrayLE(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(value);/*from  w w  w . j  ava  2 s  .c om*/
    return buffer.array();
}

From source file:Main.java

public static int byteBufferToByteArray(ByteBuffer byteBuffer, byte[] target, int offset) {
    int remaining = byteBuffer.remaining();
    System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), target, offset,
            remaining);//ww w  .ja v a 2  s .c  om
    return remaining;
}

From source file:Main.java

/**
 * Converts bitmap to the byte array without compression
 * @param bitmap source bitmap/*from   w  w w. j  a v  a  2 s  .co  m*/
 * @return result byte array
 */
public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer.array();
}

From source file:Main.java

public static byte[] leIntToByteArray(int i) {
    final ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putInt(i);//from   w w w .j a v  a  2s  .c o  m
    return bb.array();
}

From source file:Main.java

/**
 * Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}.
 *
 * @param compressed The compressed buffer.
 * @return The decompressed array.//from  www.jav  a2  s. com
 * @throws IOException If there is an error decompressing the buffer.
 */
public static byte[] degzip(ByteBuffer compressed) throws IOException {
    try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed.array()));
            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];

        while (true) {
            int read = is.read(buffer, 0, buffer.length);
            if (read == -1) {
                break;
            }

            out.write(buffer, 0, read);
        }

        return out.toByteArray();
    }
}

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

public static String string(ByteBuffer b) {
    return new String(b.array(), b.arrayOffset() + b.position(), b.remaining());
}

From source file:Main.java

public static byte[] bitmapToArray(Bitmap b) {
    int bytes = b.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
    b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

    byte[] array = buffer.array(); //Get the underlying array containing the data.

    return array;
}