public virtual ManagedHttpClientConnection Create(HttpRoute route, ConnectionConfig
                                                          config)
        {
            ConnectionConfig  cconfig              = config != null ? config : ConnectionConfig.Default;
            CharsetDecoder    chardecoder          = null;
            CharsetEncoder    charencoder          = null;
            Encoding          charset              = cconfig.GetCharset();
            CodingErrorAction malformedInputAction = cconfig.GetMalformedInputAction() != null
                                 ? cconfig.GetMalformedInputAction() : CodingErrorAction.Report;

            CodingErrorAction unmappableInputAction = cconfig.GetUnmappableInputAction() != null
                                 ? cconfig.GetUnmappableInputAction() : CodingErrorAction.Report;

            if (charset != null)
            {
                chardecoder = charset.NewDecoder();
                chardecoder.OnMalformedInput(malformedInputAction);
                chardecoder.OnUnmappableCharacter(unmappableInputAction);
                charencoder = charset.NewEncoder();
                charencoder.OnMalformedInput(malformedInputAction);
                charencoder.OnUnmappableCharacter(unmappableInputAction);
            }
            string id = "http-outgoing-" + System.Convert.ToString(Counter.GetAndIncrement());

            return(new LoggingManagedHttpClientConnection(id, log, headerlog, wirelog, cconfig
                                                          .GetBufferSize(), cconfig.GetFragmentSizeHint(), chardecoder, charencoder, cconfig
                                                          .GetMessageConstraints(), null, null, requestWriterFactory, responseParserFactory
                                                          ));
        }
Пример #2
0
        /// <summary>
        /// Converts the provided String to bytes using the
        /// UTF-8 encoding.
        /// </summary>
        /// <remarks>
        /// Converts the provided String to bytes using the
        /// UTF-8 encoding. If <code>replace</code> is true, then
        /// malformed input is replaced with the
        /// substitution character, which is U+FFFD. Otherwise the
        /// method throws a MalformedInputException.
        /// </remarks>
        /// <returns>
        /// ByteBuffer: bytes stores at ByteBuffer.array()
        /// and length is ByteBuffer.limit()
        /// </returns>
        /// <exception cref="CharacterCodingException"/>
        public static ByteBuffer Encode(string @string, bool replace)
        {
            CharsetEncoder encoder = EncoderFactory.Get();

            if (replace)
            {
                encoder.OnMalformedInput(CodingErrorAction.Replace);
                encoder.OnUnmappableCharacter(CodingErrorAction.Replace);
            }
            ByteBuffer bytes = encoder.Encode(CharBuffer.Wrap(@string.ToCharArray()));

            if (replace)
            {
                encoder.OnMalformedInput(CodingErrorAction.Report);
                encoder.OnUnmappableCharacter(CodingErrorAction.Report);
            }
            return(bytes);
        }
Пример #3
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));
            }
        }