Write a pair of computer programs, one that accepts a data file as input and pro
ID: 3541366 • Letter: W
Question
Write a pair of computer programs, one that accepts a data file as input and produces a
byte stuffed version of the file according to the mapping
Byte In Payload Sequence Sent
SOH ESC A
EOT ESC B
ESC ESC C
and another that removes byte stuffing. Show that your programs interoperate with those written by
others.
Basically, you get a file that looks similar to this:
This is a test
ESC
TEST TEST TEST
SOH
TEST TEST TEST
EOT
TEST TEST TEST
ESC
TEST TEST TEST
and it should change the file to this and resave:
This is a test
ESC C
TEST TEST TEST
ESC A
TEST TEST TEST
ESC B
TEST TEST TEST
ESC C
TEST TEST TEST
********HERE IS MY CODE OUTPUT AND INPUT AT THE BOTTOM OF CODE*************************
package bytestuffing;
import java.io.*;
/**
*
* Byte In Payload Sequence Sent
* SOH ESC A
* EOT ESC B
* ESC ESC C
*/
public class ByteStuffing
{
/**
* This program will take a payload and turn it into a sequence set.
*/
public static void main(String args[])
{
//Payloads and replacements respectively.
String pay1 = "SOH";
String seq1 = "ESC A";
String pay2 = "EOT";
String seq2 = "ESC B";
String pay3 = "ESC";
String seq3 = "ESC C";
//Line counter for error check
int line = 0;
//Store text file line
String str;
try
{
//Open the file
BufferedReader bf = new BufferedReader(new FileReader("C:\Users\Phillips Custom PC\Desktop\ByteStuffing\byteStuff.txt"));
FileOutputStream fos2=new FileOutputStream("C:\Users\Phillips Custom PC\Desktop\ByteStuffing\byteStuffUpdated.txt");
DataOutputStream output = new DataOutputStream (fos2);
//read file line by line and replace str with sequencer if payload is found
while ((str = bf.readLine()) != null)
{
if (str == pay1)
{
str = seq1;
}
else if (str == pay2)
{
str = seq2;
}
else if (str == pay3)
{
str = seq3;
}
str = str + " ";
output.writeBytes(str);
//error check to ensure entire file is being read
System.out.println("line " + line);
line++;
}
}
catch (IOException ioe)
{
System.err.println ("I/O Error - " + ioe);
}
}
}
/*
Input:
This is a test
ESC
TEST TEST TEST
SOH
TEST TEST TEST
EOT
TEST TEST TEST
ESC
TEST TEST TEST
* Output I get:
This is a testESCTEST TEST TESTSOHTEST TEST TESTEOTTEST TEST TESTESCTEST TEST TEST
* As you can see it is ignoring my new line character and not changing anything.
*/
data-mce-bogus="1">
Explanation / Answer
import java.io.*;
/**
*
* Byte In Payload Sequence Sent
* SOH ESC A
* EOT ESC B
* ESC ESC C
*/
public class ByteStuffing
{
/**
* This program will take a payload and turn it into a sequence set.
*/
public static void main(String args[])
{
//Payloads and replacements respectively.
String pay1 = "SOH";
String seq1 = "ESC A";
String pay2 = "EOT";
String seq2 = "ESC B";
String pay3 = "ESC";
String seq3 = "ESC C";
//Line counter for error check
int line = 0;
//Store text file line
String str;
try
{
//Open the file
BufferedReader bf = new BufferedReader(new FileReader("byteStuff.txt"));
FileOutputStream fos2=new FileOutputStream("byteStuffUpdated.txt");
DataOutputStream output = new DataOutputStream (fos2);
//read file line by line and replace str with sequencer if payload is found
while ((str = bf.readLine()) != null)
{
if (str.equals(pay1))
{
str = seq1;
}
else if (str.equals(pay2))
{
str = seq2;
}
else if (str.equals(pay3))
{
str = seq3;
}
str = str + " ";
output.writeBytes(str);
//error check to ensure entire file is being read
System.out.println("line " + line);
line++;
}
}
catch (IOException ioe)
{
System.err.println ("I/O Error - " + ioe);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.