class NioZipEncoding extends java.lang.Object implements ZipEncoding, CharsetAccessor
Charset
to encode names.
The methods of this class are reentrant.
Modifier and Type | Field and 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 and Description |
---|
NioZipEncoding(java.nio.charset.Charset charset,
boolean useReplacement)
Construct an NioZipEncoding using the given charset.
|
Modifier and Type | Method and 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 filename 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 characters
|
private 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() |
private final java.nio.charset.Charset charset
private final boolean useReplacement
private static final char REPLACEMENT
private static final byte[] REPLACEMENT_BYTES
private static final java.lang.String REPLACEMENT_STRING
private static final char[] HEX_CHARS
NioZipEncoding(java.nio.charset.Charset charset, boolean useReplacement)
charset
- The character set to use.useReplacement
- should invalid characters be replaced, or reported.public java.nio.charset.Charset getCharset()
CharsetAccessor
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.
getCharset
in interface CharsetAccessor
public boolean canEncode(java.lang.String name)
ZipEncoding
canEncode
in interface ZipEncoding
name
- A filename or ZIP comment.ZipEncoding.canEncode(java.lang.String)
public java.nio.ByteBuffer encode(java.lang.String name)
ZipEncoding
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"
encode
in interface ZipEncoding
name
- A filename or ZIP comment.%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.ZipEncoding.encode(java.lang.String)
public java.lang.String decode(byte[] data) throws java.io.IOException
decode
in interface ZipEncoding
data
- The byte values to decode.java.io.IOException
- on errorZipEncoding.decode(byte[])
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 java.nio.charset.CharsetEncoder newEncoder()
private java.nio.charset.CharsetDecoder newDecoder()
private static int estimateInitialBufferSize(java.nio.charset.CharsetEncoder enc, int charChount)
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.
enc
- encoder to use for estimatescharChount
- number of characters in stringprivate static int estimateIncrementalEncodingSize(java.nio.charset.CharsetEncoder enc, int charCount)
enc
- encoder to use for estimatescharCount
- number of characters remaining