Java ByteBuffer to Long readLongArray(ByteBuffer bb)

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

Description

read Long Array

License

Open Source License

Declaration

static public long[] readLongArray(ByteBuffer bb) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2007(c) G?nome Qu?bec. All rights reserved.
 * // ww  w . j  a  v a2  s .  c o m
 * This file is part of GenoByte.
 * 
 * GenoByte 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.
 * 
 * GenoByte 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 {
    static public long[] readLongArray(ByteBuffer bb) {
        long longs[] = null;
        int size = bb.getInt();
        if (size >= 0) {
            longs = new long[size];

            // It has been shown that using a bulk read operation is much faster than calling bb.readLong() for every expected long value.
            // This is due to the underlying HeapByteBuffer implementation that calls the get() method for each byte of each long.
            // This quickly gets out of hand: a vector of 500 000 bits would call get() 62,504 times. A field of dimension 3 would call it 187,512 times. 
            // Loading such a field from a store of 2000 records would call it 375,024,000 times...!

            byte[] b = new byte[size * 8];
            bb.get(b);
            int l = 0;

            // Transform the array of bytes into an array of longs. This is a BIG_ENDIAN transform, if STORED_BYTE_ORDER is not BIG_ENDIAN, this will break.
            for (int i = 0; i < b.length;) {
                longs[l++] = ((((long) b[i++] & 0xff) << 56) | (((long) b[i++] & 0xff) << 48)
                        | (((long) b[i++] & 0xff) << 40) | (((long) b[i++] & 0xff) << 32)
                        | (((long) b[i++] & 0xff) << 24) | (((long) b[i++] & 0xff) << 16)
                        | (((long) b[i++] & 0xff) << 8) | (((long) b[i++] & 0xff) << 0));
            }
        }
        return longs;
    }
}

Related

  1. readLong(ByteBuffer buffer)
  2. readLong(ByteBuffer in)
  3. readLong(ByteBuffer in, final int fitInBytes)
  4. readLong(ByteBuffer logBuf)
  5. readLong64ls(int size, ByteBuffer byteBuf)
  6. readLongLE(ByteBuffer buf, int i)
  7. readUB1(ByteBuffer buffer)
  8. readUB4(ByteBuffer buffer)
  9. readUBEInt16(ByteBuffer b)