Java ByteBuffer to Byte Array toByteArray(ByteBuffer buffer)

Here you can find the source of toByteArray(ByteBuffer buffer)

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(ByteBuffer buffer) 

Method Source Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public static byte[] toByteArray(ByteBuffer buffer) {
        if (buffer.hasArray())
            return buffer.array();
        byte[] result = new byte[buffer.capacity()];
        buffer.rewind();//from  w w  w.  j  a  va  2s. c om
        buffer.get(result);
        return result;
    }

    public static <T> T get(T[] a, int idx, T defaultValue) {
        return idx > 0 && idx < a.length ? a[idx] : defaultValue;
    }
}

Related

  1. toArray(final ByteBuffer buffer)
  2. toByteArray(ByteBuffer bb)
  3. toByteArray(ByteBuffer buf)
  4. toByteArray(ByteBuffer buffer)
  5. toByteArray(ByteBuffer buffer)
  6. toByteArray(ByteBuffer buffer)
  7. toByteArray(ByteBuffer buffer, int length)
  8. toByteArray(ByteBuffer buffer, int length)
  9. toByteArray(ByteBuffer bytes)