Example #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);
        }
        /**
         * @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);
        }