Skip to content

Using and extending the code

flanglet edited this page Jun 11, 2021 · 4 revisions

Compressing/Decompressing data

Here is how to compress/decompress a block to/from a file using RLT+TEXT as transform, Huffman as entropy codec, using a block size of 1 MB, 4 jobs and a checksum:

TBD

Implementing a new transform

Here is how to implement and add a new transform to kanzi.

  • Step 1: write the transform code

For example:

   import java.util.Map;
   import kanzi.ByteTransform;
   import kanzi.SliceByteArray;

   class SuperDuperTransform implements ByteTransform
   {
       public SuperDuperTransform() {}
       public SuperDuperTransform(Map<String, Object> context) {}

       public boolean forward(SliceByteArray input, SliceByteArray output)
       { 
           final int count = input.length;

           if (output.length - output.index < count)
               return false;

           for (int i = 0; i < count; i++)
               dst[i] = (byte) (src[i] ^ 0xAA);

           input.index += count;
           output.index += count;
           return true;
       }

       public boolean inverse(SliceByteArray input, SliceByteArray output)
       {
           final int count = input.length;

           for (int i = 0; i < count; i++)
               dst[i] = (byte) (src[i] ^ 0xAA);

           input.index += count;
           output.index += count;
           return true;

       }

       int getMaxEncodedLength(int inputLen) const { return inputLen; }
   };

Always provide a constructor with a Context: the context map contains all the application wide information (such as block size, number of jobs, input & output names, etc ...). Always implement ByteTransform and do not create more threads that the jobs value provided in the context. Implement forward and inverse methods as well as getMaxEncodedLength(int). Do not write to System.out or System.err. Be aware that your code must be multi-thread safe.

  • Step 2: Register the transform in tranform/TransformFactory.java

Add the type, say

    public static final short SUPERDUPER_TYPE = 63;

Let us say you use the name "SUPERDUPER" for the transform. Update the following methods:

   private long getTypeToken(String name)
   private static ByteTransform newFunctionToken(Map<String, Object> ctx, int functionType)
   private static String getNameToken(int functionType)
  • step 3: Update the help message in app/Kanzi.java

In Kanzi.printHelp, add the SUPERDUPER transform to the list in the -t option section.

  • This is it. For example, run
java -jar kanzi.jar -i foo.txt -f -t SUPERDUPER -e none -j 2 -v 4
Clone this wiki locally