Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

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

Prototype

public static ByteBuffer wrap(byte[] array, int start, int len) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes, 0, 4);

    System.out.println(Arrays.toString(buf.array()));
    System.out.println(buf.toString());
}

From source file:Main.java

public static byte[] subByte(byte[] bytes, int index, int count) {
    byte[] substr = new byte[count];
    ByteBuffer.wrap(bytes, index, count).get(substr);
    return substr;
}

From source file:Main.java

public static int toInt(byte[] bytes) {
    bb = ByteBuffer.wrap(bytes, 0, bytes.length);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getInt();
}

From source file:Main.java

public static float toFloat(byte[] bytes) {
    bb = ByteBuffer.wrap(bytes, 0, bytes.length);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getFloat();
}

From source file:Main.java

public static int toUnsignedShort(byte[] bytes) {
    bb = ByteBuffer.wrap(bytes, 0, bytes.length);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getShort() & 0xffff;
}

From source file:Main.java

public static long bytearray2long(byte[] b) {
    //TODO optimize with creating a new ByteBuffer object

    ByteBuffer buf = ByteBuffer.wrap(b, 0, 8);
    return buf.getLong();
}

From source file:Main.java

public static long Uint64FromBuffer(byte[] buffer, int offset) {
    ByteBuffer bb = ByteBuffer.wrap(buffer, offset, 8);
    bb.order(ByteOrder.BIG_ENDIAN);
    long result = bb.getLong();
    return result;
}

From source file:Main.java

public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> paramList) {
    ArrayList localArrayList = new ArrayList(paramList.size());
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext()) {
        ByteBuffer localByteBuffer1 = (ByteBuffer) localIterator.next();
        int i = -1 + localArrayList.size();
        if ((i >= 0) && (localByteBuffer1.hasArray()) && (((ByteBuffer) localArrayList.get(i)).hasArray())
                && (localByteBuffer1.array() == ((ByteBuffer) localArrayList.get(i)).array())
                && (((ByteBuffer) localArrayList.get(i)).arrayOffset()
                        + ((ByteBuffer) localArrayList.get(i)).limit() == localByteBuffer1.arrayOffset())) {
            ByteBuffer localByteBuffer3 = (ByteBuffer) localArrayList.remove(i);
            localArrayList.add(ByteBuffer.wrap(localByteBuffer1.array(), localByteBuffer3.arrayOffset(),
                    localByteBuffer3.limit() + localByteBuffer1.limit()).slice());
        } else if ((i >= 0) && ((localByteBuffer1 instanceof MappedByteBuffer))
                && ((localArrayList.get(i) instanceof MappedByteBuffer))
                && (((ByteBuffer) localArrayList.get(i))
                        .limit() == ((ByteBuffer) localArrayList.get(i)).capacity()
                                - localByteBuffer1.capacity())) {
            ByteBuffer localByteBuffer2 = (ByteBuffer) localArrayList.get(i);
            localByteBuffer2.limit(localByteBuffer1.limit() + localByteBuffer2.limit());
        } else {/*from w w  w  .  j  a  v  a  2  s  .  c  om*/
            localByteBuffer1.reset();
            localArrayList.add(localByteBuffer1);
        }
    }
    return localArrayList;
}

From source file:StreamUtil.java

private static CharBuffer decodeHelper(byte[] byteArray, int numberOfBytes, java.nio.charset.Charset charset)
        throws IOException {
    CharsetDecoder decoder = charset.newDecoder();
    CharBuffer charBuffer = null;
    try {// w w  w. j  a  v  a2s  . c  o  m
        charBuffer = decoder.decode(ByteBuffer.wrap(byteArray, 0, numberOfBytes));
    } catch (MalformedInputException ex) {
        charBuffer = null;
    }
    return charBuffer;

}

From source file:Main.java

public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> samples) {
    ArrayList<ByteBuffer> nuSamples = new ArrayList<ByteBuffer>(samples.size());
    for (ByteBuffer buffer : samples) {
        int lastIndex = nuSamples.size() - 1;
        if (lastIndex >= 0 && buffer.hasArray() && nuSamples.get(lastIndex).hasArray()
                && buffer.array() == nuSamples.get(lastIndex).array() && nuSamples.get(lastIndex).arrayOffset()
                        + nuSamples.get(lastIndex).limit() == buffer.arrayOffset()) {
            ByteBuffer oldBuffer = nuSamples.remove(lastIndex);
            ByteBuffer nu = ByteBuffer
                    .wrap(buffer.array(), oldBuffer.arrayOffset(), oldBuffer.limit() + buffer.limit()).slice();
            // We need to slice here since wrap([], offset, length) just sets position and not the arrayOffset.
            nuSamples.add(nu);//from   ww  w.java 2  s. c o  m
        } else if (lastIndex >= 0 && buffer instanceof MappedByteBuffer
                && nuSamples.get(lastIndex) instanceof MappedByteBuffer && nuSamples.get(lastIndex)
                        .limit() == nuSamples.get(lastIndex).capacity() - buffer.capacity()) {
            // This can go wrong - but will it?
            ByteBuffer oldBuffer = nuSamples.get(lastIndex);
            oldBuffer.limit(buffer.limit() + oldBuffer.limit());
        } else {
            buffer.reset();
            nuSamples.add(buffer);
        }
    }
    return nuSamples;
}