Пример #1
0
        /*
         * Encodes characters starting at the current position of the given input
         * buffer, and writes the equivalent byte sequence into the given output
         * buffer from its current position.
         * <p />
         * The buffers' position will be changed with the reading and writing
         * operation, but their limits and marks will be kept intact.
         * <p />
         * A <code>CoderResult</code> instance will be returned according to
         * following rules:
         * <ul>
         * <li>A {@link CoderResult#malformedForLength(int) malformed input} result
         * indicates that some malformed input error was encountered, and the
         * erroneous characters start at the input buffer's position and their
         * number can be got by result's {@link CoderResult#length() length}. This
         * kind of result can be returned only if the malformed action is
         * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.</li>
         * <li>{@link CoderResult#UNDERFLOW CoderResult.UNDERFLOW} indicates that
         * as many characters as possible in the input buffer have been encoded. If
         * there is no further input and no characters left in the input buffer then
         * this task is complete. If this is not the case then the client should
         * call this method again supplying some more input characters.</li>
         * <li>{@link CoderResult#OVERFLOW CoderResult.OVERFLOW} indicates that the
         * output buffer has been filled, while there are still some characters
         * remaining in the input buffer. This method should be invoked again with a
         * non-full output buffer.</li>
         * <li>A {@link CoderResult#unmappableForLength(int) unmappable character}
         * result indicates that some unmappable character error was encountered,
         * and the erroneous characters start at the input buffer's position and
         * their number can be got by result's {@link CoderResult#length() length}.
         * This kind of result can be returned only on
         * {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.</li>
         * </ul>
         * <p />
         * The <code>endOfInput</code> parameter indicates if the invoker can
         * provider further input. This parameter is true if and only if the
         * characters in the current input buffer are all inputs for this encoding
         * operation. Note that it is common and won't cause an error if the invoker
         * sets false and then has no more input available, while it may cause an
         * error if the invoker always sets true in several consecutive invocations.
         * This would make the remaining input to be treated as malformed input.
         * input.
         * <p />
         * This method invokes the
         * {@link #encodeLoop(CharBuffer, ByteBuffer) encodeLoop} method to
         * implement the basic encode logic for a specific charset.
         *
         * @param in
         *            the input buffer.
         * @param out
         *            the output buffer.
         * @param endOfInput
         *            true if all the input characters have been provided.
         * @return a <code>CoderResult</code> instance indicating the result.
         * @throws IllegalStateException
         *             if the encoding operation has already started or no more
         *             input is needed in this encoding process.
         * @throws CoderMalfunctionError
         *             If the {@link #encodeLoop(CharBuffer, ByteBuffer) encodeLoop}
         *             method threw an <code>BufferUnderflowException</code> or
         *             <code>BufferUnderflowException</code>.
         */
        public CoderResult encode(java.nio.CharBuffer inJ, java.nio.ByteBuffer outJ, bool endOfInput)
        {
            //If the previous step is encode(CharBuffer), then no more input is needed
            // thus endOfInput should not be false
            if (status == READY && finished && !endOfInput)
            {
                throw new java.lang.IllegalStateException();
            }

            if ((status == FLUSH) || (!endOfInput && status == END))
            {
                throw new java.lang.IllegalStateException();
            }

            CoderResult result;

            while (true)
            {
                try {
                    result = encodeLoop(inJ, outJ);
                } catch (BufferOverflowException e) {
                    throw new CoderMalfunctionError(e);
                } catch (BufferUnderflowException e) {
                    throw new CoderMalfunctionError(e);
                }
                if (result == CoderResult.UNDERFLOW)
                {
                    status = endOfInput ? END : ONGOING;
                    if (endOfInput)
                    {
                        int remaining = inJ.remaining();
                        if (remaining > 0)
                        {
                            result = CoderResult.malformedForLength(remaining);
                        }
                        else
                        {
                            return(result);
                        }
                    }
                    else
                    {
                        return(result);
                    }
                }
                else if (result == CoderResult.OVERFLOW)
                {
                    status = endOfInput ? END : ONGOING;
                    return(result);
                }
                CodingErrorAction action = malformAction;
                if (result.isUnmappable())
                {
                    action = unmapAction;
                }
                // If the action is IGNORE or REPLACE, we should continue
                // encoding.
                if (action == CodingErrorAction.REPLACE)
                {
                    if (outJ.remaining() < replace.Length)
                    {
                        return(CoderResult.OVERFLOW);
                    }
                    outJ.put(replace);
                }
                else
                {
                    if (action != CodingErrorAction.IGNORE)
                    {
                        return(result);
                    }
                }
                inJ.position(inJ.position() + result.length());
            }
        }
Пример #2
0
 /**
  * Updates this {@code MessageDigest} using the given {@code input}.
  *
  * @param input
  *            the {@code ByteBuffer}
  */
 public void update(java.nio.ByteBuffer input)
 {
     engineUpdate(input);
 }
Пример #3
0
        protected override java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer inJ, java.nio.ByteBuffer outJ)
        {
            Encoding enc = this.cs.getEncoding();

            char[] input = new char[inJ.capacityJ - inJ.positionJ];
            inJ.get(input);
            byte[] output = new byte[input.Length * fiveValue];
            int    size   = enc.GetEncoder().GetBytes(input, 0, input.Length, output, 0, true);

            outJ.put(output, 0, size);
            outJ = java.nio.ByteBuffer.wrap((byte[])outJ.array(), 0, size);
            return(java.nio.charset.CoderResult.UNDERFLOW);
        }
Пример #4
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();
        }
Пример #5
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;
        }
 /**
  * Encodes characters into bytes. This method is called by
  * {@link #encode(CharBuffer, ByteBuffer, boolean) encode}.
  * <p />
  * This method will implement the essential encoding operation, and it won't
  * stop encoding until either all the input characters are read, the output
  * buffer is filled, or some exception is encountered. Then it will
  * return a <code>CoderResult</code> object indicating the result of the
  * current encoding operation. The rule to construct the
  * <code>CoderResult</code> is the same as for
  * {@link #encode(CharBuffer, ByteBuffer, boolean) encode}. When an
  * exception is encountered in the encoding operation, most implementations
  * of this method will return a relevant result object to the
  * {@link #encode(CharBuffer, ByteBuffer, boolean) encode} method, and some
  * performance optimized implementation may handle the exception and
  * implement the error action itself.
  * <p />
  * The buffers are scanned from their current positions, and their positions
  * will be modified accordingly, while their marks and limits will be
  * intact. At most {@link CharBuffer#remaining() in.remaining()} characters
  * will be read, and {@link ByteBuffer#remaining() out.remaining()} bytes
  * will be written.
  * <p />
  * Note that some implementations may pre-scan the input buffer and return
  * <code>CoderResult.UNDERFLOW</code> until it receives sufficient input.
  * <p />
  * @param in
  *            the input buffer.
  * @param out
  *            the output buffer.
  * @return a <code>CoderResult</code> instance indicating the result.
  */
 protected abstract CoderResult encodeLoop(java.nio.CharBuffer inJ, java.nio.ByteBuffer outJ);