Java Boolean From toBooleanArray(byte[] data)

Here you can find the source of toBooleanArray(byte[] data)

Description

to Boolean Array

License

Open Source License

Declaration

public static boolean[] toBooleanArray(byte[] data) 

Method Source Code

//package com.java2s;
/*/*from   w ww .  jav a  2s  . c om*/
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * 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/>.
 */

public class Main {
    public static boolean[] toBooleanArray(byte[] data) {
        /**
         * Advanced Technique: Extract the boolean array's length from the first
         * four bytes in the char array, and then read the boolean array.
         */
        if ((data == null) || (data.length < 4)) {
            return null;
        }
        int len = toInt(new byte[] { data[0], data[1], data[2], data[3] });
        boolean[] bools = new boolean[len];
        /**
         * pack booleans.
         */
        for (int i = 0, j = 4, k = 7; i < bools.length; i++) {
            bools[i] = ((data[j] >> k--) & 0x01) == 1;
            if (k < 0) {
                j++;
                k = 7;
            }
        }
        return bools;
    }

    /**
     * Convert 4 bytes into an int.
     * <p>
     * This converts the 4 bytes into an int.
     *
     * @param byte3 The byte to be left-shifted 24 bits.
     * @param byte2 The byte to be left-shifted 16 bits.
     * @param byte1 The byte to be left-shifted 8 bits.
     * @param byte0 The byte that will not be left-shifted.
     * @return An int representing the bytes.
     */
    public static int toInt(byte byte3, byte byte2, byte byte1, byte byte0) {
        return toInt(toShort(byte3, byte2), toShort(byte1, byte0));
    }

    /**
     * Convert 2 shorts into an int.
     * <p>
     * This converts the 2 shorts into an int.
     *
     * @param mss The Most Significant Short.
     * @param lss The Least Significant Short.
     * @return An int representing the shorts.
     */
    public static int toInt(short mss, short lss) {
        return ((0xffff0000 & mss << 16) | (0x0000ffff & (int) lss));
    }

    public static int toInt(byte[] data) {
        if ((data == null) || (data.length != 4)) {
            return 0x0;
        }
        return ( // NOTE: type cast not necessary for int
        (0xff & data[0]) << 24 | (0xff & data[1]) << 16 | (0xff & data[2]) << 8 | (0xff & data[3]));
    }

    /**
     * Convert 2 bytes into a short.
     * <p>
     * This converts the 2 bytes into a short. The msb will be the high byte (8
     * bits) of the short, and the lsb will be the low byte (8 bits) of the short.
     *
     * @param msb The Most Significant Byte.
     * @param lsb The Least Significant Byte.
     * @return A short representing the bytes.
     */
    public static short toShort(byte msb, byte lsb) {
        return (short) ((0xff00 & (short) (msb << 8)) | (0x00ff & (short) lsb));
    }

    public static short toShort(byte[] data) {
        if ((data == null) || (data.length != 2)) {
            return 0x0;
        }
        return (short) ((0xff & data[0]) << 8 | (0xff & data[1]));
    }
}

Related

  1. toBoolean(StringBuilder value)
  2. toBooleanA(byte[] data)
  3. toBooleanArray(byte a, boolean[] buf)
  4. toBooleanArray(byte[] array)
  5. toBooleanArray(byte[] byteArray)
  6. toBooleanArray(final E[] array)
  7. toBooleanArray(final Object[] array)
  8. toBooleanArray(int[] intArray, int n)
  9. toBooleanArray(Object[] vs)