/// <exception cref="System.IO.IOException"></exception>
 private void HandleEncodingResult(CoderResult result)
 {
     if (result.IsError())
     {
         result.ThrowException();
     }
     this.bbuf.Flip();
     while (this.bbuf.HasRemaining())
     {
         Write(this.bbuf.Get());
     }
     this.bbuf.Compact();
 }
Example #2
0
        internal static sbyte[] Encode(Charset cs, char[] ca, int off, int len)
        {
            CharsetEncoder ce = cs.NewEncoder();
            int            en = Scale(len, ce.MaxBytesPerChar());

            sbyte[] ba = new sbyte[en];
            if (len == 0)
            {
                return(ba);
            }
            bool isTrusted = false;

            if (System.SecurityManager != null)
            {
                if (!(isTrusted = (cs.GetType().ClassLoader0 == null)))
                {
                    ca  = Arrays.CopyOfRange(ca, off, off + len);
                    off = 0;
                }
            }
            ce.OnMalformedInput(CodingErrorAction.REPLACE).OnUnmappableCharacter(CodingErrorAction.REPLACE).Reset();
            if (ce is ArrayEncoder)
            {
                int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
                return(SafeTrim(ba, blen, cs, isTrusted));
            }
            else
            {
                ByteBuffer bb = ByteBuffer.Wrap(ba);
                CharBuffer cb = CharBuffer.Wrap(ca, off, len);
                try
                {
                    CoderResult cr = ce.Encode(cb, bb, true);
                    if (!cr.Underflow)
                    {
                        cr.ThrowException();
                    }
                    cr = ce.Flush(bb);
                    if (!cr.Underflow)
                    {
                        cr.ThrowException();
                    }
                }
                catch (CharacterCodingException x)
                {
                    throw new Error(x);
                }
                return(SafeTrim(ba, bb.Position(), cs, isTrusted));
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        private int HandleDecodingResult(CoderResult result, CharArrayBuffer charbuffer,
                                         ByteBuffer bbuf)
        {
            if (result.IsError())
            {
                result.ThrowException();
            }
            this.cbuf.Flip();
            int len = this.cbuf.Remaining();

            while (this.cbuf.HasRemaining())
            {
                charbuffer.Append(this.cbuf.Get());
            }
            this.cbuf.Compact();
            return(len);
        }
        private bool isMalformed(CoderResult coder, ByteBuffer in)
        {
            if (coder.isMalformed() || coder.isUnmappable())
            {
                int errorPosition = in.position();

                if (errorPosition + 1 < in.limit() &&
                    in.get(errorPosition) == '\u00a3' &&
                    in.get(errorPosition + 1) == '\u00e1')
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
Example #5
0
        internal sbyte[] GetBytes(String s)
        {
            CharsetEncoder ce = Encoder().Reset();

            char[] ca  = s.ToCharArray();
            int    len = (int)(ca.Length * ce.MaxBytesPerChar());

            sbyte[] ba = new sbyte[len];
            if (len == 0)
            {
                return(ba);
            }
            // UTF-8 only for now. Other ArrayDeocder only handles
            // CodingErrorAction.REPLACE mode.
            if (IsUTF8 && ce is ArrayEncoder)
            {
                int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.Length, ba);
                if (blen == -1)                 // malformed
                {
                    throw new IllegalArgumentException("MALFORMED");
                }
                return(Arrays.CopyOf(ba, blen));
            }
            ByteBuffer  bb = ByteBuffer.Wrap(ba);
            CharBuffer  cb = CharBuffer.Wrap(ca);
            CoderResult cr = ce.Encode(cb, bb, true);

            if (!cr.Underflow)
            {
                throw new IllegalArgumentException(cr.ToString());
            }
            cr = ce.Flush(bb);
            if (!cr.Underflow)
            {
                throw new IllegalArgumentException(cr.ToString());
            }
            if (bb.Position() == ba.Length)             // defensive copy?
            {
                return(ba);
            }
            else
            {
                return(Arrays.CopyOf(ba, bb.Position()));
            }
        }
Example #6
0
            internal virtual sbyte[] Encode(char[] ca, int off, int len)
            {
                int en = Scale(len, Ce.MaxBytesPerChar());

                sbyte[] ba = new sbyte[en];
                if (len == 0)
                {
                    return(ba);
                }
                if (Ce is ArrayEncoder)
                {
                    int blen = ((ArrayEncoder)Ce).encode(ca, off, len, ba);
                    return(SafeTrim(ba, blen, Cs, IsTrusted));
                }
                else
                {
                    Ce.Reset();
                    ByteBuffer bb = ByteBuffer.Wrap(ba);
                    CharBuffer cb = CharBuffer.Wrap(ca, off, len);
                    try
                    {
                        CoderResult cr = Ce.Encode(cb, bb, true);
                        if (!cr.Underflow)
                        {
                            cr.ThrowException();
                        }
                        cr = Ce.Flush(bb);
                        if (!cr.Underflow)
                        {
                            cr.ThrowException();
                        }
                    }
                    catch (CharacterCodingException x)
                    {
                        // Substitution is always enabled,
                        // so this shouldn't happen
                        throw new Error(x);
                    }
                    return(SafeTrim(ba, bb.Position(), Cs, IsTrusted));
                }
            }
Example #7
0
            internal virtual char[] Decode(sbyte[] ba, int off, int len)
            {
                int en = Scale(len, Cd.MaxCharsPerByte());

                char[] ca = new char[en];
                if (len == 0)
                {
                    return(ca);
                }
                if (Cd is ArrayDecoder)
                {
                    int clen = ((ArrayDecoder)Cd).decode(ba, off, len, ca);
                    return(SafeTrim(ca, clen, Cs, IsTrusted));
                }
                else
                {
                    Cd.Reset();
                    ByteBuffer bb = ByteBuffer.Wrap(ba, off, len);
                    CharBuffer cb = CharBuffer.Wrap(ca);
                    try
                    {
                        CoderResult cr = Cd.Decode(bb, cb, true);
                        if (!cr.Underflow)
                        {
                            cr.ThrowException();
                        }
                        cr = Cd.Flush(cb);
                        if (!cr.Underflow)
                        {
                            cr.ThrowException();
                        }
                    }
                    catch (CharacterCodingException x)
                    {
                        // Substitution is always enabled,
                        // so this shouldn't happen
                        throw new Error(x);
                    }
                    return(SafeTrim(ca, cb.Position(), Cs, IsTrusted));
                }
            }
        /// <exception cref="System.IO.IOException"></exception>
        private void WriteEncoded(CharBuffer cbuf)
        {
            if (!cbuf.HasRemaining())
            {
                return;
            }
            if (this.bbuf == null)
            {
                this.bbuf = ByteBuffer.Allocate(1024);
            }
            this.encoder.Reset();
            while (cbuf.HasRemaining())
            {
                CoderResult result = this.encoder.Encode(cbuf, this.bbuf, true);
                HandleEncodingResult(result);
            }
            CoderResult result_1 = this.encoder.Flush(this.bbuf);

            HandleEncodingResult(result_1);
            this.bbuf.Clear();
        }
        public override bool isDecodable(Env env, StringValue str)
        {
            if (str.isUnicode())
            {
                return(true);
            }

            ByteBuffer in = ByteBuffer.wrap(str.toBytes());
            CharBuffer @out = CharBuffer.allocate(512);

            while (in.hasRemaining())
            {
                CoderResult coder = _decoder.decode(in, @out, false);
                if (isMalformed(coder, in))
                {
                    return(false);
                }

                @out.clear();
            }

            CoderResult coder = _decoder.decode(in, @out, true);

            if (isMalformed(coder, in))
            {
                return(false);
            }

            @out.clear();

            coder = _decoder.flush(out);
            if (isMalformed(coder, in))
            {
                return(false);
            }

            return(true);
        }
        public override StringValue encode(StringValue sb, CharSequence str,
                                           int start, int end)
        {
            CharBuffer in = CharBuffer.wrap(str, start, end);

            TempBuffer tempBuf = TempBuffer.allocate();

            try {
                ByteBuffer @out = ByteBuffer.wrap(tempBuf.getBuffer());

                while (in.hasRemaining())
                {
                    CoderResult coder = _encoder.encode(in, @out, false);

                    if (!fill(sb, in, @out, coder))
                    {
                        return(sb);
                    }

                    @out.clear();
                }

                CoderResult coder = _encoder.encode(in, @out, true);
                if (!fill(sb, in, @out, coder))
                {
                    return(sb);
                }

                @out.clear();

                coder = _encoder.flush(out);
                fill(sb, in, @out, coder);

                return(sb);
            } finally {
                TempBuffer.free(tempBuf);
            }
        }
Example #11
0
        internal String ToString(sbyte[] ba, int length)
        {
            CharsetDecoder cd  = Decoder().Reset();
            int            len = (int)(length * cd.MaxCharsPerByte());

            char[] ca = new char[len];
            if (len == 0)
            {
                return(new String(ca));
            }
            // UTF-8 only for now. Other ArrayDeocder only handles
            // CodingErrorAction.REPLACE mode. ZipCoder uses
            // REPORT mode.
            if (IsUTF8 && cd is ArrayDecoder)
            {
                int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
                if (clen == -1)                 // malformed
                {
                    throw new IllegalArgumentException("MALFORMED");
                }
                return(new String(ca, 0, clen));
            }
            ByteBuffer  bb = ByteBuffer.Wrap(ba, 0, length);
            CharBuffer  cb = CharBuffer.Wrap(ca);
            CoderResult cr = cd.Decode(bb, cb, true);

            if (!cr.Underflow)
            {
                throw new IllegalArgumentException(cr.ToString());
            }
            cr = cd.Flush(cb);
            if (!cr.Underflow)
            {
                throw new IllegalArgumentException(cr.ToString());
            }
            return(new String(ca, 0, cb.Position()));
        }
        public override void decodeUnicode(StringValue str, UnicodeBuilderValue sb)
        {
            ByteBuffer in = ByteBuffer.wrap(str.toBytes());

            TempCharBuffer tempBuf = TempCharBuffer.allocate();

            try  {
                CharBuffer @out = CharBuffer.wrap(tempBuf.getBuffer());

                while (in.hasRemaining())
                {
                    CoderResult coder = _decoder.decode(in, @out, false);
                    if (!fill(sb, in, @out, coder))
                    {
                        return;
                    }

                    @out.clear();
                }

                CoderResult coder = _decoder.decode(in, @out, true);
                if (!fill(sb, in, @out, coder))
                {
                    return;
                }

                @out.clear();

                coder = _decoder.flush(out);
                fill(sb, in, @out, coder);

                return;
            }
            finally {
                TempCharBuffer.free(tempBuf);
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        private int AppendDecoded(CharArrayBuffer charbuffer, ByteBuffer bbuf)
        {
            if (!bbuf.HasRemaining())
            {
                return(0);
            }
            if (this.cbuf == null)
            {
                this.cbuf = CharBuffer.Allocate(1024);
            }
            this.decoder.Reset();
            int len = 0;

            while (bbuf.HasRemaining())
            {
                CoderResult result = this.decoder.Decode(bbuf, this.cbuf, true);
                len += HandleDecodingResult(result, charbuffer, bbuf);
            }
            CoderResult result_1 = this.decoder.Flush(this.cbuf);

            len += HandleDecodingResult(result_1, charbuffer, bbuf);
            this.cbuf.Clear();
            return(len);
        }
Example #14
0
        internal static char[] Decode(Charset cs, sbyte[] ba, int off, int len)
        {
            // (1)We never cache the "external" cs, the only benefit of creating
            // an additional StringDe/Encoder object to wrap it is to share the
            // de/encode() method. These SD/E objects are short-lifed, the young-gen
            // gc should be able to take care of them well. But the best approash
            // is still not to generate them if not really necessary.
            // (2)The defensive copy of the input byte/char[] has a big performance
            // impact, as well as the outgoing result byte/char[]. Need to do the
            // optimization check of (sm==null && classLoader0==null) for both.
            // (3)getClass().getClassLoader0() is expensive
            // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
            // is only chcked (and then isTrusted gets set) when (SM==null). It is
            // possible that the SM==null for now but then SM is NOT null later
            // when safeTrim() is invoked...the "safe" way to do is to redundant
            // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
            // but it then can be argued that the SM is null when the opertaion
            // is started...
            CharsetDecoder cd = cs.NewDecoder();
            int            en = Scale(len, cd.MaxCharsPerByte());

            char[] ca = new char[en];
            if (len == 0)
            {
                return(ca);
            }
            bool isTrusted = false;

            if (System.SecurityManager != null)
            {
                if (!(isTrusted = (cs.GetType().ClassLoader0 == null)))
                {
                    ba  = Arrays.CopyOfRange(ba, off, off + len);
                    off = 0;
                }
            }
            cd.OnMalformedInput(CodingErrorAction.REPLACE).OnUnmappableCharacter(CodingErrorAction.REPLACE).Reset();
            if (cd is ArrayDecoder)
            {
                int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
                return(SafeTrim(ca, clen, cs, isTrusted));
            }
            else
            {
                ByteBuffer bb = ByteBuffer.Wrap(ba, off, len);
                CharBuffer cb = CharBuffer.Wrap(ca);
                try
                {
                    CoderResult cr = cd.Decode(bb, cb, true);
                    if (!cr.Underflow)
                    {
                        cr.ThrowException();
                    }
                    cr = cd.Flush(cb);
                    if (!cr.Underflow)
                    {
                        cr.ThrowException();
                    }
                }
                catch (CharacterCodingException x)
                {
                    // Substitution is always enabled,
                    // so this shouldn't happen
                    throw new Error(x);
                }
                return(SafeTrim(ca, cb.Position(), cs, isTrusted));
            }
        }