Top Description Fields Constructors Methods
org.apache.commons.compress.archivers.zip

pack-priv final Class NioZipEncoding

extends Object
implements ZipEncoding, CharsetAccessor
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.archivers.zip.CharsetAccessor, org.apache.commons.compress.archivers.zip.ZipEncoding
Imports
java.io.IOException, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset, .CharsetDecoder, .CharsetEncoder, .CoderResult, .CodingErrorAction

A ZipEncoding, which uses a Charset to encode names.

The methods of this class are reentrant.

Field Summary

Modifier and TypeField and Description
private final Charset
private static final char[]
private static final char
private static final byte[]
private static final String
private final boolean

Constructor Summary

AccessConstructor and Description
pack-priv
NioZipEncoding(final Charset
The character set to use.
charset
,
final boolean
should invalid characters be replaced, or reported.
useReplacement
)

Constructs an NioZipEncoding using the given charset.

Method Summary

Modifier and TypeMethod and Description
public boolean
canEncode(final String
A file name or ZIP comment.
name
)

Implements org.apache.commons.compress.archivers.zip.ZipEncoding.canEncode.

Check, whether the given string may be losslessly encoded using this encoding.
public String
decode(final byte[]
The byte values to decode.
data
)

Implements org.apache.commons.compress.archivers.zip.ZipEncoding.decode.

public ByteBuffer
encode(final String
A file name or ZIP comment.
name
)

Implements org.apache.commons.compress.archivers.zip.ZipEncoding.encode.

Encode a file name or a comment to a byte array suitable for storing it to a serialized ZIP entry.
private static ByteBuffer
encodeFully(final CharsetEncoder enc, final CharBuffer cb, final ByteBuffer out)

private static CharBuffer
encodeSurrogate(final CharBuffer cb, final char c)

private static int

Returns:

estimated size in bytes.
estimateIncrementalEncodingSize
(final CharsetEncoder
encoder to use for estimates
enc
,
final int
number of characters remaining
charCount
)

Estimate the size needed for remaining characters

private static int

Returns:

estimated size in bytes.
estimateInitialBufferSize
(final CharsetEncoder
encoder to use for estimates
enc
,
final int
number of characters in string
charChount
)

Estimate the initial encoded size (in bytes) for a character buffer.

public Charset
getCharset()

Implements org.apache.commons.compress.archivers.zip.CharsetAccessor.getCharset.

Provides access to the character set associated with an object.
private CharsetDecoder
private CharsetEncoder
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

charsetback to summary
private final Charset charset
HEX_CHARSback to summary
private static final char[] HEX_CHARS
REPLACEMENTback to summary
private static final char REPLACEMENT
REPLACEMENT_BYTESback to summary
private static final byte[] REPLACEMENT_BYTES
REPLACEMENT_STRINGback to summary
private static final String REPLACEMENT_STRING
useReplacementback to summary
private final boolean useReplacement

Constructor Detail

NioZipEncodingback to summary
pack-priv NioZipEncoding(final Charset charset, final boolean useReplacement)

Constructs an NioZipEncoding using the given charset.

Parameters
charset:Charset

The character set to use.

useReplacement:boolean

should invalid characters be replaced, or reported.

Method Detail

canEncodeback to summary
public boolean canEncode(final String name)

Implements org.apache.commons.compress.archivers.zip.ZipEncoding.canEncode.

Doc from org.apache.commons.compress.archivers.zip.ZipEncoding.canEncode.

Check, whether the given string may be losslessly encoded using this encoding.

Parameters
name:String

A file name or ZIP comment.

Returns:boolean

Whether the given name may be encoded without any losses.

Annotations
@Override
See Also
ZipEncoding#canEncode(String)
decodeback to summary
public String decode(final byte[] data) throws IOException

Implements org.apache.commons.compress.archivers.zip.ZipEncoding.decode.

Parameters
data:byte[]

Doc from org.apache.commons.compress.archivers.zip.ZipEncoding.decode.

The byte values to decode.

Returns:String

Doc from org.apache.commons.compress.archivers.zip.ZipEncoding.decode.

The decoded string.

Annotations
@Override
Exceptions
IOException:

Doc from org.apache.commons.compress.archivers.zip.ZipEncoding.decode.

on error

See Also
ZipEncoding#decode(byte[])
encodeback to summary
public ByteBuffer encode(final String name)

Implements org.apache.commons.compress.archivers.zip.ZipEncoding.encode.

Doc from org.apache.commons.compress.archivers.zip.ZipEncoding.encode.

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"
Parameters
name:String

A file name or ZIP comment.

Returns:ByteBuffer

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.

Annotations
@Override
See Also
ZipEncoding#encode(String)
encodeFullyback to summary
private static ByteBuffer encodeFully(final CharsetEncoder enc, final CharBuffer cb, final ByteBuffer out)
encodeSurrogateback to summary
private static CharBuffer encodeSurrogate(final CharBuffer cb, final char c)
estimateIncrementalEncodingSizeback to summary
private static int estimateIncrementalEncodingSize(final CharsetEncoder enc, final int charCount)

Estimate the size needed for remaining characters

Parameters
enc:CharsetEncoder

encoder to use for estimates

charCount:int

number of characters remaining

Returns:int

estimated size in bytes.

estimateInitialBufferSizeback to summary
private static int estimateInitialBufferSize(final CharsetEncoder enc, final 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:CharsetEncoder

encoder to use for estimates

charChount:int

number of characters in string

Returns:int

estimated size in bytes.

getCharsetback to summary
public Charset getCharset()

Implements org.apache.commons.compress.archivers.zip.CharsetAccessor.getCharset.

Doc from org.apache.commons.compress.archivers.zip.CharsetAccessor.getCharset.

Provides access to the character set associated with an object.

This allowsNIOoriented 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.

Returns:Charset

the character set associated with this object

Annotations
@Override
newDecoderback to summary
private CharsetDecoder newDecoder()
newEncoderback to summary
private CharsetEncoder newEncoder()