0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.util;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * A packed array of booleans.
0N/A *
0N/A * @author Joshua Bloch
0N/A * @author Douglas Hoover
0N/A */
0N/A
0N/Apublic class BitArray {
0N/A
0N/A private byte[] repn;
0N/A private int length;
0N/A
0N/A private static final int BITS_PER_UNIT = 8;
0N/A
0N/A private static int subscript(int idx) {
0N/A return idx / BITS_PER_UNIT;
0N/A }
0N/A
0N/A private static int position(int idx) { // bits big-endian in each unit
0N/A return 1 << (BITS_PER_UNIT - 1 - (idx % BITS_PER_UNIT));
0N/A }
0N/A
0N/A /**
0N/A * Creates a BitArray of the specified size, initialized to zeros.
0N/A */
0N/A public BitArray(int length) throws IllegalArgumentException {
0N/A if (length < 0) {
0N/A throw new IllegalArgumentException("Negative length for BitArray");
0N/A }
0N/A
0N/A this.length = length;
0N/A
0N/A repn = new byte[(length + BITS_PER_UNIT - 1)/BITS_PER_UNIT];
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a BitArray of the specified size, initialized from the
0N/A * specified byte array. The most significant bit of a[0] gets
0N/A * index zero in the BitArray. The array a must be large enough
0N/A * to specify a value for every bit in the BitArray. In other words,
0N/A * 8*a.length <= length.
0N/A */
0N/A public BitArray(int length, byte[] a) throws IllegalArgumentException {
0N/A
0N/A if (length < 0) {
0N/A throw new IllegalArgumentException("Negative length for BitArray");
0N/A }
0N/A if (a.length * BITS_PER_UNIT < length) {
0N/A throw new IllegalArgumentException("Byte array too short to represent " +
0N/A "bit array of given length");
0N/A }
0N/A
0N/A this.length = length;
0N/A
0N/A int repLength = ((length + BITS_PER_UNIT - 1)/BITS_PER_UNIT);
0N/A int unusedBits = repLength*BITS_PER_UNIT - length;
0N/A byte bitMask = (byte) (0xFF << unusedBits);
0N/A
0N/A /*
0N/A normalize the representation:
0N/A 1. discard extra bytes
0N/A 2. zero out extra bits in the last byte
0N/A */
0N/A repn = new byte[repLength];
0N/A System.arraycopy(a, 0, repn, 0, repLength);
0N/A if (repLength > 0) {
0N/A repn[repLength - 1] &= bitMask;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create a BitArray whose bits are those of the given array
0N/A * of Booleans.
0N/A */
0N/A public BitArray(boolean[] bits) {
0N/A length = bits.length;
0N/A repn = new byte[(length + 7)/8];
0N/A
0N/A for (int i=0; i < length; i++) {
0N/A set(i, bits[i]);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Copy constructor (for cloning).
0N/A */
0N/A private BitArray(BitArray ba) {
0N/A length = ba.length;
0N/A repn = ba.repn.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the indexed bit in this BitArray.
0N/A */
0N/A public boolean get(int index) throws ArrayIndexOutOfBoundsException {
0N/A if (index < 0 || index >= length) {
0N/A throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
0N/A }
0N/A
0N/A return (repn[subscript(index)] & position(index)) != 0;
0N/A }
0N/A
0N/A /**
0N/A * Sets the indexed bit in this BitArray.
0N/A */
0N/A public void set(int index, boolean value)
0N/A throws ArrayIndexOutOfBoundsException {
0N/A if (index < 0 || index >= length) {
0N/A throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
0N/A }
0N/A int idx = subscript(index);
0N/A int bit = position(index);
0N/A
0N/A if (value) {
0N/A repn[idx] |= bit;
0N/A } else {
0N/A repn[idx] &= ~bit;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the length of this BitArray.
0N/A */
0N/A public int length() {
0N/A return length;
0N/A }
0N/A
0N/A /**
0N/A * Returns a Byte array containing the contents of this BitArray.
0N/A * The bit stored at index zero in this BitArray will be copied
0N/A * into the most significant bit of the zeroth element of the
0N/A * returned byte array. The last byte of the returned byte array
0N/A * will be contain zeros in any bits that do not have corresponding
0N/A * bits in the BitArray. (This matters only if the BitArray's size
0N/A * is not a multiple of 8.)
0N/A */
0N/A public byte[] toByteArray() {
0N/A return repn.clone();
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A if (obj == this) return true;
0N/A if (obj == null || !(obj instanceof BitArray)) return false;
0N/A
0N/A BitArray ba = (BitArray) obj;
0N/A
0N/A if (ba.length != length) return false;
0N/A
0N/A for (int i = 0; i < repn.length; i += 1) {
0N/A if (repn[i] != ba.repn[i]) return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Return a boolean array with the same bit values a this BitArray.
0N/A */
0N/A public boolean[] toBooleanArray() {
0N/A boolean[] bits = new boolean[length];
0N/A
0N/A for (int i=0; i < length; i++) {
0N/A bits[i] = get(i);
0N/A }
0N/A return bits;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code value for this bit array.
0N/A *
0N/A * @return a hash code value for this bit array.
0N/A */
0N/A public int hashCode() {
0N/A int hashCode = 0;
0N/A
0N/A for (int i = 0; i < repn.length; i++)
0N/A hashCode = 31*hashCode + repn[i];
0N/A
0N/A return hashCode ^ length;
0N/A }
0N/A
0N/A
0N/A public Object clone() {
0N/A return new BitArray(this);
0N/A }
0N/A
0N/A
0N/A private static final byte[][] NYBBLE = {
0N/A { (byte)'0',(byte)'0',(byte)'0',(byte)'0'},
0N/A { (byte)'0',(byte)'0',(byte)'0',(byte)'1'},
0N/A { (byte)'0',(byte)'0',(byte)'1',(byte)'0'},
0N/A { (byte)'0',(byte)'0',(byte)'1',(byte)'1'},
0N/A { (byte)'0',(byte)'1',(byte)'0',(byte)'0'},
0N/A { (byte)'0',(byte)'1',(byte)'0',(byte)'1'},
0N/A { (byte)'0',(byte)'1',(byte)'1',(byte)'0'},
0N/A { (byte)'0',(byte)'1',(byte)'1',(byte)'1'},
0N/A { (byte)'1',(byte)'0',(byte)'0',(byte)'0'},
0N/A { (byte)'1',(byte)'0',(byte)'0',(byte)'1'},
0N/A { (byte)'1',(byte)'0',(byte)'1',(byte)'0'},
0N/A { (byte)'1',(byte)'0',(byte)'1',(byte)'1'},
0N/A { (byte)'1',(byte)'1',(byte)'0',(byte)'0'},
0N/A { (byte)'1',(byte)'1',(byte)'0',(byte)'1'},
0N/A { (byte)'1',(byte)'1',(byte)'1',(byte)'0'},
0N/A { (byte)'1',(byte)'1',(byte)'1',(byte)'1'}
0N/A };
0N/A
0N/A private static final int BYTES_PER_LINE = 8;
0N/A
0N/A /**
0N/A * Returns a string representation of this BitArray.
0N/A */
0N/A public String toString() {
0N/A ByteArrayOutputStream out = new ByteArrayOutputStream();
0N/A
0N/A for (int i = 0; i < repn.length - 1; i++) {
0N/A out.write(NYBBLE[(repn[i] >> 4) & 0x0F], 0, 4);
0N/A out.write(NYBBLE[repn[i] & 0x0F], 0, 4);
0N/A
0N/A if (i % BYTES_PER_LINE == BYTES_PER_LINE - 1) {
0N/A out.write('\n');
0N/A } else {
0N/A out.write(' ');
0N/A }
0N/A }
0N/A
0N/A // in last byte of repn, use only the valid bits
0N/A for (int i = BITS_PER_UNIT * (repn.length - 1); i < length; i++) {
0N/A out.write(get(i) ? '1' : '0');
0N/A }
0N/A
0N/A return new String(out.toByteArray());
0N/A
0N/A }
0N/A
0N/A public BitArray truncate() {
0N/A for (int i=length-1; i>=0; i--) {
0N/A if (get(i)) {
0N/A return new BitArray(i+1, Arrays.copyOf(repn, (i + BITS_PER_UNIT)/BITS_PER_UNIT));
0N/A }
0N/A }
0N/A return new BitArray(1);
0N/A }
0N/A
0N/A}