org.at4j.support.comp
Class ByteMoveToFront

java.lang.Object
  extended by org.at4j.support.comp.ByteMoveToFront

public class ByteMoveToFront
extends Object

A move-to-front (MTF) encoder and decoder for bytes. For more information on MTF encoding, seethe Wikipedia article on move-to-front transforms.

This object is not thread safe. Clients must provide external synchronization if they are to use it from several concurrent threads.

Since:
1.1
Author:
Karl Gustafsson
See Also:
IntMoveToFront

Constructor Summary
ByteMoveToFront(byte[] alphabet)
          Create a byte MTF encoder/decoder that transforms bytes using the supplied initial alphabet.
ByteMoveToFront(int minValue, int maxValue)
          Create a byte MTF encoder/decoder that transforms bytes in the range between minValue and maxValue.
 
Method Summary
 byte[] decode(byte[] in, byte[] out)
          Decode an array of bytes and update the MTF alphabet.
 byte decode(int index)
          Decode a single byte and update the MTF alphabet.
 byte[] encode(byte[] in, byte[] out)
          Encode the bytes in in and store them in the array out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteMoveToFront

public ByteMoveToFront(int minValue,
                       int maxValue)
                throws IndexOutOfBoundsException
Create a byte MTF encoder/decoder that transforms bytes in the range between minValue and maxValue.

The initial alphabet of the transformer will be minValue … maxValue.

Parameters:
minValue - The start value of the range. This should be an unsigned byte in the range 0 to 254.
maxValue - The end value of the range. This should be an unsigned byte in the range 1 to 255.
Throws:
IndexOutOfBoundsException - If the min and/or the max values are not unsigned bytes or if the min value is equal to or greater than the max value.

ByteMoveToFront

public ByteMoveToFront(byte[] alphabet)
Create a byte MTF encoder/decoder that transforms bytes using the supplied initial alphabet.

Parameters:
alphabet - The initial alphabet. This byte array is not copied by this method and it will be modified by encoding or decoding operations.
Method Detail

encode

public byte[] encode(byte[] in,
                     byte[] out)
              throws ArrayIndexOutOfBoundsException,
                     IllegalArgumentException
Encode the bytes in in and store them in the array out. The MTF alphabet is also updated by this method.

Parameters:
in - The bytes to encode.
out - The array to store the encoded bytes in. This array must be at least as long as in.
Returns:
out
Throws:
ArrayIndexOutOfBoundsException - If any of the bytes in in are not in the MTF alphabet.
IllegalArgumentException - If the out array is too short.

decode

public byte decode(int index)
Decode a single byte and update the MTF alphabet.

Parameters:
index - The index in the MTF alphabet for the byte.
Returns:
The byte.

decode

public byte[] decode(byte[] in,
                     byte[] out)
              throws ArrayIndexOutOfBoundsException,
                     IllegalArgumentException
Decode an array of bytes and update the MTF alphabet. The decoded bytes are stored in out.

Parameters:
in - The bytes to decode.
out - The array to store the decoded bytes in. This array must be at least as long as in.
Returns:
out
Throws:
ArrayIndexOutOfBoundsException - If any of the bytes in in are not in the MTF alphabet.
IllegalArgumentException - If out is too short.