org.limitless.util
Class BitDestination

java.lang.Object
  extended by org.limitless.util.BitDestination

public class BitDestination
extends java.lang.Object

This class provides a 'sink' for data trickled in a bit or bits at a time, instead of bytes at a time. This is especially useful for protocols that have complex headers with binary-packed fields. Once all the bits have been processed through this, it provides an array of bytes or ints as desired.

Version:
$Header: /repository/nmc/java/org/limitless/util/BitDestination.java,v 1.2 2004/07/08 18:17:39 brianj Exp $
Author:
Brian Jones

Field Summary
private  int bitIndex
          where the next bit will go in the last byte
private  int byteIndex
          depth used
private  int currentInt
          the int being built (bitIndex is how far from the left the next bit will go)
(package private) static org.apache.log4j.Logger logger
          Log4J Logger
private static long[] maskHighOrder
          The index value to the array indicates the number of bits that you want to REMAIN (it masks off the bits HIGHER than the chosen)
private  java.util.List values
          this holds each byte as it fills
 
Constructor Summary
BitDestination()
           
 
Method Summary
 void addBits(boolean value)
          Add a boolean, if it's true, a 1 is added, and if false, a 0 is added.
 void addBits(int value, int bits)
          Add 'bits' of value into the stream.
 void addBits(long value, int bits)
          Add 'bits' of value into the stream.
 void addBytes(byte[] value)
          This adds all the bytes in 'value' to the end of the buffer
 void addBytes(byte[] value, int startingByte, int bytesToFill)
          This adds a sub-set of all the bytes in value to the end of the buffer
 void addBytes(java.lang.String value, int bytesToFill)
          This adds the binary representation of the string in 'value' and pads it or chops it as needed to fit into bytesToFill bytes ensuring that the resulting block of data added is exactly bytesToFill bytes long.
private  void addRawBits(long value, int bits)
          This does the shifting and applying of a portion of value bits wide into the system.
 byte[] asBytes()
          Fetch the data as an array of bytes
 int[] asInts()
          Fetch teh data as an array of ints
 int getCurrentBitPosition()
          This is seldom used and is how many bits of the last byte have been used so far.
 int getCurrentBytePosition()
          This is seldom used and is which byte will be used to write the next bits into.
static void main(java.lang.String[] args)
          This exercises the class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

static org.apache.log4j.Logger logger
Log4J Logger


maskHighOrder

private static long[] maskHighOrder
The index value to the array indicates the number of bits that you want to REMAIN (it masks off the bits HIGHER than the chosen)


byteIndex

private int byteIndex
depth used


bitIndex

private int bitIndex
where the next bit will go in the last byte


currentInt

private int currentInt
the int being built (bitIndex is how far from the left the next bit will go)


values

private java.util.List values
this holds each byte as it fills

Constructor Detail

BitDestination

public BitDestination()
Method Detail

asBytes

public byte[] asBytes()
Fetch the data as an array of bytes


asInts

public int[] asInts()
Fetch teh data as an array of ints


addBits

public void addBits(int value,
                    int bits)
Add 'bits' of value into the stream. For instance, if value were '13' and bits were '3' then the lower 3 bits of 13 would be added (1101 -> 1(101) -> 101 )


addBits

public void addBits(long value,
                    int bits)
Add 'bits' of value into the stream. For instance, if value were '13' and bits were '3' then the lower 3 bits of 13 would be added (1101 -> 1(101) -> 101 )


addBits

public void addBits(boolean value)
Add a boolean, if it's true, a 1 is added, and if false, a 0 is added.


addBytes

public void addBytes(byte[] value)
This adds all the bytes in 'value' to the end of the buffer


addBytes

public void addBytes(byte[] value,
                     int startingByte,
                     int bytesToFill)
This adds a sub-set of all the bytes in value to the end of the buffer


addBytes

public void addBytes(java.lang.String value,
                     int bytesToFill)
This adds the binary representation of the string in 'value' and pads it or chops it as needed to fit into bytesToFill bytes ensuring that the resulting block of data added is exactly bytesToFill bytes long. The first bytesToFill bytes are used if the string is too long (thus, it discards extra from the RIGHT of the string). If the buffer has any bits unfilled in the current byte, then the string is 'broken' by bits and wrapped across them--not normally what's desired, but it would make for a bizarre ultra-light encryption.


getCurrentBytePosition

public int getCurrentBytePosition()
This is seldom used and is which byte will be used to write the next bits into.


getCurrentBitPosition

public int getCurrentBitPosition()
This is seldom used and is how many bits of the last byte have been used so far.


addRawBits

private void addRawBits(long value,
                        int bits)
This does the shifting and applying of a portion of value bits wide into the system.


main

public static void main(java.lang.String[] args)
This exercises the class



Copyright © 1995, 2004 Limitless Knowledge Association, All Rights Reserved.