Java ByteBuffer to Byte Array toArray(ByteBuffer buffer)

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

Description

Returns the content of buffer as byte array.

License

Open Source License

Declaration

public static byte[] toArray(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

import java.nio.ByteBuffer;

public class Main {
    /** Returns the content of {@code buffer} as byte array. */
    public static byte[] toArray(ByteBuffer buffer) {
        byte[] retVal = null;
        try {/*ww w.j  a  v  a 2s  .  c  o m*/
            retVal = buffer.array();
        } catch (Throwable t) {
        }
        if (retVal == null || retVal.length != buffer.limit()) {
            retVal = new byte[buffer.limit()];
            int pos = buffer.position();
            buffer.position(0);
            buffer.get(retVal);
            buffer.position(pos);
        }
        return retVal;
    }
}

Related

  1. toArray(ByteBuffer buffer)
  2. toArray(ByteBuffer buffer)
  3. toArray(ByteBuffer buffer)
  4. toArray(ByteBuffer buffer)
  5. toArray(ByteBuffer buffer)
  6. toArray(ByteBuffer bytebuffer)
  7. toArray(final ByteBuffer b)
  8. toArray(final ByteBuffer buffer)
  9. toByteArray(ByteBuffer bb)