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

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

 class FallbackZipEncoding
extends java.lang.Object
implements ZipEncoding

A fallback ZipEncoding, which uses a java.io means to encode names.

This implementation is not suitable for encodings other than utf-8, because java.io encodes unmappable character as question marks leading to unreadable ZIP entries on some operating systems.

Furthermore this implementation is unable to tell whether a given name can be safely encoded or not.

This implementation acts as a last resort implementation, when neither Simple8BitZipEnoding nor NioZipEncoding is available.

The methods of this class are reentrant.


Field Summary
private  java.lang.String charset
           
 
Constructor Summary
FallbackZipEncoding()
          Construct a fallback zip encoding, which uses the platform's default charset.
FallbackZipEncoding(java.lang.String charset)
          Construct a fallback zip encoding, which uses the given charset.
 
Method Summary
 boolean canEncode(java.lang.String name)
          Check, whether the given string may be losslessly encoded using this encoding.
 java.lang.String decode(byte[] data)
           
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

charset

private final java.lang.String charset
Constructor Detail

FallbackZipEncoding

public FallbackZipEncoding()
Construct a fallback zip encoding, which uses the platform's default charset.


FallbackZipEncoding

public FallbackZipEncoding(java.lang.String charset)
Construct a fallback zip encoding, which uses the given charset.

Parameters:
charset - The name of the charset or null for the platform's default character set.
Method Detail

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)
                           throws java.io.IOException
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.
Throws:
java.io.IOException
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[])