src/java.base/share/classes/sun/security/util/BitArray.java
author weijun
Wed, 20 Nov 2019 08:12:14 +0800
changeset 59141 bd436284147d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234377: new BitArray(0).toString() throws ArrayIndexOutOfBoundsException Reviewed-by: mullan, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59141
bd436284147d 8234377: new BitArray(0).toString() throws ArrayIndexOutOfBoundsException
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A packed array of booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Joshua Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Douglas Hoover
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class BitArray {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private byte[] repn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int BITS_PER_UNIT = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static int subscript(int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        return idx / BITS_PER_UNIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static int position(int idx) { // bits big-endian in each unit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return 1 << (BITS_PER_UNIT - 1 - (idx % BITS_PER_UNIT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Creates a BitArray of the specified size, initialized to zeros.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public BitArray(int length) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new IllegalArgumentException("Negative length for BitArray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        repn = new byte[(length + BITS_PER_UNIT - 1)/BITS_PER_UNIT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a BitArray of the specified size, initialized from the
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    69
     * specified byte array.  The most significant bit of {@code a[0]} gets
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * index zero in the BitArray.  The array a must be large enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * to specify a value for every bit in the BitArray.  In other words,
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    72
     * {@code 8*a.length <= length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public BitArray(int length, byte[] a) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new IllegalArgumentException("Negative length for BitArray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (a.length * BITS_PER_UNIT < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new IllegalArgumentException("Byte array too short to represent " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                               "bit array of given length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int repLength = ((length + BITS_PER_UNIT - 1)/BITS_PER_UNIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int unusedBits = repLength*BITS_PER_UNIT - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        byte bitMask = (byte) (0xFF << unusedBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         normalize the representation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
          1. discard extra bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
          2. zero out extra bits in the last byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        repn = new byte[repLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.arraycopy(a, 0, repn, 0, repLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (repLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            repn[repLength - 1] &= bitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Create a BitArray whose bits are those of the given array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * of Booleans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public BitArray(boolean[] bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        length = bits.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        repn = new byte[(length + 7)/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int i=0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            set(i, bits[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *  Copy constructor (for cloning).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private BitArray(BitArray ba) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        length = ba.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        repn = ba.repn.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *  Returns the indexed bit in this BitArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public boolean get(int index) throws ArrayIndexOutOfBoundsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (index < 0 || index >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return (repn[subscript(index)] & position(index)) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *  Sets the indexed bit in this BitArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void set(int index, boolean value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    throws ArrayIndexOutOfBoundsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (index < 0 || index >= length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int idx = subscript(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int bit = position(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            repn[idx] |= bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            repn[idx] &= ~bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the length of this BitArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns a Byte array containing the contents of this BitArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The bit stored at index zero in this BitArray will be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * into the most significant bit of the zeroth element of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * returned byte array.  The last byte of the returned byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * will be contain zeros in any bits that do not have corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * bits in the BitArray.  (This matters only if the BitArray's size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * is not a multiple of 8.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public byte[] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return repn.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (obj == this) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (obj == null || !(obj instanceof BitArray)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        BitArray ba = (BitArray) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (ba.length != length) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (int i = 0; i < repn.length; i += 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (repn[i] != ba.repn[i]) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Return a boolean array with the same bit values a this BitArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public boolean[] toBooleanArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        boolean[] bits = new boolean[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (int i=0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            bits[i] = get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns a hash code value for this bit array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return  a hash code value for this bit array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        for (int i = 0; i < repn.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            hashCode = 31*hashCode + repn[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return hashCode ^ length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new BitArray(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static final byte[][] NYBBLE = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        { (byte)'0',(byte)'0',(byte)'0',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        { (byte)'0',(byte)'0',(byte)'0',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        { (byte)'0',(byte)'0',(byte)'1',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        { (byte)'0',(byte)'0',(byte)'1',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        { (byte)'0',(byte)'1',(byte)'0',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        { (byte)'0',(byte)'1',(byte)'0',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        { (byte)'0',(byte)'1',(byte)'1',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        { (byte)'0',(byte)'1',(byte)'1',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        { (byte)'1',(byte)'0',(byte)'0',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        { (byte)'1',(byte)'0',(byte)'0',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        { (byte)'1',(byte)'0',(byte)'1',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        { (byte)'1',(byte)'0',(byte)'1',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        { (byte)'1',(byte)'1',(byte)'0',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        { (byte)'1',(byte)'1',(byte)'0',(byte)'1'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        { (byte)'1',(byte)'1',(byte)'1',(byte)'0'},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        { (byte)'1',(byte)'1',(byte)'1',(byte)'1'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static final int BYTES_PER_LINE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *  Returns a string representation of this BitArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public String toString() {
59141
bd436284147d 8234377: new BitArray(0).toString() throws ArrayIndexOutOfBoundsException
weijun
parents: 47216
diff changeset
   244
        if (length == 0) {
bd436284147d 8234377: new BitArray(0).toString() throws ArrayIndexOutOfBoundsException
weijun
parents: 47216
diff changeset
   245
            return "";
bd436284147d 8234377: new BitArray(0).toString() throws ArrayIndexOutOfBoundsException
weijun
parents: 47216
diff changeset
   246
        }
bd436284147d 8234377: new BitArray(0).toString() throws ArrayIndexOutOfBoundsException
weijun
parents: 47216
diff changeset
   247
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        ByteArrayOutputStream out = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 0; i < repn.length - 1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            out.write(NYBBLE[(repn[i] >> 4) & 0x0F], 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            out.write(NYBBLE[repn[i] & 0x0F], 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (i % BYTES_PER_LINE == BYTES_PER_LINE - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                out.write('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                out.write(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // in last byte of repn, use only the valid bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (int i = BITS_PER_UNIT * (repn.length - 1); i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            out.write(get(i) ? '1' : '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return new String(out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public BitArray truncate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (int i=length-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return new BitArray(i+1, Arrays.copyOf(repn, (i + BITS_PER_UNIT)/BITS_PER_UNIT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return new BitArray(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
}