Java Long Number Array Create toLongArray(byte[] byteArray)

Here you can find the source of toLongArray(byte[] byteArray)

Description

Convert an array of bytes into an array of longs.

License

Open Source License

Parameter

Parameter Description
byteArray The input byte array.

Return

The returned long array.

Declaration

public static long[] toLongArray(byte[] byteArray) 

Method Source Code

//package com.java2s;
/*//w w w .  j  a v  a  2s .  c  o  m
This file is part of PH-Tree:
A multi-dimensional indexing and storage structure.
    
Copyright (C) 2011-2015
Eidgen?ssische Technische Hochschule Z?rich (ETH Zurich)
Institute for Information Systems
GlobIS Group
Bogdan Vancea, Tilmann Zaeschke
zaeschke@inf.ethz.ch or zoodb@gmx.de
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Convert an array of bytes into an array of longs. This is done by combining sets
     * of 8 consecutive bytes into a long in an big-endian manner.
     *
     * @param byteArray             The input byte array.
     * @return                      The returned long array.
     */
    public static long[] toLongArray(byte[] byteArray) {
        return getLongArray(byteArray, 0, byteArray.length / 8);
    }

    /**
     * Convert an array of bytes into an array of longs. This is done by combining sets
     * of 8 consecutive bytes into a long in an big-endian manner.
     *
     * This method allow to use bytes starting from an arbitrary position in the byte[] array and
     * to create as many longs as specified by the argument.
     *
     * @param byteArray             The input byte array.
     * @param offset                The offset from which to start the processing of the array.
     * @param longArraySize         The number of longs to be parsed
     * @return                      The returned long array.
     */
    public static long[] getLongArray(byte[] byteArray, int offset, int longArraySize) {
        int longByteSize = 8;
        //int longArraySize= byteArray.length / longByteSize;
        long[] longArray = new long[longArraySize];

        //for each long
        for (int i = 0; i < longArraySize; i++) {

            //for each for the 8 bytes
            for (int j = 0; j < longByteSize; j++) {
                byte currentByte = byteArray[offset + i * longByteSize + j];

                //shift the current byte left to align it with the long
                long mask = ((long) currentByte) << ((7 - j) * longByteSize);

                //make all bit preceding current bit 0
                mask &= 255L << (7 - j) * longByteSize;

                //only set the bytes in the mask
                longArray[i] |= mask;
            }
        }
        return longArray;
    }
}

Related

  1. LongArrayFrom(long[] array)
  2. LongArrayFrom(long[] array)
  3. toLongA(byte[] data)
  4. toLongArray(boolean[] array)
  5. toLongArray(byte[] array)
  6. toLongArray(byte[] byteArray)
  7. toLongArray(byte[] data)
  8. toLongArray(double[][] array)
  9. toLongArray(final byte[] array)