Here you can find the source of toArray(final ByteBuffer b)
Parameter | Description |
---|---|
b | The ByteBuffer . |
static public byte[] toArray(final ByteBuffer b)
//package com.java2s; /**// w w w.j av a2 s. co m Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. Contact: SYSTAP, LLC 4501 Tower Road Greensboro, NC 27410 licenses@bigdata.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.nio.ByteBuffer; public class Main { /** * Return a byte[] having the data in the {@link ByteBuffer} from the * {@link ByteBuffer#position()} to the {@link ByteBuffer#limit()}. The * position, limit, and mark are not affected by this operation. When the * {@link ByteBuffer} has a backing array, the array offset is ZERO (0), and * the {@link ByteBuffer#limit()} is equal to the * {@link ByteBuffer#capacity()} then the backing array is returned. * Otherwise, a new byte[] is allocated and the data are copied into that * byte[], which is then returned. * * @param b * The {@link ByteBuffer}. * * @return The byte[]. */ static public byte[] toArray(final ByteBuffer b) { return toArray(b, false/* forceCopy */, null/* dst */); } /** * Return a byte[] having the data in the {@link ByteBuffer} from the * {@link ByteBuffer#position()} to the {@link ByteBuffer#limit()}. The * position, limit, and mark are not affected by this operation. * <p> * Under certain circumstances it is possible and may be desirable to return * the backing {@link ByteBuffer#array}. This behavior is enabled by * <code>forceCopy := false</code>. * <p> * It is possible to return the backing byte[] when the {@link ByteBuffer} * has a backing array, the array offset is ZERO (0), and the * {@link ByteBuffer#limit()} is equal to the {@link ByteBuffer#capacity()} * then the backing array is returned. Otherwise, a new byte[] must be * allocated, and the data are copied into that byte[], which may then be * returned. * * @param b * The {@link ByteBuffer}. * @param forceCopy * When <code>false</code>, the backing array will be returned if * possible. * @param dst * A byte[] provided by the caller (optional). When non- * <code>null</code> and having a length GTE * {@link ByteBuffer#remaining()}, this array will be preferred * to a newly allocated array. * * @return The byte[] having the data. When <i>dst</i> is non- * <code>null</code> this MAY be the caller's array. When it is the * caller's array, it MAY be larger than the #of bytes actually * read. */ static public byte[] toArray(final ByteBuffer b, final boolean forceCopy, final byte[] dst) { if (!forceCopy && b.hasArray() && b.arrayOffset() == 0 && b.position() == 0) { // && b.limit() == b.capacity() final byte[] a = b.array(); if (a.length == b.limit()) { return a; } } /* * Copy the data into a byte[] using a read-only view on the buffer so * that we do not mess with its position, mark, or limit. */ final ByteBuffer tmp = b.asReadOnlyBuffer(); final int len = tmp.remaining(); final byte[] a = dst != null && dst.length >= len ? dst : new byte[len]; // Transfer only the available bytes. tmp.get(a, 0, len); return a; } }