Esempio n. 1
0
        /// <summary>Read the contents of an entity and return it as a byte array.</summary>
        /// <remarks>Read the contents of an entity and return it as a byte array.</remarks>
        /// <param name="entity">the entity to read from=</param>
        /// <returns>
        /// byte array containing the entity content. May be null if
        /// <see cref="Org.Apache.Http.HttpEntity.GetContent()">Org.Apache.Http.HttpEntity.GetContent()
        ///     </see>
        /// is null.
        /// </returns>
        /// <exception cref="System.IO.IOException">if an error occurs reading the input stream
        ///     </exception>
        /// <exception cref="System.ArgumentException">if entity is null or if content length &gt; Integer.MAX_VALUE
        ///     </exception>
        public static byte[] ToByteArray(HttpEntity entity)
        {
            Args.NotNull(entity, "Entity");
            InputStream instream = entity.GetContent();

            if (instream == null)
            {
                return(null);
            }
            try
            {
                Args.Check(entity.GetContentLength() <= int.MaxValue, "HTTP entity too large to be buffered in memory"
                           );
                int i = (int)entity.GetContentLength();
                if (i < 0)
                {
                    i = 4096;
                }
                ByteArrayBuffer buffer = new ByteArrayBuffer(i);
                byte[]          tmp    = new byte[4096];
                int             l;
                while ((l = instream.Read(tmp)) != -1)
                {
                    buffer.Append(tmp, 0, l);
                }
                return(buffer.ToByteArray());
            }
            finally
            {
                instream.Close();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get the entity content as a String, using the provided default character set
        /// if none is found in the entity.
        /// </summary>
        /// <remarks>
        /// Get the entity content as a String, using the provided default character set
        /// if none is found in the entity.
        /// If defaultCharset is null, the default "ISO-8859-1" is used.
        /// </remarks>
        /// <param name="entity">must not be null</param>
        /// <param name="defaultCharset">character set to be applied if none found in the entity
        ///     </param>
        /// <returns>
        /// the entity content as a String. May be null if
        /// <see cref="Org.Apache.Http.HttpEntity.GetContent()">Org.Apache.Http.HttpEntity.GetContent()
        ///     </see>
        /// is null.
        /// </returns>
        /// <exception cref="Org.Apache.Http.ParseException">if header elements cannot be parsed
        ///     </exception>
        /// <exception cref="System.ArgumentException">if entity is null or if content length &gt; Integer.MAX_VALUE
        ///     </exception>
        /// <exception cref="System.IO.IOException">if an error occurs reading the input stream
        ///     </exception>
        /// <exception cref="Sharpen.UnsupportedCharsetException">
        /// Thrown when the named charset is not available in
        /// this instance of the Java virtual machine
        /// </exception>
        public static string ToString(HttpEntity entity, Encoding defaultCharset)
        {
            Args.NotNull(entity, "Entity");
            InputStream instream = entity.GetContent();

            if (instream == null)
            {
                return(null);
            }
            try
            {
                Args.Check(entity.GetContentLength() <= int.MaxValue, "HTTP entity too large to be buffered in memory"
                           );
                int i = (int)entity.GetContentLength();
                if (i < 0)
                {
                    i = 4096;
                }
                Encoding charset = null;
                try
                {
                    ContentType contentType = ContentType.Get(entity);
                    if (contentType != null)
                    {
                        charset = contentType.GetCharset();
                    }
                }
                catch (UnsupportedCharsetException ex)
                {
                    throw new UnsupportedEncodingException(ex.Message);
                }
                if (charset == null)
                {
                    charset = defaultCharset;
                }
                if (charset == null)
                {
                    charset = HTTP.DefContentCharset;
                }
                StreamReader    reader = new InputStreamReader(instream, charset);
                CharArrayBuffer buffer = new CharArrayBuffer(i);
                char[]          tmp    = new char[1024];
                int             l;
                while ((l = reader.Read(tmp)) != -1)
                {
                    buffer.Append(tmp, 0, l);
                }
                return(buffer.ToString());
            }
            finally
            {
                instream.Close();
            }
        }