Пример #1
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));
            }
        }
Пример #2
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()));
            }
        }
Пример #3
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));
                }
            }