Bit Stuffing

Submitted by Karthikeyan on

A simple java program for bit-stuffing. This program inserts the bits given pattern in the given file


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class BitStuff {
private static final int SIZE = 128;
private byte[] buffer;
private String pattern;
public BitStuff()
{
    pattern = "101111111111111101111111110";
}
public void stuffBytes(String infile, String outfile) throws Exception
{
    try
    {
        FileInputStream in = new FileInputStream(infile);
        FileOutputStream out = new FileOutputStream(outfile);
        buffer = new byte[SIZE];
        int noBytes = in.read(buffer);
        while(noBytes>0)
        {
            out.write(buffer);
            out.write(pattern.getBytes());
            buffer = new byte[SIZE];
            noBytes = in.read(buffer);
        }
        out.close();
        in.close();
    }
    catch(IOException ioe)
    {
        System.out.println("File io problem");
        ioe.printStackTrace();
    }
    
}
    public static void main(String[] args) throws Exception {
        Scanner console = new Scanner(System.in);
        System.out.println("Enter the filename to be bit stuffed");
        String infile = console.nextLine();
        System.out.println("Enter the output filename");
        String outfile = console.nextLine();
        BitStuff stuffer = new BitStuff();
        stuffer.stuffBytes(infile, outfile);
        
    }
}