You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by we...@apache.org on 2004/01/29 00:12:48 UTC
svn commit: rev 6344 - incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn
Author: wesmckean
Date: Wed Jan 28 15:12:47 2004
New Revision: 6344
Modified:
incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java
Log:
Changed to use BitSet
Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java (original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java Wed Jan 28 15:12:47 2004
@@ -50,6 +50,7 @@
package org.apache.snickers.asn;
+import java.util.BitSet;
import org.apache.commons.codec.binary.Binary;
/**
@@ -57,13 +58,7 @@
*
* Encapsulates the basic functionality of a bit string
*/
-public class BitString {
-
- /** The bits that make up this bit string */
- int[] bits = null;
-
- /** The number of bits */
- int numbits;
+public class BitString extends BitSet {
private static final int[] bitflags = {
(1 << 0 ),
@@ -104,14 +99,33 @@
* The basic constructor for a BitString
*/
public BitString( int[] bits, int numbits ) {
- super();
+ super(numbits);
+
+ int index = 0;
- this.bits = new int[ bits.length ];
- System.arraycopy( bits, 0, this.bits, 0, bits.length );
- this.numbits = numbits;
+ for( int i = 0; i < bits.length && index < numbits; i++ ) {
+ for( int j = 0; j < 32 && index < numbits; j++ ) {
+ this.set(index++, ((bits[i] & bitflags[j]) != 0) );
+ }
+ }
}
/**
+ * Constuct a BitString from an existing BitSet. Since
+ * BitString inherits BitSet, this will also work for
+ * BitStrings as well.
+ *
+ * @param bitSet the bit set containing our data
+ */
+ public BitString( BitSet bitSet ) {
+ super( bitSet.length() );
+
+ for( int i = 0; i < bitSet.length(); i++ ) {
+ this.set(i, bitSet.get(i));
+ }
+ }
+
+ /**
* Performs a binary and on the passed in BitString
*
* @param value the BitString to and against this one
@@ -120,20 +134,9 @@
* the largest BitString.
*/
public BitString and( BitString value ) {
- BitString result = null;
-
- int min = ((bits.length < value.bits.length) ? bits.length : value.bits.length);
- int length = ((bits.length > value.bits.length) ? bits.length : value.bits.length);
- int[] l_bits = new int[ length ];
- java.util.Arrays.fill( l_bits, 0 );
-
- for( int i = 0; i < min; i++ ) {
- l_bits[i] = (bits[i] & value.bits[i]);
- }
-
- int l_numbits = getLength() > value.getLength() ? getLength() : value.getLength();
- result = new BitString( l_bits, l_numbits );
- return result;
+ BitSet bt = (BitSet)this.clone();
+ bt.and( value );
+ return new BitString( bt );
}
/**
@@ -145,20 +148,9 @@
* the largest BitString.
*/
public BitString or( BitString value ) {
- BitString result = null;
-
- int min = ((bits.length < value.bits.length) ? bits.length : value.bits.length);
- int length = ((bits.length > value.bits.length) ? bits.length : value.bits.length);
- int[] l_bits = new int[ length ];
- java.util.Arrays.fill( l_bits, 0 );
-
- for( int i = 0; i < min; i++ ) {
- l_bits[i] = (bits[i] | value.bits[i]);
- }
-
- int l_numbits = getLength() > value.getLength() ? getLength() : value.getLength();
- result = new BitString( l_bits, l_numbits );
- return result;
+ BitSet bt = (BitSet)this.clone();
+ bt.or( value );
+ return new BitString( bt );
}
/**
@@ -170,77 +162,11 @@
* the largest BitString.
*/
public BitString xor( BitString value ) {
- BitString result = null;
-
- int min = ((bits.length < value.bits.length) ? bits.length : value.bits.length);
- int length = ((bits.length > value.bits.length) ? bits.length : value.bits.length);
- int[] l_bits = new int[ length ];
- java.util.Arrays.fill( l_bits, 0 );
-
- for( int i = 0; i < min; i++ ) {
- l_bits[i] = (bits[i] ^ value.bits[i]);
- }
-
- int l_numbits = getLength() > value.getLength() ? getLength() : value.getLength();
- result = new BitString( l_bits, l_numbits );
- return result;
- }
-
- /**
- * Return the number of bits in this bit string
- *
- * @return an integer count of the number of bits
- */
- public int getLength() {
- return numbits;
- }
-
- /**
- * Method used to test the value of a specific bit
- * @param i the index of the bit to test
- * @return true if the bit is on, else false
- */
- public boolean isSet( int i ) {
- boolean result = false;
-
- // Calculate the byte offset
- if( i >= numbits ) {
- throw new IndexOutOfBoundsException();
- }
-
- // Calculate the offset to get the actual byte
- int index = i / 32;
- int bit = i % 32;
-
- int aByte = (int)bits[index];
- result = ((aByte & bitflags[bit]) != 0 );
-
- return result;
+ BitSet bt = (BitSet)this.clone();
+ bt.xor( value );
+ return new BitString( bt );
}
- /* (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(Object obj) {
- boolean result = false;
-
- if( obj instanceof BitString ) {
- if( obj == this ) {
- result = true;
- }
- else {
- BitString bObj = (BitString)obj;
-
- if( bObj.getLength() == getLength() ) {
- result = true;
- for( int i = 0; result && i < getLength(); i++ ) {
- result = (isSet( i ) == bObj.isSet(i));
- }
- }
- }
- }
- return result;
- }
/* (non-Javadoc)
* @see java.lang.Object#toString()
@@ -248,8 +174,8 @@
public String toString() {
StringBuffer sb = new StringBuffer();
- for( int i = numbits-1; i >= 0; i-- ) {
- if( isSet(i) ) {
+ for( int i = length()-1; i >= 0; i-- ) {
+ if( super.get(i) ) {
sb.append("1");
}
else {