Converts byte[] to long[], assuming little-endian byte order. - Java Internationalization

Java examples for Internationalization:Charset

Description

Converts byte[] to long[], assuming little-endian byte order.

Demo Code

/*//from   w  w w  .  j av  a 2 s.  co m
 * Copyright (c) 2014. Real Time Genomics Limited.
 *
 * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement
 * for Academic Non-commercial Research Purposes only.
 *
 * If you did not receive a license accompanying this file, a copy must first be obtained by email
 * from support@realtimegenomics.com.  On downloading, using and/or continuing to use this source
 * code you accept the terms of that license agreement and any amendments to those terms that may
 * be made from time to time by Real Time Genomics Limited.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] vals = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        long[] dest = new long[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(convertToLongArrayLittleEndian(vals, dest));
    }

    /**
     * Converts <code>byte[]</code> to <code>long[]</code>, assuming little-endian byte order.
     * @param vals source data
     * @param dest array to put longs into
     * @return space taken in destination
     * @throws IllegalArgumentException If length of <code>vals</code> is not a multiple of 8
     * or length of <code>dest</code> is not length of <code>vals</code> divided by 8 or greater
     */
    public static int convertToLongArrayLittleEndian(final byte[] vals,
            final long[] dest) {
        checkSource(vals.length, 8);
        checkDestination(vals.length, dest.length, 8);
        return convertToLongArrayLittleEndianInternal(vals, 0, vals.length,
                dest, 0);
    }

    private static void checkSource(final int length, final int mod) {
        if (length % mod != 0) {
            throw new IllegalArgumentException(
                    "Source data length needs to be multiple of " + mod
                            + " was: " + length);
        }
    }

    private static void checkDestination(final int sLength,
            final int dLength, final int mod) {
        final int lLength = sLength / mod;
        if (dLength < lLength) {
            throw new IllegalArgumentException(
                    "Destination length needs to be at least: " + lLength
                            + " was: " + dLength);
        }
    }

    private static int convertToLongArrayLittleEndianInternal(
            final byte[] src, final int sFrom, final int sTo,
            final long[] dest, final int dFrom) {
        int i, j;
        for (i = dFrom, j = sFrom; j < sTo; i++, j += 8) {
            dest[i] = ((long) src[j + 7] << 56)
                    + ((long) (src[j + 6] & 0xFF) << 48)
                    + ((long) (src[j + 5] & 0xFF) << 40)
                    + ((long) (src[j + 4] & 0xFF) << 32)
                    + ((long) (src[j + 3] & 0xFF) << 24)
                    + ((src[j + 2] & 0xFF) << 16)
                    + ((src[j + 1] & 0xFF) << 8) + (src[j] & 0xFF);
        }
        return i - dFrom;
    }
}

Related Tutorials