Fanny
Fanny

Reputation: 151

why is my program not reading/writing the bits?

Im currently creating a Huffman compression program. But Im having some trouble with writing/reading the bits. I want to be able to write specific bits to a file. e.g first "0100" then "0101" should be written as a byte to a new file using fileOutputStream as "01000101" :69

Class BitFileWriter - writes bits to file by saving each byte in buffer and then writing when buffer is full (contains 8 bits).

In the main function of this class I have some tests to se if all bytes will be written to file. but opening the text file it doesn't read "AB".

    /**
 * writes bits to file outPutStream.
 */
public class BitFileWriter {

    private BufferedOutputStream out;
    private int buffer; // 8-bit buffer of bits to write out
    private int n; // number of bits remaining in buffer
    private String filename;


    public BitFileWriter(String filename){

        this.filename = filename;

    }

    private void addBitToBuffer(boolean bit) throws IOException {

            // add bit to buffer
            this.buffer <<= 1;
            if (bit) this.buffer |= 1;
            n++;

            //if buffer is full write a whole byte.
            if(n == 8){

                writeByte(this.buffer);
                this.n = 0;
                this.buffer = 0;


            }

    }

    private void writeByte(int b) throws IOException {


            this.out = new BufferedOutputStream(new 
            FileOutputStream(filename));
            out.write(b);
    }

    public void flush() throws IOException {
        this.out.flush();
    }

    public static void main(String[] args) throws IOException {
        BitFileWriter bitFileWriter = new 
        BitFileWriter("./src/result.txt");


        // byte: 01000001, A
        bitFileWriter.addBitToBuffer(true);
        bitFileWriter.addBitToBuffer(true);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(true);
        bitFileWriter.addBitToBuffer(false);

        //byte 01000011, B
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(true);
        bitFileWriter.addBitToBuffer(true);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);
        bitFileWriter.addBitToBuffer(false);


        bitFileWriter.flush();



    }

}

Class BitFileReader - reads bits from file.

but reading all 16 bits that I wanted to write to result.txt doesn´t give me the bits I (think) have written.

    /**
 * Reads one bit at a time from a file.
 *
 *
 */
public class BitFileReader {
    private BufferedInputStream in;
    private int currentByte; // -1 if no more data
    private int bitPos;      // position in currentByte

    /**
     * Creates a BitFileReader by opening a connection to an actual file,
     * the file named by the File object file in the file system.
     */
    public BitFileReader(File file) throws IOException {

        in = new BufferedInputStream(new FileInputStream(file));
        currentByte = in.read();
        bitPos = 7;
    }

    /** Returns true if this reader has another bit in its input. */
    public boolean hasNextBit() {
        return currentByte != -1 && in != null;
    }

    /** Reads a single bit. */
    public int nextBit() throws IOException {
        int res = (currentByte>>bitPos) & 1;
        --bitPos;
        if (bitPos < 0) {
            currentByte = in.read(); // -1 if end of file has been reached (read returns -1 if end of file).
            bitPos = 7;
        }
        return res ;
    }

    /** Closes this reader. */
    public void close() throws IOException {
        if (in != null) {
            in.close();
        }
    }

    //Test
    public static void main(String[] args) throws IOException {
        File temp;
        BitFileReader reader;



        reader = new BitFileReader(new File("./src/result.txt"));

        System.out.print("first byte: ");
        for(int i = 0; i <8; i++){
            System.out.print(reader.nextBit());
        }

        System.out.print(". second byte: ");
        for(int i = 0; i <8; i++){
            System.out.print(reader.nextBit());
        }




        reader.close();
    }
}

Output is: first byte: 01100000. second byte: 11111111

Upvotes: 0

Views: 100

Answers (1)

Maurice Perry
Maurice Perry

Reputation: 9648

The first thing I would do, is to move the statement:

this.out = new BufferedOutputStream(new 
            FileOutputStream(filename));

from writeByte to the constructor

Upvotes: 1

Related Questions