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

Java examples for Internationalization:Charset

Description

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

Demo Code

/*/*  ww w  .  j  a v  a 2 s. c  o  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 };
        System.out.println(java.util.Arrays
                .toString(convertToLongArray(vals)));
    }

    /**
     * Converts <code>byte[]</code> to <code>long[]</code>, assuming big-endian byte order.
     * @param vals source data
     * @return converted data
     * @throws IllegalArgumentException If length of <code>vals</code> is not a multiple of 8
     */
    public static long[] convertToLongArray(final byte[] vals) {
        checkSource(vals.length, 8);
        final long[] dest = new long[vals.length / 8];
        convertToLongArrayInternal(vals, 0, vals.length, dest, 0);
        return dest;
    }

    /**
     * Converts <code>byte[]</code> to <code>long[]</code>, assuming big-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 convertToLongArray(final byte[] vals,
            final long[] dest) {
        checkSource(vals.length, 8);
        checkDestination(vals.length, dest.length, 8);
        return convertToLongArrayInternal(vals, 0, vals.length, dest, 0);
    }

    /**
     * Converts <code>byte[]</code> to <code>long[]</code>, assuming big-endian byte order.
     * @param src source data
     * @param sFrom starting source position
     * @param sTo ending source position (excl)
     * @param dest array to put longs into
     * @param dFrom starting destination position
     * @param dTo ending destination position (excl)
     * @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 convertToLongArray(final byte[] src, final int sFrom,
            final int sTo, final long[] dest, final int dFrom, final int dTo) {
        final int length = sTo - sFrom;
        checkSource(length, 8);
        checkDestination(length, dTo - dFrom, 8);
        return convertToLongArrayInternal(src, sFrom, sTo, dest, dFrom);
    }

    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 int convertToLongArrayInternal(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] << 56)
                    + ((long) (src[j + 1] & 0xFF) << 48)
                    + ((long) (src[j + 2] & 0xFF) << 40)
                    + ((long) (src[j + 3] & 0xFF) << 32)
                    + ((long) (src[j + 4] & 0xFF) << 24)
                    + ((src[j + 5] & 0xFF) << 16)
                    + ((src[j + 6] & 0xFF) << 8) + (src[j + 7] & 0xFF);
        }
        return i - dFrom;
    }

    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);
        }
    }
}

Related Tutorials