Beispiel #1
0
        /*
         * Constructs a new {@code InputStreamReader} on the {@link InputStream}
         * {@code in}. This constructor sets the character converter to the encoding
         * specified in the "file.encoding" property and falls back to ISO 8859_1
         * (ISO-Latin-1) if the property doesn't exist.
         *
         * @param in
         *            the input stream from which to read characters.
         */
        public InputStreamReader(InputStream inJ) : base(inJ)
        {
            this.inJ = inJ;

            /*String encoding = AccessController
             *      .doPrivileged(new PriviAction<String>(
             *              "file.encoding", "ISO8859_1")); //$NON-NLS-1$//$NON-NLS-2$*/
            String encoding = java.lang.SystemJ.getProperty("file.encoding", "ISO8859_1");

            decoder = java.nio.charset.Charset.forName(encoding).newDecoder().onMalformedInput(
                java.nio.charset.CodingErrorAction.REPLACE).onUnmappableCharacter(
                java.nio.charset.CodingErrorAction.REPLACE);
            bytes.limit(0);
        }
Beispiel #2
0
        /**
         * Grow a byte buffer, so it has a minimal capacity or at least
         * the double capacity of the original buffer
         *
         * @param b The original buffer.
         * @param newCapacity The minimal requested new capacity.
         * @return A byte buffer <code>r</code> with
         *         <code>r.capacity() = max(b.capacity()*2,newCapacity)</code> and
         *         all the data contained in <code>b</code> copied to the beginning
         *         of <code>r</code>.
         *
         */
        internal static java.nio.ByteBuffer growBuffer(java.nio.ByteBuffer b, int newCapacity)
        {
            b.limit(b.position());
            b.rewind();

            int c2 = b.capacity() * 2;

            java.nio.ByteBuffer on = java.nio.ByteBuffer.allocate(c2 < newCapacity ? newCapacity : c2);

            on.put(b);
            return(on);
        }
        /**
         * Writes the &quot;End of central dir record&quot;.
         * @throws IOException on error
         */
        protected void writeCentralDirectoryEnd() //throws IOException
        {
            writeOut(EOCD_SIG);

            // disk numbers
            writeOut(ZERO);
            writeOut(ZERO);

            // number of entries
            byte[] num = ZipShort.getBytes(entries.size());
            writeOut(num);
            writeOut(num);

            // length and location of CD
            writeOut(ZipLong.getBytes(cdLength));
            writeOut(ZipLong.getBytes(cdOffset));

            // ZIP file comment
            java.nio.ByteBuffer data = this.zipEncoding.encode(comment);
            writeOut(ZipShort.getBytes(data.limit()));
            writeOut((byte[])data.array(), data.arrayOffset(), data.limit());
        }
Beispiel #4
0
 /**
  * Updates this {@code MessageDigestSpi} using the given {@code input}.
  *
  * @param input
  *            the {@code ByteBuffer}.
  */
 protected internal virtual void engineUpdate(java.nio.ByteBuffer input)
 {
     if (!input.hasRemaining())
     {
         return;
     }
     byte[] tmp;
     if (input.hasArray())
     {
         tmp = (byte[])input.array();
         int offset   = input.arrayOffset();
         int position = input.position();
         int limit    = input.limit();
         engineUpdate(tmp, offset + position, limit - position);
         input.position(limit);
     }
     else
     {
         tmp = new byte[input.limit() - input.position()];
         input.get(tmp);
         engineUpdate(tmp, 0, tmp.Length);
     }
 }
