org.apache.commons.compress.archivers.zip
Class Simple8BitZipEncoding

java.lang.Object
  extended by org.apache.commons.compress.archivers.zip.Simple8BitZipEncoding
All Implemented Interfaces:
ZipEncoding

 class Simple8BitZipEncoding
extends java.lang.Object
implements ZipEncoding

This ZipEncoding implementation implements a simple 8bit character set, which mets the following restrictions:

These restrictions most notably apply to the most prominent omissions of java-1.4's Charset implementation, Cp437 and Cp850.

The methods of this class are reentrant.


Nested Class Summary
private static class Simple8BitZipEncoding.Simple8BitChar
          A character entity, which is put to the reverse mapping table of a simple encoding.
 
Field Summary
private  char[] highChars
          The characters for byte values of 128 to 255 stored as an array of 128 chars.
private  java.util.List reverseMapping
          A list of Simple8BitZipEncoding.Simple8BitChar objects sorted by the unicode field.
 
Constructor Summary
Simple8BitZipEncoding(char[] highChars)
           
 
Method Summary
 boolean canEncode(java.lang.String name)
          Check, whether the given string may be losslessly encoded using this encoding.
 boolean canEncodeChar(char c)
           
 java.lang.String decode(byte[] data)
           
 char decodeByte(byte b)
          Return the character code for a given encoded byte.
 java.nio.ByteBuffer encode(java.lang.String name)
          Encode a filename or a comment to a byte array suitable for storing it to a serialized zip entry.
private  Simple8BitZipEncoding.Simple8BitChar encodeHighChar(char c)
           
 boolean pushEncodedChar(java.nio.ByteBuffer bb, char c)
          Pushes the encoded form of the given character to the given byte buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

highChars

private final char[] highChars
The characters for byte values of 128 to 255 stored as an array of 128 chars.


reverseMapping

private final java.util.List reverseMapping
A list of Simple8BitZipEncoding.Simple8BitChar objects sorted by the unicode field. This list is used to binary search reverse mapping of unicode characters with a character code greater than 127.

Constructor Detail

Simple8BitZipEncoding

public Simple8BitZipEncoding(char[] highChars)
Parameters:
highChars - The characters for byte values of 128 to 255 stored as an array of 128 chars.
Method Detail

decodeByte

public char decodeByte(byte b)
Return the character code for a given encoded byte.

Parameters:
b - The byte to decode.
Returns:
The associated character value.

canEncodeChar

public boolean canEncodeChar(char c)
Parameters:
c - The character to encode.
Returns:
Whether the given unicode character is covered by this encoding.

pushEncodedChar

public boolean pushEncodedChar(java.nio.ByteBuffer bb,
                               char c)
Pushes the encoded form of the given character to the given byte buffer.

Parameters:
bb - The byte buffer to write to.
c - The character to encode.
Returns:
Whether the given unicode character is covered by this encoding. If false is returned, nothing is pushed to the byte buffer.

encodeHighChar

private Simple8BitZipEncoding.Simple8BitChar encodeHighChar(char c)
Parameters:
c - A unicode character in the range from 0x0080 to 0x7f00
Returns:
A Simple8BitChar, if this character is covered by this encoding. A null value is returned, if this character is not covered by this encoding.

canEncode

public boolean canEncode(java.lang.String name)
Description copied from interface: ZipEncoding
Check, whether the given string may be losslessly encoded using this encoding.

Specified by:
canEncode in interface ZipEncoding
Parameters:
name - A filename or ZIP comment.
Returns:
Whether the given name may be encoded with out any losses.
See Also:
ZipEncoding.canEncode(java.lang.String)

encode

public java.nio.ByteBuffer encode(java.lang.String name)
Description copied from interface: ZipEncoding
Encode a filename or a comment to a byte array suitable for storing it to a serialized zip entry.

Examples for CP 437 (in pseudo-notation, right hand side is C-style notation):

  encode("?_for_Dollar.txt") = "%U20AC_for_Dollar.txt"
  encode("?lf?sser.txt") = "\231lf\204sser.txt"
 

Specified by:
encode in interface ZipEncoding
Parameters:
name - A filename or ZIP comment.
Returns:
A byte buffer with a backing array containing the encoded name. Unmappable characters or malformed character sequences are mapped to a sequence of utf-16 words encoded in the format %Uxxxx. It is assumed, that the byte buffer is positioned at the beginning of the encoded result, the byte buffer has a backing array and the limit of the byte buffer points to the end of the encoded result.
See Also:
ZipEncoding.encode(java.lang.String)

decode

public java.lang.String decode(byte[] data)
                        throws java.io.IOException
Specified by:
decode in interface ZipEncoding
Parameters:
data - The byte values to decode.
Returns:
The decoded string.
Throws:
java.io.IOException
See Also:
ZipEncoding.decode(byte[])