Class NioZipEncoding
- java.lang.Object
-
- org.apache.commons.compress.archivers.zip.NioZipEncoding
-
- All Implemented Interfaces:
CharsetAccessor
,ZipEncoding
class NioZipEncoding extends java.lang.Object implements ZipEncoding, CharsetAccessor
A ZipEncoding, which uses a java.nioCharset
to encode names.The methods of this class are reentrant.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.charset.Charset
charset
private static char[]
HEX_CHARS
private static char
REPLACEMENT
private static byte[]
REPLACEMENT_BYTES
private static java.lang.String
REPLACEMENT_STRING
private boolean
useReplacement
-
Constructor Summary
Constructors Constructor Description NioZipEncoding(java.nio.charset.Charset charset, boolean useReplacement)
Construct an NioZipEncoding using the given charset.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 file name or a comment to a byte array suitable for storing it to a serialized zip entry.private static java.nio.ByteBuffer
encodeFully(java.nio.charset.CharsetEncoder enc, java.nio.CharBuffer cb, java.nio.ByteBuffer out)
private static java.nio.CharBuffer
encodeSurrogate(java.nio.CharBuffer cb, char c)
private static int
estimateIncrementalEncodingSize(java.nio.charset.CharsetEncoder enc, int charCount)
Estimate the size needed for remaining charactersprivate static int
estimateInitialBufferSize(java.nio.charset.CharsetEncoder enc, int charChount)
Estimate the initial encoded size (in bytes) for a character buffer.java.nio.charset.Charset
getCharset()
Provides access to the character set associated with an object.private java.nio.charset.CharsetDecoder
newDecoder()
private java.nio.charset.CharsetEncoder
newEncoder()
-
-
-
Field Detail
-
charset
private final java.nio.charset.Charset charset
-
useReplacement
private final boolean useReplacement
-
REPLACEMENT
private static final char REPLACEMENT
- See Also:
- Constant Field Values
-
REPLACEMENT_BYTES
private static final byte[] REPLACEMENT_BYTES
-
REPLACEMENT_STRING
private static final java.lang.String REPLACEMENT_STRING
-
HEX_CHARS
private static final char[] HEX_CHARS
-
-
Method Detail
-
getCharset
public java.nio.charset.Charset getCharset()
Description copied from interface:CharsetAccessor
Provides access to the character set associated with an object.This allows nio oriented code to use more natural character encoding/decoding methods, whilst allowing existing code to continue to rely on special-case error handling for UTF-8.
- Specified by:
getCharset
in interfaceCharsetAccessor
- Returns:
- the character set associated with this object
-
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 interfaceZipEncoding
- Parameters:
name
- A file name 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 file name 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 interfaceZipEncoding
- Parameters:
name
- A file name 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 interfaceZipEncoding
- Parameters:
data
- The byte values to decode.- Returns:
- The decoded string.
- Throws:
java.io.IOException
- on error- See Also:
ZipEncoding.decode(byte[])
-
encodeFully
private static java.nio.ByteBuffer encodeFully(java.nio.charset.CharsetEncoder enc, java.nio.CharBuffer cb, java.nio.ByteBuffer out)
-
encodeSurrogate
private static java.nio.CharBuffer encodeSurrogate(java.nio.CharBuffer cb, char c)
-
newEncoder
private java.nio.charset.CharsetEncoder newEncoder()
-
newDecoder
private java.nio.charset.CharsetDecoder newDecoder()
-
estimateInitialBufferSize
private static int estimateInitialBufferSize(java.nio.charset.CharsetEncoder enc, int charChount)
Estimate the initial encoded size (in bytes) for a character buffer.The estimate assumes that one character consumes uses the maximum length encoding, whilst the rest use an average size encoding. This accounts for any BOM for UTF-16, at the expense of a couple of extra bytes for UTF-8 encoded ASCII.
- Parameters:
enc
- encoder to use for estimatescharChount
- number of characters in string- Returns:
- estimated size in bytes.
-
estimateIncrementalEncodingSize
private static int estimateIncrementalEncodingSize(java.nio.charset.CharsetEncoder enc, int charCount)
Estimate the size needed for remaining characters- Parameters:
enc
- encoder to use for estimatescharCount
- number of characters remaining- Returns:
- estimated size in bytes.
-
-