Coders Packet

Implementing Bit stuffing and Character Stuffing in JAVA programming.

By Gaurav Dhavala

Using JAVA's Collecters package we will implement bit stuffing and character stuffing.

Hi, firstly we will learn about Bit stuffing and Character Stuffing

Bit Stuffing

Bit stuffing is a technique in which we add extra bits to a given set of data. The added bits don't carry any information but rather they help in better communication. It is all about protocol management.

Character Stuffing

Character stuffing or Byte Stuffing is the technique of adding a particular byte to the data section before every byte in the data having the same pattern as the flag. 

In Character destuffing while traversing the data we delete the byte with the same pattern as the given flag and continues traversing the given data frame.

Logic

1) For character stuffing we will create a character array of the given data. While traversing the array as we reach the byte (which is == to the flag) we will append the flag before it. We also add the flag byte at the start also at the end of the data frame.

2) For character destuffing we will create a character array for the given data and while traversing we will increment the variable count (initially zero) whenever we reach the byte (which is == to the flag). When count%2==0 we will delete the current byte and reset the variable count=0 and continue traversing.

3) Then we will create a class that will take the input string/data as an argument and will convert the given data into binary data for further implementation.

4) For bit stuffing we will create an array of the given data and while traversing the array if '1' occurs five times we will append a '0' after them (ex. 111110) to the array. 

5) For bit destuffing we will create an array of the given data and while traversing we will delete all the '0s' that occurred after five '1s'.

Code

import java.util.*;
import java.util.stream.Collectors;

public class Stuffing {
    static Scanner s = new Scanner(System.in);

    static void CharacterDestuffing(String st[], int n, char char_byte) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < n; i++) {
            String temp1 = "", temp2 = "";
            temp1 += char_byte;
            temp2 = temp1 + temp1;
            st[i] = st[i].replaceAll(temp2, temp1);
            sb.append(st[i]);
            if (i != n - 1) {
                sb.append(" ");
            }
        }

        System.out.println(sb);

    }

    // For Character Stuffing
    static void CharacterStuffing() {

        char char_byte = s.nextLine().charAt(0);

        String st[] = s.nextLine().split(" ");
        StringBuffer sb = new StringBuffer();
        sb.append(char_byte);
        int n = st.length;
        for (int i = 0; i < n; i++) {
            String temp1 = "", temp2 = "";
            temp1 += char_byte;
            temp2 = temp1 + temp1;
            st[i] = st[i].replaceAll(temp1, temp2);
            sb.append(st[i]);
            if (i != (n - 1)) {
                sb.append(" ");
            }
        }

        sb.append(char_byte);
        System.out.println(sb);
        CharacterDestuffing(st, n, char_byte);

    }

    // Conversion of string to binary
    static String convertStringToBinary(String input) {

        StringBuilder result = new StringBuilder();
        char[] chars = input.toCharArray();
        for (char aChar : chars) {
            result.append(String.format("%8s", Integer.toBinaryString(aChar)) // char -> int, auto-cast
                    .replaceAll(" ", "0") // zero pads
            );
        }
        return result.toString();

    }

    // BitDestuffing Function
    static void BitDestuffing(String stuffedString) {
        int len = 0;
        String result = "";
        for (int i = 0; i < stuffedString.length(); i++) {
            result += stuffedString.charAt(i);
            if (stuffedString.charAt(i) == '1') {
                len++;
                if (len == 5 && i < stuffedString.length() - 2 && stuffedString.charAt(i + 1) == '0'
                        && stuffedString.charAt(i + 2) == '1') {
                    i++;
                    len = 0;
                }
            } else {
                len = 0;
            }

        }
        System.out.println("After Destuffing : " + result);
        String new_result = "";
        for (int i = 0; i < result.length(); i++) {
            new_result += result.charAt(i);
            if ((i + 1) % 8 == 0) {
                new_result += " ";
            }
        }
        String raw = Arrays.stream(new_result.split(" ")).map(binary -> Integer.parseInt(binary, 2))
                .map(Character::toString).collect(Collectors.joining());

        System.out.println(raw);
    }

    // BitStuffing Function
    static void BitStuffing(String BinaryString) {
        int len = 0;
        String st = "";
        for (int i = 0; i < BinaryString.length(); i++) {
            if (BinaryString.charAt(i) == '1') {
                len++;
            } else {
                len = 0;
            }
            st += String.valueOf(BinaryString.charAt(i));
            if (len == 5 && i < BinaryString.length() - 1 && BinaryString.charAt(i + 1) == '1') {
                len = 0;
                st += "0";
            }
        }
        System.out.println("After stuffing : " + st);
        BitDestuffing(st);
    }

    static void BitStuffingDestuffing() {
        String st[] = s.nextLine().split(" ");
        StringBuilder sb = new StringBuilder();
        for (String value : st) {
            sb.append(value).append(" ");
        }

        String BinaryString = convertStringToBinary(String.valueOf(sb));
        System.out.println("Before stuffing : " + BinaryString);
        BitStuffing(BinaryString);

    }

    public static void main(String[] args) {

        BitStuffingDestuffing();
        CharacterStuffing();
    }
}

Output 

output

 

Download project

Reviews Report

Submitted by Gaurav Dhavala (gauravdhavala)

Download packets of source code on Coders Packet