Java Boolean From toBooleanA(byte[] data)

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

Description

to Boolean A

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static boolean[] toBooleanA(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 bools:
        for (int i = 0, j = 4, k = 7; i < bools.length; i++) {
            bools[i] = ((data[j] >> k--) & 0x01) == 1;
            if (k < 0) {
                j++;//  w  ww .j  a  v  a  2s  .c  o m
                k = 7;
            }
        }
        // ----------
        return bools;
    }

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

Related

  1. toBoolean(String value, boolean defaultValue)
  2. toBoolean(String value, boolean defaultValue)
  3. toBoolean(String value, boolean defaultValue)
  4. toBoolean(String value, boolean defaultValue)
  5. toBoolean(StringBuilder value)
  6. toBooleanArray(byte a, boolean[] buf)
  7. toBooleanArray(byte[] array)
  8. toBooleanArray(byte[] byteArray)
  9. toBooleanArray(byte[] data)