Implementing Byte stuffing using Java

Learn via video courses
Topics Covered

Overview

Byte stuffing is used in the variable size framing of the data link layer. In the variable size framing flag is used for the separation of different data frames. And data may contain pattern which is similar to the flag pattern. So to differentiate the part of the data that is similar to the flag data byte stuffing is used.

Introduction to Byte Stuffing

In variable-size framing, we need to define the way the separation of the two or more frames from each other. Byte Stuffing is used for the accomplishment of this task. For distinguishing between different frames, in byte stuffing flag of 8-bit ('F') is added in the frame starting and the frame ending. 'F' in data represents the frames' starting and ending. But the discrepancy is caused by this method, as there may be a situation that flag added in the starting and ending may occur in the data carried by the frame. Byte stuffing is the method that helps in overcoming this problem by adding the extra 8-bit escape sequence (‘E’) before the occurrence of the flag pattern in the data. Then the data is de stuff at the receiver end for getting the original data. But there may be a situation when the escape characters may also occur in the data, so this situation is also overcome by the above-explained approach. Escape characters 'E' is added to the data before the occurrence of the Escape characters in the data carried by the frame.

For more clear and easy understanding we will consider three-byte sequences in the data sent: F: Flag Sequence E: Escape Sequence D: Any other Data Sequence

Refer to the below image for the byte stuffing byte stuffing and unstuffing

Example

At Sender Side Enter the Data to be Sent : GEF GEF GEF The data being sent to the receiver (after byte stuffing) is: FGEEEF GEEEF GEEEFF Sending Message.... Thank you!! At Receiver Side Successfully received the message!!! The Stuffed Message is: FGEEEF GEEEF GEEEFF The Destuffed Message is: GEF GEF GEF EXITING

We can see in the above example original data will get back after de-stuffing the data received from the sender.

Approach

At Sender (Client) Side

  • An 8-bit flag sequence(F) is added at the starting and end of each data frame.
  • Now check whether there is any part in the data, where a flag sequence (F) will occur.
  • If the answer to the above statement is yes then add an extra escape sequence (E) before such flag sequences.
  • Now if there is any part of the data which is similar to the escape sequence (E), the escape sequence (E) is again stuffed before that part.
  • And now this stuffed data is sent from the sender.

Example: If we take the example given above GEF GEF GEF then first of all we add the flag 'F' at starting and ending of the frame FGEF GEF GEFF. Now if we see the flag pattern 'F' appearing in the data so we stuff 'E' before every occurrence of 'F' in the data FGEEF GEEF GEEFF. Now if we see the Escape character 'E' is also there in the data so we stuff 'E' before the occurrence of the escape characters 'E' in the data FGEEEF GEEEF GEEEFF.

At Receiver (Server) Side

  • The first and last byte of the data frame is skipped by the receiver as they are for representing starting and ending of the flag means they do not contain any important information.
  • Now the data frame is scanned byte by byte and if two contiguous escape characters(E) are found then the first escape character is de-stuffed.
  • And if we found the escape character followed by a flag sequence(F) then the former character is de-stuffed.
  • Sent data can be accurately recovered by the receiver by following the above strategy.

Example: The receiver has received FGEEEF GEEEF GEEEFF data. Now first of all receiver skips the F present at the starting and end of the data and after that data becomes GEEEF GEEEF GEEEF. And now the receiver will de stuff by removing the first occurrence of E if there are two contiguous occurrences of the E and then we get GEEF GEEF GEEF. And then the E Escape character present before the flag pattern F is de-stuffed from the data and then we get GEF GEF GEF. And the receiver successfully recovered the original data through the process of de-stuffing.

Implementation in Java

At Sender (Client) Side

At Receiver (Server) Side

Conclusion

  • In variable-size framing, we need to define the flag for the separation of the two or more frames from each other.
  • Byte stuffing is the method of adding the extra 8-bit escape sequence (‘E’) before the occurrence of the flag pattern in the data.
  • Escape characters 'E' is also added to the data before the occurrence of the Escape characters 'E' in the data of the frame.