Пример #1
0
        /**
         * Writes the "End of central dir record".
         * @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());
        }
Пример #2
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);
     }
 }
Пример #3
0
        /**
         * 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();
        }
Пример #4
0
        /**
         * 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;
        }
Пример #5
0
        /*
         * Reads at most {@code length} characters from this reader and stores them
         * at position {@code offset} in the character array {@code buf}. Returns
         * the number of characters actually read or -1 if the end of the reader has
         * been reached. The bytes are either obtained from converting bytes in this
         * reader's buffer or by first filling the buffer from the source
         * InputStream and then reading from the buffer.
         *
         * @param buf
         *            the array to store the characters read.
         * @param offset
         *            the initial position in {@code buf} to store the characters
         *            read from this reader.
         * @param length
         *            the maximum number of characters to read.
         * @return the number of characters read or -1 if the end of the reader has
         *         been reached.
         * @throws IndexOutOfBoundsException
         *             if {@code offset &lt; 0} or {@code length &lt; 0}, or if
         *             {@code offset + length} is greater than the length of
         *             {@code buf}.
         * @throws IOException
         *             if this reader is closed or some other I/O error occurs.
         */
        public override int read(char[] buf, int offset, int length)
        {// throws IOException {
            lock (lockJ)
            {
                if (!isOpen())
                {
                    // luni.BA=InputStreamReader is closed.
                    throw new IOException("InputStreamReader is closed."); //$NON-NLS-1$
                }
                if (offset < 0 || offset > buf.Length - length || length < 0)
                {
                    throw new java.lang.IndexOutOfBoundsException();
                }
                if (length == 0)
                {
                    return(0);
                }

                java.nio.CharBuffer          outJ   = java.nio.CharBuffer.wrap(buf, offset, length);
                java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW;

                // bytes.remaining() indicates number of bytes in buffer
                // when 1-st time entered, it'll be equal to zero
                bool needInput = !bytes.hasRemaining();

                while (outJ.hasRemaining())
                {
                    // fill the buffer if needed
                    if (needInput)
                    {
                        try
                        {
                            if ((inJ.available() == 0) &&
                                (outJ.position() > offset))
                            {
                                // we could return the result without blocking read
                                break;
                            }
                        }
                        catch (IOException e)
                        {
                            // available didn't work so just try the read
                        }

                        int to_read = bytes.capacity() - bytes.limit();
                        int off     = bytes.arrayOffset() + bytes.limit();
                        int was_red = inJ.read((byte[])bytes.array(), off, to_read);

                        if (was_red == -1)
                        {
                            endOfInput = true;
                            break;
                        }
                        else if (was_red == 0)
                        {
                            break;
                        }
                        bytes.limit(bytes.limit() + was_red);
                        needInput = false;
                    }

                    // decode bytes
                    result = decoder.decode(bytes, outJ, false);

                    if (result.isUnderflow())
                    {
                        // compact the buffer if no space left
                        if (bytes.limit() == bytes.capacity())
                        {
                            bytes.compact();
                            bytes.limit(bytes.position());
                            bytes.position(0);
                        }
                        needInput = true;
                    }
                    else
                    {
                        break;
                    }
                }

                if (result == java.nio.charset.CoderResult.UNDERFLOW && endOfInput)
                {
                    result = decoder.decode(bytes, outJ, true);
                    decoder.flush(outJ);
                    decoder.reset();
                }
                if (result.isMalformed())
                {
                    throw new java.nio.charset.MalformedInputException(result.length());
                }
                else if (result.isUnmappable())
                {
                    throw new java.nio.charset.UnmappableCharacterException(result.length());
                }

                return(outJ.position() - offset == 0 ? -1 : outJ.position() - offset);
            }
        }