Write a program that has two methodsfunctions namely a Metho

Write a program that has two methods/functions, namely,

a) Method/function that generates a bit stream from a text file with bit stuffing. Recall that for

bit stuffing, an extra 0 bit is added after every five consecutive ones. Your method should

read a text file consisting of alphanumeric characters, white spaces and punctuations,

convert the data into binary values using ASCII codes (http://www.asciitable.com/), and

then generate the binary sequence after bit stuffing.

b) Another method/function that takes a bit stream with stuffed bits, removes the stuffed bits,

converts the bits into ASCII codes and generates text file.

You may use Java, C, C++ or Python as your programming language.

please explain how to test the program using a random gibberish text file?

Solution

Please let me know if you need this information in other way:-

-----------------------------------------------------------------------------------

replaceFileContent.java

----------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;

class replaceFileContent {
   public static String convertToBin(String s) {
       byte[] bytes = s.getBytes();
       StringBuilder binary = new StringBuilder();
       for (byte b : bytes) {
           int val = b;
           for (int i = 0; i < 8; i++) {
               binary.append((val & 128) == 0 ? 0 : 1);
               val <<= 1;
           }
           // binary.append(\'\');
       }
       System.out.println(\"\'\" + s + \"\' to binary: \" + binary.toString());
       return binary.toString().replaceAll(\"11111\", \"111110\");

   }

   public static String convertToAscii(String text) {
       int index = 0;
       StringBuffer str = new StringBuffer();
       while (index < text.length()) {
           String string = text.substring(index,
                   Math.min(index + 8, text.length()));
           str.append((char) Integer.parseInt(string, 2));
           index += 8;
       }
       System.out.println(str.toString());
       return str.toString();
   }

   @SuppressWarnings({ \"rawtypes\", \"unchecked\" })
   public static void main(String args[]) {
       String inFile = \"Ascii.data\";// give your in file
       String outFile = \"Binary.data\";// give your out file
       try {

           ArrayList storeWordList = new ArrayList();
           FileInputStream fstream = new FileInputStream(inFile);
           // Get the object of DataInputStream
           DataInputStream in = new DataInputStream(fstream);
           BufferedReader br = new BufferedReader(new InputStreamReader(in));
           String strLine;
           // Read File Line By Line
           while ((strLine = br.readLine()) != null) {// lines
               storeWordList.add(convertToBin(strLine));
           }
           // Close the input stream
           in.close();

           // ///////////writing binary data to file

           Writer output = null;
           File file = new File(outFile);
           output = new BufferedWriter(new FileWriter(file));
           for (Iterator iter = storeWordList.iterator(); iter.hasNext();) {
               output.write((String) iter.next() + \"\ \ \");
           }
           output.close();
           System.out.println(\"Your file has been written to binary data\");

           // ///////////converting binary to ascii reading from file
           inFile = outFile;
           storeWordList = new ArrayList();
           fstream = new FileInputStream(inFile);
           // Get the object of DataInputStream
           in = new DataInputStream(fstream);
           br = new BufferedReader(new InputStreamReader(in));
           strLine = \"\";
           // Read File Line By Line
           while ((strLine = br.readLine()) != null) {// lines
               strLine.replaceAll(\"111110\", \"11111\");
               storeWordList.add(convertToAscii(strLine));
           }
           // Close the input stream
           in.close();

           // ///////////writing data to file from Binary to Ascii
           outFile = \"fromBinToAscii.data\";
           output = null;
           file = new File(outFile);
           output = new BufferedWriter(new FileWriter(file));
           for (Iterator iter = storeWordList.iterator(); iter.hasNext();) {
               output.write((String) iter.next() + \"\ \ \");
           }
           output.close();
           System.out.println(\"Your file has been written back to ascii data\");
           // ///////////writing data to file from Binary to Ascii
       } catch (Exception e) {// Catch exception if any
           System.err.println(\"Error: \" + e.getMessage());
       }
   }
}

--------------------------------------------------------------

SAMPLE INPUT FIles:-

=================================

Ascii.data

-------------------

ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200000>
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200002
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200004
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200001
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200006

Sample OUTPUT

--------------------------------------

Binary.data

--------------------------------------

0100000101000010010000110011101000111010010000110100001001000001001110100011101001000001010001100100000101000110010000010100011001000110010000010100011001000001010001100011101000111010001110010011100000110111001101110011011000110101001101010011010000111010001110100011000100110010001100000011100100110010001100000011000100110110001110100011101000110001001100100011000000110000001100000011000000110000001111100
0100000101000010010000110011101000111010010000110100001001000001001110100011101001000001010001100100000101000110010000010100011001000110010000010100011001000001010001100011101000111010001110010011100000110111001101110011011000110101001101010011010000111010001110100011000100110010001100000011100100110010001100000011000100110110001110100011101000110001001100100011000000110000001100000011000000110010
0100000101000010010000110011101000111010010000110100001001000001001110100011101001000001010001100100000101000110010000010100011001000110010000010100011001000001010001100011101000111010001110010011100000110111001101110011011000110101001101010011010000111010001110100011000100110010001100000011100100110010001100000011000100110110001110100011101000110001001100100011000000110000001100000011000000110100
0100000101000010010000110011101000111010010000110100001001000001001110100011101001000001010001100100000101000110010000010100011001000110010000010100011001000001010001100011101000111010001110010011100000110111001101110011011000110101001101010011010000111010001110100011000100110010001100000011100100110010001100000011000100110110001110100011101000110001001100100011000000110000001100000011000000110001
0100000101000010010000110011101000111010010000110100001001000001001110100011101001000001010001100100000101000110010000010100011001000110010000010100011001000001010001100011101000111010001110010011100000110111001101110011011000110101001101010011010000111010001110100011000100110010001100000011100100110010001100000011000100110110001110100011101000110001001100100011000000110000001100000011000000110110

Sample outpiut after conversion from binary to ascii:-

----------------------------

fromBinToAscii.data

-------------------------------

ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200000>

ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200002
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200004
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200001
ABC::CBA::AFAFAFFAFAF::98776554::12092016::1200006

Thanks

Hope you like it.

Write a program that has two methods/functions, namely, a) Method/function that generates a bit stream from a text file with bit stuffing. Recall that for bit s
Write a program that has two methods/functions, namely, a) Method/function that generates a bit stream from a text file with bit stuffing. Recall that for bit s
Write a program that has two methods/functions, namely, a) Method/function that generates a bit stream from a text file with bit stuffing. Recall that for bit s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site