Check whether a given byte array contains only zeroes. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Check whether a given byte array contains only zeroes.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] array = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(byteArrayBlank(array));
    }/*  w w w .j av  a 2s  .  c  o m*/

    /**
     * Check whether a given byte array contains only zeroes.
     */
    public static final boolean byteArrayBlank(byte[] array) {
        return byteArraySliceBlank(array, 0, array.length);
    }

    /**
     * Check whether a byte array slice contains only zeroes.
     * 
     * @param array
     *            base array
     * @param start
     *            first offset of the slice to be scanned
     * @param length
     *            length of the slice to be scanned
     */
    public static final boolean byteArraySliceBlank(final byte[] array,
            final int start, final int length) {
        for (int i = start; i < start + length; i++) {
            if (array[i] != 0) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials