Top Description Fields Constructors Methods
com.ibm.icu.text

public final Class UnicodeCompressor

extends Object
implements SCSU
Class Inheritance
All Implemented Interfaces
com.ibm.icu.text.SCSU

A compression engine implementing the Standard Compression Scheme for Unicode (SCSU) as outlined in Unicode Technical Report #6.

The SCSU works by using dynamically positioned windows consisting of 128 consecutive characters in Unicode. During compression, characters within a window are encoded in the compressed stream as the bytes 0x7F - 0xFF. The SCSU provides transparency for the characters (bytes) between U+0000 - U+00FF. The SCSU approximates the storage size of traditional character sets, for example 1 byte per character for ASCII or Latin-1 text, and 2 bytes per character for CJK ideographs.

USAGE

The static methods on UnicodeCompressor may be used in a straightforward manner to compress simple strings:

 String s = ... ; // get string from somewhere
 byte [] compressed = UnicodeCompressor.compress(s);

The static methods have a fairly large memory footprint. For finer-grained control over memory usage, UnicodeCompressor offers more powerful APIs allowing iterative compression:

 // Compress an array "chars" of length "len" using a buffer of 512 bytes
 // to the OutputStream "out"

 UnicodeCompressor myCompressor         = new UnicodeCompressor();
 final static int  BUFSIZE              = 512;
 byte []           byteBuffer           = new byte [ BUFSIZE ];
 int               bytesWritten         = 0;
 int []            unicharsRead         = new int [1];
 int               totalCharsCompressed = 0;
 int               totalBytesWritten    = 0;

 do {
   // do the compression
   bytesWritten = myCompressor.compress(chars, totalCharsCompressed, 
                                        len, unicharsRead,
                                        byteBuffer, 0, BUFSIZE);

   // do something with the current set of bytes
   out.write(byteBuffer, 0, bytesWritten);

   // update the no. of characters compressed
   totalCharsCompressed += unicharsRead[0];

   // update the no. of bytes written
   totalBytesWritten += bytesWritten;

 } while(totalCharsCompressed < len);

 myCompressor.reset(); // reuse compressor
Author
Stephen F. Booth
ICU Status
Stable since ICU 2.4.
See Also
UnicodeDecompressor

Field Summary

Modifier and TypeField and Description
private int
fCurrentWindow

Alias to current dynamic window

private int[]
fIndexCount

Keeps count of times character indices are encountered

private int
fMode

Current compression mode

private int[]
fOffsets

Dynamic compression window offsets

private int
fTimeStamp

The current time stamp

private int[]
fTimeStamps

The time stamps indicate when a window was last defined

private static boolean[]
sSingleTagTable

For quick identification of a byte as a single-byte mode tag

private static boolean[]
sUnicodeTagTable

For quick identification of a byte as a unicode mode tag

Constructor Summary

AccessConstructor and Description
public
UnicodeCompressor()

Create a UnicodeCompressor.

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

A byte array containing the compressed characters.
compress
(String
The string to compress.
buffer
)

Compress a string into a byte array.

public static byte[]

Returns:

A byte array containing the compressed characters.
compress
(char[]
The character buffer to compress.
buffer
,
int
The start of the character run to compress.
start
,
int
The limit of the character run to compress.
limit
)

Compress a Unicode character array into a byte array.

public int

Returns:

The number of bytes written to byteBuffer.
compress
(char[]
The character buffer to compress.
charBuffer
,
int
The start of the character run to compress.
charBufferStart
,
int
The limit of the character run to compress.
charBufferLimit
,
int[]
A one-element array. If not null, on return the number of characters read from charBuffer.
charsRead
,
byte[]
A buffer to receive the compressed data. This buffer must be at minimum four bytes in size.
byteBuffer
,
int
The starting offset to which to write compressed data.
byteBufferStart
,
int
The limiting offset for writing compressed data.
byteBufferLimit
)

Compress a Unicode character array into a byte array.

private int

Returns:

The dynamic window containing c, or INVALIDWINDOW if not defined.
findDynamicWindow
(int
The character in question
c
)

Determine if a dynamic window for a certain character is defined

private static int

Returns:

The static window containing c, or INVALIDWINDOW if not defined.
findStaticWindow
(int
The character in question
c
)

Determine if a static window for a certain character is defined

private int
getLRDefinedWindow()

Find the least-recently defined window

private boolean

Returns:

true if c will fit in whichWindow, false otherwise.
inDynamicWindow
(int
The character to test
c
,
int
The dynamic window the test
whichWindow
)

Determine if a character is in a dynamic window.

private static boolean

Returns:

true if c will fit in whichWindow, false otherwise.
inStaticWindow
(int
The character to test
c
,
int
The static window the test
whichWindow
)

Determine if a character is in a static window.

private static boolean

Returns:

true if the c is compressible, false otherwise.
isCompressible
(int
The character to test.
c
)

Determine if a character is compressible.

private static int

Returns:

An index for c
makeIndex
(int
The character in question.
c
)

Create the index value for a character.

public void
reset()

Reset the compressor to its initial state.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fCurrentWindowback to summary
private int fCurrentWindow

Alias to current dynamic window

fIndexCountback to summary
private int[] fIndexCount

Keeps count of times character indices are encountered

fModeback to summary
private int fMode

Current compression mode

fOffsetsback to summary
private int[] fOffsets

Dynamic compression window offsets

fTimeStampback to summary
private int fTimeStamp

The current time stamp

fTimeStampsback to summary
private int[] fTimeStamps

The time stamps indicate when a window was last defined

sSingleTagTableback to summary
private static boolean[] sSingleTagTable

For quick identification of a byte as a single-byte mode tag

sUnicodeTagTableback to summary
private static boolean[] sUnicodeTagTable

For quick identification of a byte as a unicode mode tag

Constructor Detail

UnicodeCompressorback to summary
public UnicodeCompressor()

Create a UnicodeCompressor. Sets all windows to their default values.

ICU Status
Stable since ICU 2.4.
See Also
reset

Method Detail

compressback to summary
public static byte[] compress(String buffer)

Compress a string into a byte array.

Parameters
buffer:String

The string to compress.

Returns:byte[]

A byte array containing the compressed characters.

ICU Status
Stable since ICU 2.4.
See Also
compress(char [], int, int)
compressback to summary
public static byte[] compress(char[] buffer, int start, int limit)

Compress a Unicode character array into a byte array.

Parameters
buffer:char[]

The character buffer to compress.

start:int

The start of the character run to compress.

limit:int

The limit of the character run to compress.

Returns:byte[]

A byte array containing the compressed characters.

ICU Status
Stable since ICU 2.4.
See Also
compress(String)
compressback to summary
public int compress(char[] charBuffer, int charBufferStart, int charBufferLimit, int[] charsRead, byte[] byteBuffer, int byteBufferStart, int byteBufferLimit)

Compress a Unicode character array into a byte array. This function will only consume input that can be completely output.

Parameters
charBuffer:char[]

The character buffer to compress.

charBufferStart:int

The start of the character run to compress.

charBufferLimit:int

The limit of the character run to compress.

charsRead:int[]

A one-element array. If not null, on return the number of characters read from charBuffer.

byteBuffer:byte[]

A buffer to receive the compressed data. This buffer must be at minimum four bytes in size.

byteBufferStart:int

The starting offset to which to write compressed data.

byteBufferLimit:int

The limiting offset for writing compressed data.

Returns:int

The number of bytes written to byteBuffer.

ICU Status
Stable since ICU 2.4.
findDynamicWindowback to summary
private int findDynamicWindow(int c)

Determine if a dynamic window for a certain character is defined

Parameters
c:int

The character in question

Returns:int

The dynamic window containing c, or INVALIDWINDOW if not defined.

findStaticWindowback to summary
private static int findStaticWindow(int c)

Determine if a static window for a certain character is defined

Parameters
c:int

The character in question

Returns:int

The static window containing c, or INVALIDWINDOW if not defined.

getLRDefinedWindowback to summary
private int getLRDefinedWindow()

Find the least-recently defined window

inDynamicWindowback to summary
private boolean inDynamicWindow(int c, int whichWindow)

Determine if a character is in a dynamic window.

Parameters
c:int

The character to test

whichWindow:int

The dynamic window the test

Returns:boolean

true if c will fit in whichWindow, false otherwise.

inStaticWindowback to summary
private static boolean inStaticWindow(int c, int whichWindow)

Determine if a character is in a static window.

Parameters
c:int

The character to test

whichWindow:int

The static window the test

Returns:boolean

true if c will fit in whichWindow, false otherwise.

isCompressibleback to summary
private static boolean isCompressible(int c)

Determine if a character is compressible.

Parameters
c:int

The character to test.

Returns:boolean

true if the c is compressible, false otherwise.

makeIndexback to summary
private static int makeIndex(int c)

Create the index value for a character. For more information on this function, refer to table X-3 UTR6.

Parameters
c:int

The character in question.

Returns:int

An index for c

resetback to summary
public void reset()

Reset the compressor to its initial state.

ICU Status
Stable since ICU 2.4.