Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
            }
        }