Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.*;
  5.  
  6. public class Main {
  7.  
  8.     static String binary = "";
  9.  
  10.     public static void decode(ArrayList<Byte> inputBytes) {
  11.         int tableSize =  127;
  12.  
  13.         TreeMap<ArrayList<Byte>, Integer> table = new TreeMap<ArrayList<Byte>, Integer>(new Comparator<ArrayList<Byte>>() {
  14.             @Override
  15.             public int compare(ArrayList<Byte> o1, ArrayList<Byte> o2) {
  16.                 for (int i=0; i<Math.min(o1.size(), o2.size()); i++) {
  17.                     if (o1.get(i) < o2.get(i))
  18.                         return -1;
  19.                     if (o1.get(i) > o2.get(i))
  20.                         return 1;
  21.                 }
  22.                 if (o1.size() < o2.size())
  23.                     return -1;
  24.                 if (o1.size() > o2.size())
  25.                     return 1;
  26.                 return 0;
  27.             }
  28.         });
  29.  
  30.         Integer k = 0;
  31.         for (Byte i = 0; i < 127 ; i++) {
  32.             ArrayList<Byte> arr = new ArrayList<>();
  33.             arr.add(i);
  34.             table.put(arr, k);
  35.             k++;
  36.         }
  37.  
  38.         ArrayList<Byte> currentBytes = new ArrayList<>();
  39.         currentBytes.add(inputBytes.get(0));
  40.         boolean isZeroIndex = true;
  41.         for (Byte b : inputBytes) {
  42.             //Skip first symbol
  43.             if (isZeroIndex) {
  44.                 isZeroIndex = false;
  45.                 continue;
  46.             }
  47.             //TODO: fix (do not create copy of array each time)
  48.             ArrayList<Byte> tempCurrentBytes = new ArrayList<>();
  49.             tempCurrentBytes.addAll(currentBytes);
  50.             tempCurrentBytes.add(b);
  51.  
  52.             if (table.containsKey(tempCurrentBytes)) {
  53.                 currentBytes.add(b);
  54.             } else {
  55.                 System.out.print((table.get(currentBytes)) + " ");
  56.                 binary += String.format("%8s", Integer.toBinaryString(table.get(currentBytes))).replace(' ', '0') + " "; //TODO remove (only for testing)
  57.                 currentBytes.add(b);
  58.  
  59.                 //TODO: fix (do not create copy of array each time)
  60.                 ArrayList<Byte> arrayToPut = new ArrayList<>();
  61.                 for (Byte b1 : currentBytes)
  62.                     arrayToPut.add(b1);
  63.  
  64.                 table.put(arrayToPut, tableSize);
  65.                 tableSize++;
  66.  
  67.                 currentBytes.clear();
  68.                 currentBytes.add(b);
  69.             }
  70.         }
  71.         //TODO fix (do not print last byte without it)
  72.         currentBytes.clear();
  73.         currentBytes.add(inputBytes.get(inputBytes.size() - 1));
  74.         System.out.println(table.get(currentBytes));
  75.         binary += String.format("%8s", Integer.toBinaryString(table.get(currentBytes))).replace(' ', '0') + " "; //TODO remove (only for testing)
  76.     }
  77.  
  78.     public static void encode() {
  79.         int tableSize =  127;
  80.  
  81.         TreeMap<Integer, ArrayList<Byte>> table = new TreeMap<Integer, ArrayList<Byte>>();
  82.  
  83.         Integer k = 0;
  84.         for (Byte i = 0; i < 127 ; i++) {
  85.             ArrayList<Byte> arr = new ArrayList<>();
  86.             arr.add(i);
  87.             table.put(k, arr);
  88.             k++;
  89.         }
  90.  
  91.         String byteString = "";
  92.         int WORD_LENGTH = 8; //TODO will be dynamic when 9-10-11-bit words work
  93.         ArrayList<Integer> bytes = new ArrayList<>(); //Contains codes not bytes
  94.         for (char c : binary.toCharArray()) {
  95.             if (c != ' ') {
  96.                 byteString += c;
  97.             } else {
  98.                 Integer b = Integer.parseInt(byteString, 2);
  99.                 byteString = "";
  100.  
  101.                 bytes.add(b);
  102.             }
  103.         }
  104.  
  105.         boolean isZeroIndex = true;
  106.         System.out.print(table.get(bytes.get(0)).get(0) + " ");
  107.         ArrayList<Byte> currentBytes = new ArrayList<>();
  108.         Integer oldToken = bytes.get(0);
  109.         Byte C = 0; //TODO rename
  110.         for (Integer currentToken : bytes) {
  111.             //Skip first symbol
  112.             if (isZeroIndex) {
  113.                 isZeroIndex = false;
  114.                 continue;
  115.             }
  116.  
  117.             if (!table.containsKey(currentToken)) {
  118.                 currentBytes = new ArrayList<>();
  119.                 for (Byte b : table.get(oldToken))
  120.                     currentBytes.add(b);
  121.                 currentBytes.add(C);
  122.             } else {
  123.                 currentBytes = new ArrayList<>();
  124.                 for (Byte b : table.get(currentToken))
  125.                     currentBytes.add(b);
  126.             }
  127.  
  128.             for (Byte b : currentBytes)
  129.                 System.out.print(b + " ");
  130.             //System.out.print(currentBytes.toString() + " ");
  131.             C = currentBytes.get(0);
  132.             ArrayList<Byte> arrayToPut = new ArrayList<>();
  133.             for (Byte b : table.get(oldToken))
  134.                 arrayToPut.add(b);
  135.             arrayToPut.add(C);
  136.             table.put(tableSize, arrayToPut);
  137.             //System.out.println();
  138.             //System.out.println("PUT " + tableSize + " " + arrayToPut.toString());
  139.             tableSize++;
  140.             oldToken = currentToken;
  141.         }
  142.  
  143.         System.out.println();
  144.     }
  145.  
  146.  
  147.     public static void main(String[] args) throws IOException {
  148.  
  149.         String filename = "src/input.txt";
  150.  
  151.         byte[] fileContents =  Files.readAllBytes(Paths.get(filename));
  152.         ArrayList<Byte> input = new ArrayList<Byte>();
  153.         System.out.println("input in bytes: ");
  154.         for (byte b : fileContents) {
  155.             System.out.print(b + " ");
  156.             input.add(b);
  157.         }
  158.         System.out.println();
  159.  
  160.         System.out.println("Decoded in bytes: ");
  161.         decode(input);
  162.         System.out.println("Decoded in binary: ");
  163.         System.out.println(binary);
  164.  
  165.         System.out.println("Encoded in bytes: ");
  166.         encode();
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement