libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / java / security / der / BitString.java
blobac10be22e6b3bf4e1ae16114cb227fd0b797a6a7
1 /* BitString.java -- Java representation of the BIT STRING type.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.der;
41 import gnu.java.lang.CPStringBuilder;
43 import java.math.BigInteger;
44 import java.util.Arrays;
46 /**
47 * Immutable representation of a bit string, which is equivalent to a
48 * byte array except some number of the rightmost bits are ignored. For
49 * example, this could be the bit string:
51 * <pre> 00010101 11101101 11010xxx</pre>
53 * <p>Where the "xxx" represents three bits that should be ignored, and
54 * can have any value.
56 * @author Casey Marshall (csm@gnu.org)
58 public class BitString implements Cloneable, Comparable
61 // Fields.
62 // ------------------------------------------------------------------------
64 /** The bits themselves. */
65 private final byte[] bytes;
67 /**
68 * The exportable byte array. This array has the ignored bits
69 * removed.
71 private transient byte[] externBytes;
73 /** The number of bits ignored at the end of the byte array. */
74 private final int ignoredBits;
76 /** This bit string as a boolean array. */
77 private transient boolean[] boolVal;
79 // Constructors.
80 // ------------------------------------------------------------------------
82 /**
83 * Create a new bit string, shifting the given byte array if needed.
85 * @param bytes The byte array holding the bit string.
86 * @param ignoredBits The number of bits to ignore.
87 * @param doShift Pass true in this parameter if the byte array has
88 * not yet been shifted left by <i>ignoredBits</i>.
89 * @throws IllegalArgumentException If <i>ignoredBits</i> is negative
90 * or greater than 7.
91 * @throws NullPointerException If <i>bytes</i> is null.
93 public BitString(byte[] bytes, int ignoredBits, boolean doShift)
95 this(bytes, 0, bytes.length, ignoredBits, doShift);
98 /**
99 * Create a new bit string, shifting the given byte array if needed.
101 * @param bytes The byte array holding the bit string.
102 * @param offset The offset where the meaningful bytes begin.
103 * @param length The number of meaningful bytes.
104 * @param ignoredBits The number of bits to ignore.
105 * @param doShift Pass true in this parameter if the byte array has
106 * not yet been shifted left by <i>ignoredBits</i>.
107 * @throws IllegalArgumentException If <i>ignoredBits</i> is negative
108 * or greater than 7.
109 * @throws NullPointerException If <i>bytes</i> is null.
111 public BitString(byte[] bytes, int offset, int length,
112 int ignoredBits, boolean doShift)
114 if (ignoredBits < 0 || ignoredBits > 7)
115 throw new IllegalArgumentException();
116 if (bytes == null)
117 throw new NullPointerException();
118 if (doShift && ignoredBits > 0)
120 this.externBytes = new byte[length];
121 System.arraycopy(bytes, offset, externBytes, 0, length);
122 this.bytes = new BigInteger(externBytes).shiftLeft(ignoredBits)
123 .toByteArray();
125 else
127 this.bytes = new byte[length];
128 System.arraycopy(bytes, offset, this.bytes, 0, length);
130 this.ignoredBits = ignoredBits;
134 * Create a new bit string.
136 * @param bytes The byte array holding the bit string.
137 * @param offset The offset where the meaningful bytes begin.
138 * @param length The number of meaningful bytes.
139 * @param ignoredBits The number of bits to ignore.
140 * @throws IllegalArgumentException If <i>ignoredBits</i> is negative
141 * or greater than 7.
142 * @throws NullPointerException If <i>bytes</i> is null.
144 public BitString(byte[] bytes, int offset, int length, int ignoredBits)
146 this(bytes, offset, length, ignoredBits, false);
150 * Create a new bit string.
152 * @param bytes The byte array holding the bit string.
153 * @param ignoredBits The number of bits to ignore.
154 * @throws IllegalArgumentException If <i>ignoredBits</i> is negative
155 * or greater than 7.
156 * @throws NullPointerException If <i>bytes</i> is null.
158 public BitString(byte[] bytes, int ignoredBits)
160 this(bytes, 0, bytes.length, ignoredBits, false);
164 * Create a new bit string.
166 * @param bytes The byte array holding the bit string.
167 * @param offset The offset where the meaningful bytes begin.
168 * @param length The number of meaningful bytes.
169 * @throws NullPointerException If <i>bytes</i> is null.
171 public BitString(byte[] bytes, int offset, int length)
173 this(bytes, offset, length, 0, false);
177 * Create a new bit string.
179 * @param bytes The byte array holding the bit string.
180 * @throws NullPointerException If <i>bytes</i> is null.
182 public BitString(byte[] bytes)
184 this(bytes, 0, bytes.length, 0, false);
187 // Instance methods.
188 // ------------------------------------------------------------------------
191 * Return this bit string as a byte array, with the ignored bits
192 * trimmed off. The byte array is cloned every time this method is
193 * called to prevent modification.
195 * @return The trimmed byte array.
197 public byte[] toByteArray()
199 if (ignoredBits == 0)
200 return (byte[]) bytes.clone();
201 if (externBytes == null)
202 externBytes = new BigInteger(bytes).shiftRight(ignoredBits).toByteArray();
203 return (byte[]) externBytes.clone();
207 * Returns this bit string as a byte array, with the ignored bits
208 * present. The byte array is cloned every time this method is
209 * called to prevent modification.
211 * @return The byte array.
213 public byte[] getShiftedByteArray()
215 return (byte[]) bytes.clone();
219 * Returns the number of ignored bits.
221 * @return The number of ignored bits.
223 public int getIgnoredBits()
225 return ignoredBits;
229 * Returns the size, in bits, of this bit string.
231 * @return The size of this bit string.
233 public int size()
235 return (bytes.length << 3) - ignoredBits;
239 * Return this bit string as a boolean array. The value returned is of
240 * size {@link #size()}, and each <code>true</code> value
241 * corresponding to each "1" in this bit string. The boolean array is
242 * cloned before it is returned.
244 * @return The boolean array.
246 public boolean[] toBooleanArray()
248 if (boolVal == null)
250 boolVal = new boolean[size()];
251 for (int i = 0, j = 7, k = 0; i < boolVal.length; i++)
253 boolVal[i] = (bytes[k] & 1 << j--) != 0;
254 if (j < 0)
256 j = 7;
257 k++;
261 return (boolean[]) boolVal.clone();
264 public Object clone()
268 return super.clone();
270 catch (CloneNotSupportedException cce)
272 throw new InternalError(cce.getMessage());
276 public int compareTo(Object o)
278 BitString that = (BitString) o;
279 if (this.equals(that))
280 return 0;
281 if (this.bytes.length != that.bytes.length)
282 return (this.bytes.length < that.bytes.length) ? -1 : 1;
283 if (this.ignoredBits != that.ignoredBits)
284 return (this.ignoredBits < that.ignoredBits) ? -1 : 1;
285 for (int i = 0; i < this.bytes.length; i++)
286 if (this.bytes[i] != that.bytes[i])
287 return (this.bytes[i] < that.bytes[i]) ? -1 : 1;
288 return 0; // not reached.
291 public int hashCode()
293 int result = 0;
294 for (int i = 0; i < bytes.length - 1; ++i)
295 result = result * 31 + bytes[i];
296 if (bytes.length > 0)
298 int lastByte = bytes[bytes.length - 1] & ~ ((1 << ignoredBits) - 1);
299 result = result * 31 + lastByte;
301 return result;
304 public boolean equals(Object o)
306 if (!(o instanceof BitString))
307 return false;
308 BitString that = (BitString) o;
309 // True for cloned instances.
310 if (this.bytes == that.bytes && this.ignoredBits == that.ignoredBits)
311 return true;
312 if (this.ignoredBits == that.ignoredBits)
313 return Arrays.equals(this.bytes, that.bytes);
314 return false;
317 public String toString()
319 CPStringBuilder sb = new CPStringBuilder();
320 for (int i = 0, j = 7, k = 0; i < size(); i++)
322 sb.append((bytes[k] & 1 << j) != 0 ? "1" : "0");
323 j--;
324 if (j < 0)
326 j = 7;
327 k++;
330 return sb.toString();