Java ByteBuffer Compact toCompactArray(ByteBuffer src)

Here you can find the source of toCompactArray(ByteBuffer src)

Description

Return an appropriately sized byte array filled with the contents of src.

License

Open Source License

Parameter

Parameter Description
src a parameter

Declaration

static byte[] toCompactArray(ByteBuffer src) 

Method Source Code

//package com.java2s;
/*************************************************************************
*                                                                        *
*  This file is part of the 20n/act project.                             *
*  20n/act enables DNA prediction for synthetic biology/bioengineering.  *
*  Copyright (C) 2017 20n Labs, Inc.                                     *
*                                                                        *
*  Please direct all queries to act@20n.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, either version 3 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
*  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, see <http://www.gnu.org/licenses/>. *
*                                                                        *
*************************************************************************/

import java.nio.ByteBuffer;

public class Main {
    /**//w w w.  j a  va2s.c  o m
     * Return an appropriately sized byte array filled with the contents of src.
     *
     * The {@link ByteBuffer#array()} call returns the array that backs the buffer, which is sometimes larger than the
     * data contained in the buffer.  For instance, in this example:
     * <pre>
     * {@code
     *   ByteBuffer buffer = ByteBuffer.allocate(16);
     *   buffer.put("hello world!".getBytes());
     *   byte[] arr = buffer.array();
     * }
     * </pre>
     * {@code arr} will always have length 16, even though we've only written 12 bytes to it.
     *
     * @param src
     * @return
     */
    static byte[] toCompactArray(ByteBuffer src) {
        // Assume src is pre-flipped to avoid a double-flip.
        if (src.hasArray() && src.limit() >= src.capacity()) { // No need to compact if limit covers the full backing array.
            return src.array();
        }
        assertReadyForFreshRead(src);

        // Otherwise, we need to allocate and copy into a new array of the correct size.

        // TODO: is Arrays.copyOf any faster than this?  Ideally we want CoW semantics here...
        // Warning: do not use src.compact().  That does something entirely different.

        /* We use a byte array here rather than another byte buffer for two reasons:
         * 1) It's possible for byte buffers to reside off-heap (like in shared pages), and we definitely want the array on
         *    the heap.
         * 2) If the byte-buffer resides on the heap, it's allocation will be identical, and we save the object management
         *    overhead by not creating another ByteBuffer. */
        byte[] dest = new byte[src.limit()];
        src.get(dest); // Copy the contents of the buffer into our appropriately sized array.
        assert (src.position() == src.limit()); // All bytes should have been read.
        src.rewind(); // Be kind: rewind.
        return dest;
    }

    /**
     * Ensure that a buffer is prepared for reading from the beginning.
     * @param b The buffer to check.
     */
    static void assertReadyForFreshRead(ByteBuffer b) {
        assert (b.limit() != 0); // A zero limit would cause an immediate buffer underflow.
        assert (b.position() == 0); // A non-zero position means either the buffer hasn't been flipped or has been read from.
    }
}

Related

  1. compact(ByteBuffer buffer)
  2. compact(ByteBuffer buffer)
  3. compact(ByteBuffer buffer)
  4. compact(ByteBuffer[] buffers)