Class NioZipEncoding

java.lang.Object
org.apache.commons.compress.archivers.zip.NioZipEncoding
All Implemented Interfaces:
CharsetAccessor, ZipEncoding

class NioZipEncoding extends Object implements ZipEncoding, CharsetAccessor
A ZipEncoding, which uses a java.nio Charset to encode names.

The methods of this class are reentrant.

  • Field Details

    • charset

      private final Charset charset
    • useReplacement

      private final boolean useReplacement
    • REPLACEMENT

      private static final char REPLACEMENT
      See Also:
    • REPLACEMENT_BYTES

      private static final byte[] REPLACEMENT_BYTES
    • REPLACEMENT_STRING

      private static final String REPLACEMENT_STRING
    • HEX_CHARS

      private static final char[] HEX_CHARS
  • Constructor Details

    • NioZipEncoding

      NioZipEncoding(Charset charset, boolean useReplacement)
      Construct an NioZipEncoding using the given charset.
      Parameters:
      charset - The character set to use.
      useReplacement - should invalid characters be replaced, or reported.
  • Method Details

    • getCharset

      public 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 interface CharsetAccessor
      Returns:
      the character set associated with this object
    • canEncode

      public boolean canEncode(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 file name or ZIP comment.
      Returns:
      Whether the given name may be encoded with out any losses.
      See Also:
    • encode

      public ByteBuffer encode(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 interface ZipEncoding
      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:
    • decode

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

      private static ByteBuffer encodeFully(CharsetEncoder enc, CharBuffer cb, ByteBuffer out)
    • encodeSurrogate

      private static CharBuffer encodeSurrogate(CharBuffer cb, char c)
    • newEncoder

      private CharsetEncoder newEncoder()
    • newDecoder

      private CharsetDecoder newDecoder()
    • estimateInitialBufferSize

      private static int estimateInitialBufferSize(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 estimates
      charChount - number of characters in string
      Returns:
      estimated size in bytes.
    • estimateIncrementalEncodingSize

      private static int estimateIncrementalEncodingSize(CharsetEncoder enc, int charCount)
      Estimate the size needed for remaining characters
      Parameters:
      enc - encoder to use for estimates
      charCount - number of characters remaining
      Returns:
      estimated size in bytes.