Monday, January 10, 2011

java code for character stuffing and destuffing

Although using characters to mark the beginning and end of each frame has advantages, mostco mp u te r
networks

cannot afford to reserve characters for use by the network. Instead, the network permits ana pp lica t ion
to transfer arbitrary data values across the network. In particular, an application may choose to send data
that contains one or more occurrences of characters like soh and eot that are used for framing. In
general, to distinguish between data being sent and control information such as frame delimiters, network
systems arrange for the sending side to change the data slightly before it is sent, and then arrange for the
receiving side to restore the original data before passing it to the receiving application.

Thus, although applications using the network can transfer arbitrary data, the network system never
confuses data with control information. Because network systems usually insert bits or bytes to change
data for transmission, the technique is known as data stuffing. The terms character stuffing and byte
stuffing refer to data stuffing used with character orientedha rd wa re, and bit stuffing which is more
common, refers to data stuffing used with bit oriented hardware. To implement byte stuffing, a sender
must scan an entire data block and perform the mapping before any data is sent. Byte stuffing can solve
the problem by reserving a third character to mark occurrences of special characters in the data.

Use reserved characters to indicate the start and end of a frame. For instance, use the two-character
sequence DLE STX (Data-Link Escape, Start of TeXt) to signal the beginning of a frame, and the
sequence DLE ETX (End of TeXt) to flag the frame's end.
Problem: What happens if the two-character sequence DLE ETX happens to appear in the frame itself?

Solution: Use character stuffing; within the frame, replace every occurrence of DLE with the two-character sequence DLE DLE. The receiver reverses the processes, replacing every occurrence of DLE DLE with a single DLE.
Example: If the frame contained ``A B DLE D E DLE'', the characters transmitted over the channel would
be ``DLE STX A B DLE DLE D E DLE DLE DLE ETX''.
Disadvantage: character is the smallest unit that can be operated on; not all architectures are byte
oriented