JAVA To understand the concept of encap sulation/decap sulation, you will create
ID: 3754670 • Letter: J
Question
JAVA
To understand the concept of encap sulation/decap sulation, you will create a simulation project of an envelope letter that will have three encapsulation headers, plus the message, (to make things easy for you, we are limiting our letters to the US states, and making each header a fixed sized) The Person to whom the letter is intended The Address of the letter (address, city) The State to which the letter is intended Your message will be a simple text message of no more than 1024 bytes (fixed size) The person header will take 1024 bytes, fixed size The Address header will take 1024 bytes, fixed size The State header will take 1024 bytes, fixed size Your program will have two parts: 1- The encapsulation part Ask the user for the message, create a 1024-byte message, the unused art will be empty spaces Message: 1024 Bytes Ask the user for the person to whom the letter is intended, create a 1024-byte person header, the unused art will be empty spaces. This will be appended to the front of the message part to create a 2048-byte person-message-packet Person: 1024 Bvtes Ask the user for the Address to which the letter is intended, create a 1024-byte address header, the unused part will be empty spaces. This will be appended to the front of the a. b. Message: 1024 Bytes c. erson-message-packet to create a 3072-byte address-person-message-packet Address: 1024 Byte Ask the user for the State to which the letter is intended, create a 1024-byte address header, the unused part will be empty spaces. This will be appended to the front of the address-person-message-packet to create a 4098-byte state-address-person message-packet State: 1024 Byte Address: 1024 BytePerson: 1024 BytesMessage: 1024 Bytes You can store the final 4098 bytes in file called finalPacketraw. Person: 1024 Bvtes Message: 1024 Bytes d. 2- The decncapsulation part (will take the finalpacketraw, file) The decapsulation part will undo the above 4-part encapsulation one step at a time a. Extract the state from the state-address-p erson-message-packet i. Show the state and create 3072-byte address-person-message- packet b. Extract the address from the address-person-message-packet Extract the person from the person-message-packet Extract themessage-packet i. Show the address and create 2048-byte person-message-packet Show the person and create 1024-byte message-packet Show the message that is meant for the receiver c. i. d. i.Explanation / Answer
here is your program : --------------->>>>>>>>>>>>
import java.io.BufferedReader;
import java.io.File;
import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.OutputStream;
public class EncapsDecaps{
public static void main(String[] args) {
byte[] bytes = new byte[4098];
File file = new File("demo.txt");
Encapsulation(bytes);
try {
OutputStream os = new FileOutputStream(file);
os.write(bytes);
os.close();
Decapsulation(new File("demo.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Encapsulation(byte[] bytes){
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Message : ");
String in = sc.nextLine();
byte[] msg = in.getBytes();
System.out.println("Enter The Person details : ");
in = sc.nextLine();
byte[] pd = in.getBytes();
System.out.println("Enter The Address : ");
in = sc.nextLine();
byte[] ad = in.getBytes();
System.out.println("Enter The State : ");
in = sc.nextLine();
byte[] sd = in.getBytes();
int c = 0;
for(int i = 3072;i<bytes.length;i++){
if(c < msg.length){
bytes[i] = msg[c++];
}else{
bytes[i] = ' ';
}
}
c = 0;
for(int i = 2048;i<3072;i++){
if(c < pd.length){
bytes[i] = pd[c++];
}else{
bytes[i] = ' ';
}
}
c = 0;
for(int i = 1024;i<2048;i++){
if(c < ad.length){
bytes[i] = ad[c++];
}else{
bytes[i] = ' ';
}
}
c = 0;
for(int i = 0;i<1024;i++){
if(c < sd.length){
bytes[i] = sd[c++];
}else{
bytes[i] = ' ';
}
}
}
public static void Decapsulation(File file) throws Exception {
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
byte[] msg = new byte[1024];
byte[] pd = new byte[1024];
byte[] ad = new byte[1024];
byte[] sd = new byte[1024];
int c = 0;
for(int i = 0;i<1024;i++){
sd[i] = bytesArray[c++];
}
System.out.println("Printing Decapsulation Datas : ");
System.out.println("State : "+ (new String(sd)).trim());
for(int i = 0;i<1024;i++){
ad[i] = bytesArray[c++];
}
System.out.println("Address : "+(new String(ad)).trim());
for(int i = 0;i<1024;i++){
pd[i] = bytesArray[c++];
}
System.out.println("Person : "+(new String(pd)).trim());
for(int i = 0;i<1024;i++){
msg[i] = bytesArray[c++];
}
System.out.println("Message : "+(new String(msg)).trim());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.