Exemplo n.º 1
0
        /**
         * @see
         * org.apache.commons.compress.archivers.zip.ZipEncoding#canEncode(java.lang.String)
         */
        public bool canEncode(String name)
        {
            java.nio.charset.CharsetEncoder enc = this.charset.newEncoder();
            enc.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT);
            enc.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT);

            return(enc.canEncode(name));
        }
Exemplo n.º 2
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);
        }