GiciStream
Class BitStream

java.lang.Object
  extended by GiciStream.BitStream

public class BitStream
extends java.lang.Object

This class is a bit buffer, useful to store a bit stream.
ATTENTION: this class should be optimized like ByteStream. Now it has a very poor performance!!!

Version:
1.2
Author:
Group on Interactive Coding of Images (GICI)

Field Summary
(package private)  long actualPosition
          Actual position in the bitstream (useful to scan bytestream from begin to end)..
(package private)  byte[] bitBuffer
          Byte array used to store bits.
(package private)  int buffAllocation
          Number of bytes used in each step of buffer allocation (when the buffer is full, another one es reserved with buffAllocation bytes more and the contents are copied).
(package private)  long numBits
          Number of bits saved in bitBuffer.
 
Constructor Summary
BitStream()
          Class constructor (performs needed initializations).
BitStream(byte[] bitBuffer)
          Class constructor that initilizaes the BitStream with a bitBuffer.
BitStream(int initialSizeBuffer)
          Class constructor with a specified amount of bits to allocate initially in the buffer.
 
Method Summary
 void addBit(boolean bit)
          Inserts a bit in the bitstream.
 void addBit(int bit)
          Inserts a bit in the bitstream.
 void addBits(int bits, int length)
          Inserts the bits of an int to the bitstream starting at length MSB position of the int passed.
 void addBitStream(BitStream bs)
          Inserts a BitStream in the current object.
 void addByte(int byt)
          Inserts the bits of a byte into the bitstream.
 void deleteBeginBit()
          Deletes a bit of the bitstream (starting at the begining of bitstream).
 void deleteBeginBits(long num)
          Deletes a number of bits of the bitstream (starting at the begining of bitstream).
 void deleteEndBit()
          Deletes a bit of the bitstream (starting at the end of bitstream).
 void deleteEndBits(long num)
          Deletes a number of bits of the bitstream (starting at the end of bitstream).
 long getActualPosition()
          Return the position of the bit which is pointed by the bitstream
 boolean getBit()
          Return the actual bit and increments actual bit plus 1.
 boolean getBit(long numBit)
          Return the bit value indicated.
 int getBits(int length)
          Return length bits like an integer.
 byte[] getBitStream()
          Return the bit stream in an array of bytes (the length of array byte returned is equal or greater than the real bytes used in bitStream -it can be known using getNumBits function-).
 byte getByte(int numByte)
          Return the byte indicated.
 long getNumBits()
          Return the number of bits in the bit stream.
 long getNumBytes()
          Return the number of bytes in the bit stream.
 void reset()
          Reset the bit stream
 void rewindBits(long numBits)
          Rewind the index in the bit stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bitBuffer

byte[] bitBuffer
Byte array used to store bits.

All values allowed.


numBits

long numBits
Number of bits saved in bitBuffer.

Negative values are not allowed for this field.


actualPosition

long actualPosition
Actual position in the bitstream (useful to scan bytestream from begin to end)..

Only positive values allowed.


buffAllocation

int buffAllocation
Number of bytes used in each step of buffer allocation (when the buffer is full, another one es reserved with buffAllocation bytes more and the contents are copied).

Negative values are not allowed for this field.

Constructor Detail

BitStream

public BitStream()
Class constructor (performs needed initializations).


BitStream

public BitStream(int initialSizeBuffer)
Class constructor with a specified amount of bits to allocate initially in the buffer.

Parameters:
initialSizeBuffer - number of bits that initially the buffer will contain

BitStream

public BitStream(byte[] bitBuffer)
Class constructor that initilizaes the BitStream with a bitBuffer.

Parameters:
bitBuffer - a byte array from which the BitStream initializes his bitBuffer
Method Detail

addBit

public void addBit(boolean bit)
Inserts a bit in the bitstream.

Parameters:
bit - a boolean that indicates the bit (1=true, 0=false) to insert

addBit

public void addBit(int bit)
Inserts a bit in the bitstream.

Parameters:
bit - a integer that indicates the bit (1=true, 0=false) to insert

addByte

public void addByte(int byt)
Inserts the bits of a byte into the bitstream.

Parameters:
byt - the byte to be inserted

addBits

public void addBits(int bits,
                    int length)
Inserts the bits of an int to the bitstream starting at length MSB position of the int passed.

Parameters:
bits - a integer that contains the sequence of values to be inserted
length - a integer that indicates the number of bits to take into account

addBitStream

public void addBitStream(BitStream bs)
Inserts a BitStream in the current object.

Parameters:
bs - BitStream object to append.

deleteEndBit

public void deleteEndBit()
Deletes a bit of the bitstream (starting at the end of bitstream).


deleteEndBits

public void deleteEndBits(long num)
Deletes a number of bits of the bitstream (starting at the end of bitstream). If number of bits to delete is greater than bitstream, bitstream length will be 0.

Parameters:
num - the number of bits to delete

deleteBeginBit

public void deleteBeginBit()
                    throws WarningException
Deletes a bit of the bitstream (starting at the begining of bitstream).

Throws:
WarningException - because it uses addBit functions

deleteBeginBits

public void deleteBeginBits(long num)
                     throws WarningException
Deletes a number of bits of the bitstream (starting at the begining of bitstream). If number of bits to delete is greater than bitstream, bitstream length will be 0.

Parameters:
num - the number of bits to delete
Throws:
WarningException - because it uses addBit functions

getBit

public boolean getBit(long numBit)
               throws WarningException
Return the bit value indicated.

Parameters:
numBit - the number of bit in bitstream (starting at 0)
Returns:
a boolean that represents the bit value indicated (true - 1, false - 0)
Throws:
WarningException - when numBit is greater than the buffer length

getBit

public boolean getBit()
               throws WarningException
Return the actual bit and increments actual bit plus 1.

Returns:
a boolean that represents the bit value indicated (true - 1, false - 0)
Throws:
WarningException - when max buffer length is reached

getBits

public int getBits(int length)
            throws WarningException
Return length bits like an integer.

Parameters:
length - indicated the number of bits that must be given
Returns:
an integer that represents the bits value
Throws:
WarningException - when max buffer length is reached

getBitStream

public byte[] getBitStream()
Return the bit stream in an array of bytes (the length of array byte returned is equal or greater than the real bytes used in bitStream -it can be known using getNumBits function-).

Returns:
a byte array that contains the bit stream

getNumBits

public long getNumBits()
Return the number of bits in the bit stream.

Returns:
a long indicating number of bits in the bit stream

getByte

public byte getByte(int numByte)
             throws WarningException
Return the byte indicated.

Parameters:
numByte - the number of byte in bytestream (starting at 0)
Returns:
the byte asked for
Throws:
WarningException - when numByte is greater than the buffer length

getNumBytes

public long getNumBytes()
Return the number of bytes in the bit stream.

Returns:
a long indicating number of bytes in the bit stream

getActualPosition

public long getActualPosition()
Return the position of the bit which is pointed by the bitstream

Returns:
a long indicating number of the bit which is pointed by the bitstream

reset

public void reset()
Reset the bit stream


rewindBits

public void rewindBits(long numBits)
                throws WarningException
Rewind the index in the bit stream

Parameters:
numBits - indicates the number of bits to be rewinded
Throws:
WarningException - when the number of bits to be rewinded is greater than the available.


TER - Copyright (C) 2006
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Group on Interactive Coding of Images (GICI) - UAB (Spain)