Author: elecharny
Date: Tue May 3 16:24:34 2005
New Revision: 168018
URL: http://svn.apache.org/viewcvs?rev=168018&view=rev
Log:
Corrected the BitString(byte[]) constructor, adding a private method shared
with the setBits method.
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java
URL:
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java?rev=168018&r1=168017&r2=168018&view=diff
==============================================================================
---
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java
(original)
+++
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java
Tue May 3 16:24:34 2005
@@ -137,11 +137,8 @@
*/
public BitString( byte[] bytes )
{
- nbInts = ( bytes.length / 4 ) + ( ( ( bytes.length % 4 ) != 0 ) ? 1 :
0 );
-
- // The first byte contains the number of unused ints
- nbUnusedBits = bytes[0] & 0x07;
- nbBits = ( bytes.length * 8 ) - nbUnusedBits;
+ int nbBytes = bytes.length - 1;
+ nbInts = ( nbBytes / 4 ) + ( ( ( nbBytes % 4 ) != 0 ) ? 1 : 0 );
if ( nbInts > DEFAULT_LENGTH )
{
@@ -158,33 +155,66 @@
ints = new int[nbInts];
}
- // We have to transfer the data from bytes to ints
- int pos = 1;
- int bytesPos = 1;
-
- for ( int i = 0; i < nbInts; i++ )
- {
-
- for ( int j = 0; j < 4; j++ )
- {
-
- if ( bytesPos == ( nbInts - 1 ) )
- {
- ints[i] = ( ints[i] << ( 8 - nbUnusedBits ) ) +
- ( bytes[bytesPos] >> nbUnusedBits );
- break;
- }
- else
- {
- ints[i] = ( ints[i] << 8 ) + bytes[bytesPos++];
- }
- }
- }
+ setBytes(bytes, nbBytes);
}
//~ Methods
------------------------------------------------------------------------------------
/**
+ * Set the bytes into the ints.
+ */
+ private void setBytes(byte[] bytes, int nbBytes)
+ {
+ // The first byte contains the number of unused ints
+ nbUnusedBits = bytes[0] & 0x07;
+ nbBits = ( nbBytes * 8 ) - nbUnusedBits;
+
+ // We have to transfer the data from bytes to ints
+ int pos = 1;
+ int bytesPos = 1;
+
+ for ( int i = 0; i < ( nbInts - 1 ); i++ )
+ {
+ ints[i] =
+ ( ( bytes[( i * 4 ) + 1] & 0x00FF ) << 24 ) +
+ ( ( bytes[( i * 4 ) + 2] & 0x00FF ) << 16 ) +
+ ( ( bytes[( i * 4 ) + 3] & 0x00FF ) << 8 ) +
+ ( bytes[( i * 4 ) + 4] & 0x00FF );
+ }
+
+ switch ( nbBytes % 4 )
+ {
+
+ case 0 :
+ ints[nbInts - 1] = ( bytes[( ( nbInts - 1 ) * 4 ) + pos] &
0x00FF );
+ pos++;
+ // fallthrough
+
+ case 3 :
+ ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
+ ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
+ pos++;
+ // fallthrough
+
+ case 2 :
+ ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
+ ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
+ pos++;
+ // fallthrough
+
+ case 1 :
+ ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
+ ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
+ pos++;
+ // fallthrough
+
+ default :
+ // Nothing to do...
+ }
+
+ ints[nbInts - 1] >>= nbUnusedBits;
+ }
+ /**
* Set a new BitString in the BitString. It will replace the old BitString,
* and reset the current length with the new one.
*
@@ -203,10 +233,6 @@
int nbInts = ( nbBytes / 4 ) + ( ( ( nbBytes % 4 ) != 0 ) ? 1 : 0 );
- // The first byte contains the number of unused ints
- nbUnusedBits = bytes[0] & 0x07;
- nbBits = ( nbBytes * 8 ) - nbUnusedBits;
-
if ( ( nbInts > DEFAULT_LENGTH ) && ( ints.length < nbInts ) )
{
@@ -216,50 +242,7 @@
ints = new int[nbInts];
}
- // We have to transfer the data from bytes to ints
- int pos = 1;
- int bytesPos = 1;
-
- for ( int i = 0; i < ( nbInts - 1 ); i++ )
- {
- ints[i] =
- ( ( bytes[( i * 4 ) + 1] & 0x00FF ) << 24 ) +
- ( ( bytes[( i * 4 ) + 2] & 0x00FF ) << 16 ) +
- ( ( bytes[( i * 4 ) + 3] & 0x00FF ) << 8 ) +
- ( bytes[( i * 4 ) + 4] & 0x00FF );
- }
-
- switch ( nbBytes % 4 )
- {
-
- case 0 :
- ints[nbInts - 1] = ( bytes[( ( nbInts - 1 ) * 4 ) + pos] &
0x00FF );
- pos++;
- // fallthrough
-
- case 3 :
- ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
- ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
- pos++;
- // fallthrough
-
- case 2 :
- ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
- ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
- pos++;
- // fallthrough
-
- case 1 :
- ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
- ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
- pos++;
- // fallthrough
-
- default :
- // Nothing to do...
- }
-
- ints[nbInts - 1] >>= nbUnusedBits;
+ setBytes(bytes, nbBytes);
}
/**