BitInputStream.java failas:
import java.io.*;
public class BitInputStream extends ByteArrayInputStream {
private int bitsRead;
private int bitPosition;
private int currentByte;
private int myMark;
private final static int NUM_BITS_IN_BYTE = 8;
private final static int END_POSITION = -1;
private boolean readingStarted;
/**
* Create a BitInputStream for a File on disk.
*/public BitInputStream( byte[] buf ) throws IOException {
super( buf );
myMark = 0;
bitsRead = 0;
bitPosition = NUM_BITS_IN_BYTE-1;
currentByte = 0;
readingStarted = false;
}
/**
* Read a binary „1“ or „0“ from the File.
*/public int readBit() throws IOException {
int theBit = -1;
if( bitPosition == END_POSITION || !readingStarted ) {
currentByte = super.read();
bitPosition = NUM_BITS_IN_BYTE-1;
readingStarted = true;
}
theBit = (0x01 << bitPosition) & currentByte;
bitPosition–;
if( theBit > 0 ) {
theBit = 1;
}
return( theBit );
}
/**
* Return the next byte in the File as lowest 8 bits of int.
*/public int read() {
currentByte = super.read();
bitPosition = END_POSITION;
readingStarted = true;
return( currentByte );
}
/**
*
*/public void mark( int readAheadLimit ) {
super.mark(readAheadLimit);
myMark = bitPosition;
}
/**
* Add needed functionality to super’s reset() method. Reset to
* the last valid position marked in the input stream.
*/public void reset() {
super.pos = super.mark-1;
currentByte = super.read();
bitPosition = myMark;
}
/**
* Returns the number of bits still available to be read.
*/public int availableBits() throws IOException {
return( ((super.available() * 8) + (bitPosition + 1)) );
}
}
BitOutputStream.java failas:
import java.io.*;
import java.lang.Exception;
public class BitOutputStream extends ByteArrayOutputStream {
private FileOutputStream myFileOS;
private int bitsWritten;
private int bitPosition;
private byte[] myByte;
private final static int NUM_BITS_IN_BYTE = 8;
private final static int END_POSITION = -1;
/**
* Default constructor.
*/public BitOutputStream() {
this(null);
}
/**
* Constuct based upon a FileOutputSteam to write to
*/public BitOutputStream( FileOutputStream fos ) {
super();
myFileOS = fos;
bitsWritten = 0;
myByte = new byte[1];
resetByte();
}
/**
* Reset the byte and position marker.
*/private void resetByte() {
bitPosition = NUM_BITS_IN_BYTE-1;
myByte[0] = (byte)0x00;
}
/**
* Write to my super’s buffer.
*/private void flushByte() {
write( myByte, 0, 1 );
bitsWritten = bitsWritten + 8;
resetByte();
}
/**
* Write a bit to the stream.
* If bit == „0“, writes a ‘0’ bit.
* If bit == „1“, writes a ‘1’ bit.
*/public void writeBit( int bit ) {
if( bit != 0 && bit != 1 ) {
System.err.println( „: only 1’s and 0’s can be written“ );
}
if( bitPosition == END_POSITION ) {
flushByte();
}
if( bit == 0 ) {
bitPosition–;
}
else if( bit == 1 ) {
myByte[0] = (byte)(myByte[0] | (0x01 << bitPosition));
bitPosition–;
}
}
/**
* Writes to the file if a FileOutputStream is set.
*/public void flushToFile() {