net.siefkes.nlstego.util
Class BitArray

java.lang.Object
  extended by net.siefkes.nlstego.util.BitArray

public class BitArray
extends Object

This class provides read access to an array of bits (booleans).

Version:
$Revision: 1.9 $, $Date: 2005/07/12 17:02:22 $, $Author: siefkes $
Author:
Christian Siefkes

Constructor Summary
BitArray(byte[] myBytes)
          Creates a new instance.
BitArray(InputStream in)
          Creates a new instance, reading the data to wrap from an input stream.
BitArray(int byteLength)
          Creates a new instance.
 
Method Summary
 int byteLength()
          Returns the length of this array, in bytes.
 boolean get(int byteIndex, int bitIndex)
          Returns the value of a bit.
 byte getByte(int byteIndex)
          Returns the value of a byte.
 byte[] getBytes()
          Returns the internal byte array wrapped by this instance.
 boolean hasNext()
          Checks whether there are more bits to read or set from this array.
 long length()
          Returns the length of this array, in bits.
 boolean next()
          Returns the next unread or unset bit from this array.
 void set(int byteIndex, int bitIndex, boolean value)
          Modifies the value of a bit.
 void setNext(boolean value)
          Returns the next unread or unset bit from this array.
 String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitArray

public BitArray(int byteLength)
Creates a new instance. Creates an internal byte array of the specified length, initially setting all bytes (and thus all bits) to 0.

Parameters:
byteLength - the length of the internal array of bytes (in bytes)

BitArray

public BitArray(byte[] myBytes)
Creates a new instance.

Parameters:
myBytes - the array of bytes to wrap; any changes to this array will immediately affect the wrapping bit array

BitArray

public BitArray(InputStream in)
         throws IOException
Creates a new instance, reading the data to wrap from an input stream.

Parameters:
in - the contents of this input stream will be wrapped
Throws:
IOException - if an I/O error occurs
Method Detail

byteLength

public int byteLength()
Returns the length of this array, in bytes.

Returns:
the length of this array, in bytes

get

public boolean get(int byteIndex,
                   int bitIndex)
            throws ArrayIndexOutOfBoundsException
Returns the value of a bit.

Parameters:
byteIndex - the index of the byte to read from, must by in the range from 0 to byteLength() - 1}
bitIndex - the index of the bit within the byte, must in in the range from 0 (highest-order bit) to 7 (lowest-order bit)
Returns:
the value of the specified bit: true for 1, false for 0
Throws:
ArrayIndexOutOfBoundsException - if one of the indexes is outside the allowed range

getByte

public byte getByte(int byteIndex)
             throws ArrayIndexOutOfBoundsException
Returns the value of a byte.

Parameters:
byteIndex - the index of the byte to get, must by in the range from 0 to byteLength() - 1}
Returns:
the value of the specified byte
Throws:
ArrayIndexOutOfBoundsException - if the index is outside the allowed range

getBytes

public byte[] getBytes()
Returns the internal byte array wrapped by this instance. Note that this method returns the actually used array (not a copy), so all changes to the returned array will immediately influence the outer instance and vice versa.

Returns:
the wrapped array

length

public long length()
Returns the length of this array, in bits.

Returns:
the length of this array, in bits

hasNext

public boolean hasNext()
Checks whether there are more bits to read or set from this array. Iff this method returns false, the next call to next() or setNext(boolean) will throw an exception.

Returns:
true iff there are more bits to read or set

next

public boolean next()
             throws ArrayIndexOutOfBoundsException
Returns the next unread or unset bit from this array.

Returns:
the value of the next unread bit: true for 1, false for 0
Throws:
ArrayIndexOutOfBoundsException - if all bits from this array have already been read or set

set

public void set(int byteIndex,
                int bitIndex,
                boolean value)
         throws ArrayIndexOutOfBoundsException
Modifies the value of a bit.

Parameters:
byteIndex - the index of the byte to modify, must by in the range from 0 to byteLength() - 1}
bitIndex - the index of the bit within the byte, must in in the range from 0 (highest-order bit) to 7 (lowest-order bit)
value - the new value of the specified bit: true for 1, false for 0
Throws:
ArrayIndexOutOfBoundsException - if one of the indexes is outside the allowed range

setNext

public void setNext(boolean value)
             throws ArrayIndexOutOfBoundsException
Returns the next unread or unset bit from this array.

Parameters:
value - the new value of the bit: true for 1, false for 0
Throws:
ArrayIndexOutOfBoundsException - if all bits from this array have already been read or set

toString

public String toString()
Returns a string representation of this object.

Overrides:
toString in class Object
Returns:
a textual representation


Copyright © 2003-2005 Christian Siefkes. All Rights Reserved.