Path: blob/master/src/java.base/share/classes/sun/security/util/BitArray.java
41159 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.io.ByteArrayOutputStream;28import java.util.Arrays;2930/**31* A packed array of booleans.32*33* @author Joshua Bloch34* @author Douglas Hoover35*/3637public class BitArray {3839private byte[] repn;40private int length;4142private static final int BITS_PER_UNIT = 8;4344private static int subscript(int idx) {45return idx / BITS_PER_UNIT;46}4748private static int position(int idx) { // bits big-endian in each unit49return 1 << (BITS_PER_UNIT - 1 - (idx % BITS_PER_UNIT));50}5152/**53* Creates a BitArray of the specified size, initialized to zeros.54*/55public BitArray(int length) throws IllegalArgumentException {56if (length < 0) {57throw new IllegalArgumentException("Negative length for BitArray");58}5960this.length = length;6162repn = new byte[(length + BITS_PER_UNIT - 1)/BITS_PER_UNIT];63}646566/**67* Creates a BitArray of the specified size, initialized from the68* specified byte array. The most significant bit of {@code a[0]} gets69* index zero in the BitArray. The array a must be large enough70* to specify a value for every bit in the BitArray. In other words,71* {@code 8*a.length <= length}.72*/73public BitArray(int length, byte[] a) throws IllegalArgumentException {7475if (length < 0) {76throw new IllegalArgumentException("Negative length for BitArray");77}78if (a.length * BITS_PER_UNIT < length) {79throw new IllegalArgumentException("Byte array too short to represent " +80"bit array of given length");81}8283this.length = length;8485int repLength = ((length + BITS_PER_UNIT - 1)/BITS_PER_UNIT);86int unusedBits = repLength*BITS_PER_UNIT - length;87byte bitMask = (byte) (0xFF << unusedBits);8889/*90normalize the representation:911. discard extra bytes922. zero out extra bits in the last byte93*/94repn = new byte[repLength];95System.arraycopy(a, 0, repn, 0, repLength);96if (repLength > 0) {97repn[repLength - 1] &= bitMask;98}99}100101/**102* Create a BitArray whose bits are those of the given array103* of Booleans.104*/105public BitArray(boolean[] bits) {106length = bits.length;107repn = new byte[(length + 7)/8];108109for (int i=0; i < length; i++) {110set(i, bits[i]);111}112}113114115/**116* Copy constructor (for cloning).117*/118private BitArray(BitArray ba) {119length = ba.length;120repn = ba.repn.clone();121}122123/**124* Returns the indexed bit in this BitArray.125*/126public boolean get(int index) throws ArrayIndexOutOfBoundsException {127if (index < 0 || index >= length) {128throw new ArrayIndexOutOfBoundsException(Integer.toString(index));129}130131return (repn[subscript(index)] & position(index)) != 0;132}133134/**135* Sets the indexed bit in this BitArray.136*/137public void set(int index, boolean value)138throws ArrayIndexOutOfBoundsException {139if (index < 0 || index >= length) {140throw new ArrayIndexOutOfBoundsException(Integer.toString(index));141}142int idx = subscript(index);143int bit = position(index);144145if (value) {146repn[idx] |= bit;147} else {148repn[idx] &= ~bit;149}150}151152/**153* Returns the length of this BitArray.154*/155public int length() {156return length;157}158159/**160* Returns a Byte array containing the contents of this BitArray.161* The bit stored at index zero in this BitArray will be copied162* into the most significant bit of the zeroth element of the163* returned byte array. The last byte of the returned byte array164* will be contain zeros in any bits that do not have corresponding165* bits in the BitArray. (This matters only if the BitArray's size166* is not a multiple of 8.)167*/168public byte[] toByteArray() {169return repn.clone();170}171172public boolean equals(Object obj) {173if (obj == this) return true;174if (obj == null || !(obj instanceof BitArray)) return false;175176BitArray ba = (BitArray) obj;177178if (ba.length != length) return false;179180for (int i = 0; i < repn.length; i += 1) {181if (repn[i] != ba.repn[i]) return false;182}183return true;184}185186/**187* Return a boolean array with the same bit values a this BitArray.188*/189public boolean[] toBooleanArray() {190boolean[] bits = new boolean[length];191192for (int i=0; i < length; i++) {193bits[i] = get(i);194}195return bits;196}197198/**199* Returns a hash code value for this bit array.200*201* @return a hash code value for this bit array.202*/203public int hashCode() {204int hashCode = 0;205206for (int i = 0; i < repn.length; i++)207hashCode = 31*hashCode + repn[i];208209return hashCode ^ length;210}211212213public Object clone() {214return new BitArray(this);215}216217218private static final byte[][] NYBBLE = {219{ (byte)'0',(byte)'0',(byte)'0',(byte)'0'},220{ (byte)'0',(byte)'0',(byte)'0',(byte)'1'},221{ (byte)'0',(byte)'0',(byte)'1',(byte)'0'},222{ (byte)'0',(byte)'0',(byte)'1',(byte)'1'},223{ (byte)'0',(byte)'1',(byte)'0',(byte)'0'},224{ (byte)'0',(byte)'1',(byte)'0',(byte)'1'},225{ (byte)'0',(byte)'1',(byte)'1',(byte)'0'},226{ (byte)'0',(byte)'1',(byte)'1',(byte)'1'},227{ (byte)'1',(byte)'0',(byte)'0',(byte)'0'},228{ (byte)'1',(byte)'0',(byte)'0',(byte)'1'},229{ (byte)'1',(byte)'0',(byte)'1',(byte)'0'},230{ (byte)'1',(byte)'0',(byte)'1',(byte)'1'},231{ (byte)'1',(byte)'1',(byte)'0',(byte)'0'},232{ (byte)'1',(byte)'1',(byte)'0',(byte)'1'},233{ (byte)'1',(byte)'1',(byte)'1',(byte)'0'},234{ (byte)'1',(byte)'1',(byte)'1',(byte)'1'}235};236237private static final int BYTES_PER_LINE = 8;238239/**240* Returns a string representation of this BitArray.241*/242public String toString() {243if (length == 0) {244return "";245}246247ByteArrayOutputStream out = new ByteArrayOutputStream();248249for (int i = 0; i < repn.length - 1; i++) {250out.write(NYBBLE[(repn[i] >> 4) & 0x0F], 0, 4);251out.write(NYBBLE[repn[i] & 0x0F], 0, 4);252253if (i % BYTES_PER_LINE == BYTES_PER_LINE - 1) {254out.write('\n');255} else {256out.write(' ');257}258}259260// in last byte of repn, use only the valid bits261for (int i = BITS_PER_UNIT * (repn.length - 1); i < length; i++) {262out.write(get(i) ? '1' : '0');263}264265return new String(out.toByteArray());266267}268269public BitArray truncate() {270for (int i=length-1; i>=0; i--) {271if (get(i)) {272return new BitArray(i+1, Arrays.copyOf(repn, (i + BITS_PER_UNIT)/BITS_PER_UNIT));273}274}275return new BitArray(1);276}277278}279280281