Java ByteBuffer to Byte Array toByteArray(ByteBuffer bb)

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

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(ByteBuffer bb) 

Method Source Code

//package com.java2s;
/*/*from ww  w .  j  ava  2  s  . c o  m*/
 * Copyright (c) 2012-2014, Parallel Universe Software Co. All rights reserved.
 * 
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *  
 *   or (per the licensee's choosing)
 *  
 * under the terms of the GNU Lesser General Public License version 3.0
 * as published by the Free Software Foundation.
 */

import java.nio.ByteBuffer;

public class Main {
    public static byte[] toByteArray(ByteBuffer bb) {
        if (bb.hasArray() && bb.arrayOffset() == 0 && bb.position() == 0) {
            return bb.array();
        } else {
            byte[] arr = new byte[bb.remaining()];
            int p = bb.position();
            bb.get(arr);
            bb.position(p);
            return arr;
        }
    }
}

Related

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