OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / security / der / BitString.java
1 /* BitString.java -- Java representation of the BIT STRING type.
2    Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package gnu.java.security.der;
40
41 import gnu.java.lang.CPStringBuilder;
42
43 import java.math.BigInteger;
44 import java.util.Arrays;
45
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:
50  *
51  * <pre>   00010101 11101101 11010xxx</pre>
52  *
53  * <p>Where the "xxx" represents three bits that should be ignored, and
54  * can have any value.
55  *
56  * @author Casey Marshall (csm@gnu.org)
57  */
58 public class BitString implements Cloneable, Comparable
59 {
60
61   // Fields.
62   // ------------------------------------------------------------------------
63
64   /** The bits themselves. */
65   private final byte[] bytes;
66
67   /**
68    * The exportable byte array. This array has the ignored bits
69    * removed.
70    */
71   private transient byte[] externBytes;
72
73   /** The number of bits ignored at the end of the byte array. */
74   private final int ignoredBits;
75
76   /** This bit string as a boolean array. */
77   private transient boolean[] boolVal;
78
79   // Constructors.
80   // ------------------------------------------------------------------------
81
82   /**
83    * Create a new bit string, shifting the given byte array if needed.
84    *
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.
92    */
93   public BitString(byte[] bytes, int ignoredBits, boolean doShift)
94   {
95     this(bytes, 0, bytes.length, ignoredBits, doShift);
96   }
97
98   /**
99    * Create a new bit string, shifting the given byte array if needed.
100    *
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.
110    */
111   public BitString(byte[] bytes, int offset, int length,
112                    int ignoredBits, boolean doShift)
113   {
114     if (ignoredBits < 0 || ignoredBits > 7)
115       throw new IllegalArgumentException();
116     if (bytes == null)
117       throw new NullPointerException();
118     if (doShift && ignoredBits > 0)
119       {
120         this.externBytes = new byte[length];
121         System.arraycopy(bytes, offset, externBytes, 0, length);
122         this.bytes = new BigInteger(externBytes).shiftLeft(ignoredBits)
123                        .toByteArray();
124       }
125     else
126       {
127         this.bytes = new byte[length];
128         System.arraycopy(bytes, offset, this.bytes, 0, length);
129       }
130     this.ignoredBits = ignoredBits;
131   }
132
133   /**
134    * Create a new bit string.
135    *
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.
143    */
144   public BitString(byte[] bytes, int offset, int length, int ignoredBits)
145   {
146     this(bytes, offset, length, ignoredBits, false);
147   }
148
149   /**
150    * Create a new bit string.
151    *
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.
157    */
158   public BitString(byte[] bytes, int ignoredBits)
159   {
160     this(bytes, 0, bytes.length, ignoredBits, false);
161   }
162
163   /**
164    * Create a new bit string.
165    *
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.
170    */
171   public BitString(byte[] bytes, int offset, int length)
172   {
173     this(bytes, offset, length, 0, false);
174   }
175
176   /**
177    * Create a new bit string.
178    *
179    * @param bytes The byte array holding the bit string.
180    * @throws NullPointerException If <i>bytes</i> is null.
181    */
182   public BitString(byte[] bytes)
183   {
184     this(bytes, 0, bytes.length, 0, false);
185   }
186
187   // Instance methods.
188   // ------------------------------------------------------------------------
189
190   /**
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.
194    *
195    * @return The trimmed byte array.
196    */
197   public byte[] toByteArray()
198   {
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();
204   }
205
206   /**
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.
210    *
211    * @return The byte array.
212    */
213   public byte[] getShiftedByteArray()
214   {
215     return (byte[]) bytes.clone();
216   }
217
218   /**
219    * Returns the number of ignored bits.
220    *
221    * @return The number of ignored bits.
222    */
223   public int getIgnoredBits()
224   {
225     return ignoredBits;
226   }
227
228   /**
229    * Returns the size, in bits, of this bit string.
230    *
231    * @return The size of this bit string.
232    */
233   public int size()
234   {
235     return (bytes.length << 3) - ignoredBits;
236   }
237
238   /**
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.
243    *
244    * @return The boolean array.
245    */
246   public boolean[] toBooleanArray()
247   {
248     if (boolVal == null)
249       {
250         boolVal = new boolean[size()];
251         for (int i = 0, j = 7, k = 0; i < boolVal.length; i++)
252           {
253             boolVal[i] = (bytes[k] & 1 << j--) != 0;
254             if (j < 0)
255               {
256                 j = 7;
257                 k++;
258               }
259           }
260       }
261     return (boolean[]) boolVal.clone();
262   }
263
264   public Object clone()
265   {
266     try
267       {
268         return super.clone();
269       }
270     catch (CloneNotSupportedException cce)
271       {
272         throw new InternalError(cce.getMessage());
273       }
274   }
275
276   public int compareTo(Object o)
277   {
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.
289   }
290
291   public int hashCode()
292   {
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)
297       {
298         int lastByte = bytes[bytes.length - 1] & ~ ((1 << ignoredBits) - 1);
299         result = result * 31 + lastByte;
300       }
301     return result;
302   }
303
304   public boolean equals(Object o)
305   {
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;
315   }
316
317   public String toString()
318   {
319     CPStringBuilder sb = new CPStringBuilder();
320     for (int i = 0, j = 7, k = 0; i < size(); i++)
321       {
322         sb.append((bytes[k] & 1 << j) != 0 ? "1" : "0");
323         j--;
324         if (j < 0)
325           {
326             j = 7;
327             k++;
328           }
329       }
330     return sb.toString();
331   }
332 }