Beispiel #5
0
        /**
         * @see
         * org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String)
         */
        public java.nio.ByteBuffer encode(String name)
        {
            java.nio.charset.CharsetEncoder enc = this.charset.newEncoder();

            enc.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT);
            enc.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT);

            java.nio.CharBuffer cb   = java.nio.CharBuffer.wrap(name);
            java.nio.ByteBuffer outJ = java.nio.ByteBuffer.allocate(name.length()
                                                                    + (name.length() + 1) / 2);

            while (cb.remaining() > 0)
            {
                java.nio.charset.CoderResult res = enc.encode(cb, outJ, true);

                if (res.isUnmappable() || res.isMalformed())
                {
                    // write the unmappable characters in utf-16
                    // pseudo-URL encoding style to ByteBuffer.
                    if (res.length() * 6 > outJ.remaining())
                    {
                        outJ = ZipEncodingHelper.growBuffer(outJ, outJ.position()
                                                            + res.length() * 6);
                    }

                    for (int i = 0; i < res.length(); ++i)
                    {
                        ZipEncodingHelper.appendSurrogate(outJ, cb.get());
                    }
                }
                else if (res.isOverflow())
                {
                    outJ = ZipEncodingHelper.growBuffer(outJ, 0);
                }
                else if (res.isUnderflow())
                {
                    enc.flush(outJ);
                    break;
                }
            }

            outJ.limit(outJ.position());
            outJ.rewind();
            return(outJ);
        }
        /**
         * @see
         * org.apache.commons.compress.archivers.zip.ZipEncoding#encode(java.lang.String)
         */
        public java.nio.ByteBuffer encode(String name)
        {
            java.nio.ByteBuffer outJ = java.nio.ByteBuffer.allocate(name.length()
                                                                    + 6 + (name.length() + 1) / 2);

            for (int i = 0; i < name.length(); ++i)
            {
                char c = name.charAt(i);

                if (outJ.remaining() < 6)
                {
                    outJ = ZipEncodingHelper.growBuffer(outJ, outJ.position() + 6);
                }

                if (!this.pushEncodedChar(outJ, c))
                {
                    ZipEncodingHelper.appendSurrogate(outJ, c);
                }
            }

            outJ.limit(outJ.position());
            outJ.rewind();
            return(outJ);
        }
        /**
         * Writes the central file header entry.
         * @param ze the entry to write
         * @throws IOException on error
         */
        protected void writeCentralFileHeader(ZipArchiveEntry ze) //throws IOException
        {
            writeOut(CFH_SIG);
            written += WORD;

            // version made by
            // CheckStyle:MagicNumber OFF
            writeOut(ZipShort.getBytes((ze.getPlatform() << 8) | 20));
            written += SHORT;

            int  zipMethod = ze.getMethod();
            bool encodable = zipEncoding.canEncode(ze.getName());

            writeVersionNeededToExtractAndGeneralPurposeBits(zipMethod,
                                                             !encodable &&
                                                             fallbackToUTF8);
            written += WORD;

            // compression method
            writeOut(ZipShort.getBytes(zipMethod));
            written += SHORT;

            // last mod. time and date
            writeOut(ZipUtil.toDosTime(ze.getTime()));
            written += WORD;

            // CRC
            // compressed length
            // uncompressed length
            writeOut(ZipLong.getBytes(ze.getCrc()));
            writeOut(ZipLong.getBytes(ze.getCompressedSize()));
            writeOut(ZipLong.getBytes(ze.getSize()));
            // CheckStyle:MagicNumber OFF
            written += 12;
            // CheckStyle:MagicNumber ON

            // file name length
            ZipEncoding entryEncoding;

            if (!encodable && fallbackToUTF8)
            {
                entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING;
            }
            else
            {
                entryEncoding = zipEncoding;
            }

            java.nio.ByteBuffer name = entryEncoding.encode(ze.getName());

            writeOut(ZipShort.getBytes(name.limit()));
            written += SHORT;

            // extra field length
            byte[] extra = ze.getCentralDirectoryExtra();
            writeOut(ZipShort.getBytes(extra.Length));
            written += SHORT;

            // file comment length
            String comm = ze.getComment();

            if (comm == null)
            {
                comm = "";
            }

            java.nio.ByteBuffer commentB = entryEncoding.encode(comm);

            writeOut(ZipShort.getBytes(commentB.limit()));
            written += SHORT;

            // disk number start
            writeOut(ZERO);
            written += SHORT;

            // internal file attributes
            writeOut(ZipShort.getBytes(ze.getInternalAttributes()));
            written += SHORT;

            // external file attributes
            writeOut(ZipLong.getBytes(ze.getExternalAttributes()));
            written += WORD;

            // relative offset of LFH
            writeOut((byte[])offsets.get(ze));
            written += WORD;

            // file name
            writeOut((byte[])name.array(), name.arrayOffset(), name.limit());
            written += name.limit();

            // extra field
            writeOut(extra);
            written += extra.Length;

            // file comment
            writeOut((byte[])commentB.array(), commentB.arrayOffset(), commentB.limit());
            written += commentB.limit();
        }
        /**
         * Writes the local file header entry
         * @param ze the entry to write
         * @throws IOException on error
         */
        protected void writeLocalFileHeader(ZipArchiveEntry ze) //throws IOException
        {
            bool encodable = zipEncoding.canEncode(ze.getName());

            ZipEncoding entryEncoding;

            if (!encodable && fallbackToUTF8)
            {
                entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING;
            }
            else
            {
                entryEncoding = zipEncoding;
            }

            java.nio.ByteBuffer name = entryEncoding.encode(ze.getName());

            if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER)
            {
                if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS ||
                    !encodable)
                {
                    ze.addExtraField(new UnicodePathExtraField(ze.getName(),
                                                               (byte[])name.array(),
                                                               name.arrayOffset(),
                                                               name.limit()));
                }

                String comm = ze.getComment();
                if (comm != null && !"".equals(comm))
                {
                    bool commentEncodable = this.zipEncoding.canEncode(comm);

                    if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS ||
                        !commentEncodable)
                    {
                        java.nio.ByteBuffer commentB = entryEncoding.encode(comm);
                        ze.addExtraField(new UnicodeCommentExtraField(comm,
                                                                      (byte[])commentB.array(),
                                                                      commentB.arrayOffset(),
                                                                      commentB.limit())
                                         );
                    }
                }
            }

            offsets.put(ze, ZipLong.getBytes(written));

            writeOut(LFH_SIG);
            written += WORD;

            //store method in local variable to prevent multiple method calls
            int zipMethod = ze.getMethod();

            writeVersionNeededToExtractAndGeneralPurposeBits(zipMethod,
                                                             !encodable &&
                                                             fallbackToUTF8);
            written += WORD;

            // compression method
            writeOut(ZipShort.getBytes(zipMethod));
            written += SHORT;

            // last mod. time and date
            writeOut(ZipUtil.toDosTime(ze.getTime()));
            written += WORD;

            // CRC
            // compressed length
            // uncompressed length
            localDataStart = written;
            if (zipMethod == DEFLATED || raf != null)
            {
                writeOut(LZERO);
                writeOut(LZERO);
                writeOut(LZERO);
            }
            else
            {
                writeOut(ZipLong.getBytes(ze.getCrc()));
                writeOut(ZipLong.getBytes(ze.getSize()));
                writeOut(ZipLong.getBytes(ze.getSize()));
            }
            // CheckStyle:MagicNumber OFF
            written += 12;
            // CheckStyle:MagicNumber ON

            // file name length
            writeOut(ZipShort.getBytes(name.limit()));
            written += SHORT;

            // extra field length
            byte[] extra = ze.getLocalFileDataExtra();
            writeOut(ZipShort.getBytes(extra.Length));
            written += SHORT;

            // file name
            writeOut((byte[])name.array(), name.arrayOffset(), name.limit());
            written += name.limit();

            // extra field
            writeOut(extra);
            written += extra.Length;

            dataStart = written;
        }