Skip to content

Java use guide

Martsim edited this page Jan 26, 2018 · 4 revisions

Requirements

The code was tested with Java8

Using Huffman coding

File: src/Huffman.java

Functions:

  • hcEncode(text) - encodes the text and returns the codeword.
  • decode(encoded string) - decodes a bitstring. Assumes that it was previously encoded with the same object.

Run:

Modify the content of the main method or import the class, create an object and encode.

Using ranged ANS coding

File: src/RANSimpl.java

Functions:

Lower level:

  • encode(symbol, x) - encodes the symbol to the given x and returns the new x.
  • decode(x) - decodes a symbol and returns the new x.

Higher level:

  • encode_text_with_dict(text, possible x value) - calculates probabilities of symbols in given text and then encodes the text based on this. Returns the variable into which the text is encoded. Default starting x is 0.
  • encode_text_with_dict_check(text) - encodes the text and checks if it can be decoded as well. Returns the coded value.
  • decode_text(x, nr_of_symbols) - takes the encoded x and the number of symbols to read from this x.

Example:

  • demo_dickens(blockSize) - expects 'dickens' text file to exist in the running directory. Encodes the text into given block size pieces.

Run:

Either import the prementioned functions or run the main method for a test.

Comparing Huffman coding and rANS

File: src/CompareTwo.java

Classes:

  • ComparisonValues - used by the comparer for storing calculated values during the test.
  • CompareTwo - class which runs the tests.

Function:

  • runTests(block_size, filename) - used for running tests. Reads chunks of block_size from given file and encodes them with Huffman coding and rANS. Calculates the average compression rates, how many times which algorithm was better.

Run:

Run from the main method or import the class and use runTests.