Java Utililty Methods ByteBuffer Create

List of utility methods to do ByteBuffer Create

Description

The list of methods to do ByteBuffer Create are organized into topic(s).

Method

ByteBuffercreateByteBuffer(int size)
create Byte Buffer
totalsize += size;
return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
ByteBuffercreateByteBuffer(int size)
Create a direct byte buffer with the given size
return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
ByteBuffercreateByteBufferOnHeap(int size)
Create a new ByteBuffer of the specified size.
ByteBuffer buf = ByteBuffer.allocate(size).order(ByteOrder.nativeOrder());
buf.clear();
return buf;
ByteBuffercreateCopy(ByteBuffer buffer)
create Copy
buffer.rewind();
ByteBuffer copy = ByteBuffer.allocate(buffer.limit());
copy.order(buffer.order());
copy.put(buffer);
return copy;
java.nio.ByteBuffercreateDirectByteBuffer(byte[] data)
create Direct Byte Buffer
if (data == null) {
    return null;
java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocateDirect((Byte.SIZE / 8) * data.length)
        .order(java.nio.ByteOrder.nativeOrder());
buf.clear();
buf.put(data);
buf.flip();
...
ByteBuffer[]createIconsFromBuffers(List byteBuffers)
create Icons From Buffers
ByteBuffer[] icons = new ByteBuffer[byteBuffers.size()];
for (int i = 0; i < byteBuffers.size(); i++) {
    icons[i] = byteBuffers.get(i);
return icons;
InputStreamcreateInputStream(final ByteBuffer buf)
create Input Stream
return new InputStream() {
    @Override
    public int read() throws IOException {
        if (!buf.hasRemaining()) {
            return -1;
        return buf.get() & 0xFF;
    @Override
    public int read(byte[] bytes, int off, int len) throws IOException {
        if (!buf.hasRemaining()) {
            return -1;
        len = Math.min(len, buf.remaining());
        buf.get(bytes, off, len);
        return len;
};
byte[]newByteArrayFromByteBuffer(ByteBuffer buf)
Creates a byte array from the given ByteBuffer, the position property of input ByteBuffer remains unchanged.
final int length = buf.remaining();
byte[] bytes = new byte[length];
buf.duplicate().get(bytes, 0, length);
return bytes;
ByteBuffernewByteBuffer(int bufferSize, boolean direct)
new Byte Buffer
if (direct) {
    return ByteBuffer.allocateDirect(bufferSize);
} else {
    return ByteBuffer.allocate(bufferSize);
ByteBuffernewByteBuffer(int len)
new Byte Buffer
return ByteBuffer.allocateDirect(len).order(ByteOrder.nativeOrder());