public static String ToString(InputStream stream, String encoding) { StreamReader sr = new StreamReader(stream); return sr.ReadToEnd(); }
public static void CloseQuietly(InputStream stream) { stream.Close(); }
/// <summary> /// <p>Sniffs encoding settings from the specified XML content and/or the corresponding HTTP headers using /// a custom algorithm.</p> /// <p>Note that if an encoding is found but it is not supported on the current platform, this method returns /// <tt>null</tt>, as if no encoding had been found.</p> /// </summary> /// <param name="headers">the HTTP response headers sent back with the XML content to be sniffed</param> /// <param name="content">the XML content to be sniffed</param> /// <returns>the encoding sniffed from the specified XML content and/or the corresponding HTTP headers, or <tt>null</tt> if the encoding could not be determined</returns> public static String SniffXmlEncoding(List<NameValuePair> headers, InputStream content) { String encoding = SniffEncodingFromHttpHeaders(headers); if (encoding != null || content == null) { return encoding; } byte[] bytes = Read(content, 3); encoding = SniffEncodingFromUnicodeBom(bytes); if (encoding != null) { return encoding; } bytes = ReadAndPrepend(content, SIZE_OF_XML_CONTENT_SNIFFED, bytes); encoding = SniffEncodingFromXmlDeclaration(bytes); return encoding; }
/// <summary> /// <p>Sniffs encoding settings from the specified content of unknown type by looking for <tt>Content-Type</tt> /// information in the HTTP headers and <a href="http://en.wikipedia.org/wiki/Byte_Order_Mark">Byte Order Mark</a> /// information in the content.</p> /// /// <p>Note that if an encoding is found but it is not supported on the current platform, this method returns /// <tt>null</tt>, as if no encoding had been found.</p> /// </summary> /// <param name="headers">the HTTP response headers sent back with the content to be sniffed</param> /// <param name="content">the content to be sniffed</param> /// <returns>the encoding sniffed from the specified content and/or the corresponding HTTP headers, or <tt>null</tt> if the encoding could not be determined</returns> public static String SniffUnknownContentTypeEncoding(List<NameValuePair> headers, InputStream content) { String encoding = SniffEncodingFromHttpHeaders(headers); if (encoding != null || content == null) { return encoding; } byte[] bytes = Read(content, 3); encoding = SniffEncodingFromUnicodeBom(bytes); return encoding; }
/// <summary> /// <p>If the specified content is HTML content, this method sniffs encoding settings /// from the specified HTML content and/or the corresponding HTTP headers based on the /// <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#determining-the-character-encoding">HTML5 /// encoding sniffing algorithm</a>.</p> /// /// <p>If the specified content is XML content, this method sniffs encoding settings /// from the specified XML content and/or the corresponding HTTP headers using a custom algorithm.</p> /// /// <p>Otherwise, this method sniffs encoding settings from the specified content of unknown type by looking for /// <tt>Content-Type</tt> information in the HTTP headers and /// <a href="http://en.wikipedia.org/wiki/Byte_Order_Mark">Byte Order Mark</a> information in the content.</p> /// /// <p>Note that if an encoding is found but it is not supported on the current platform, this method returns /// <tt>null</tt>, as if no encoding had been found.</p> /// </summary> /// <param name="headers">the HTTP response headers sent back with the content to be sniffed</param> /// <param name="content">the content to be sniffed</param> /// <returns>the encoding sniffed from the specified content and/or the corresponding HTTP headers, or <tt>null</tt> if the encoding could not be determined</returns> public static String SniffEncoding(List<NameValuePair> headers, InputStream content) { if (IsHtml(headers)) { return SniffHtmlEncoding(headers, content); } else if (IsXml(headers)) { return SniffXmlEncoding(headers, content); } else { return SniffUnknownContentTypeEncoding(headers, content); } }
/// <summary> /// Attempts to read <tt>size</tt> bytes from the specified input stream and then prepends the specified prefix to /// the bytes read, returning the resultant byte array. Note that this method is not guaranteed to be able to read /// <tt>size</tt> bytes; however, the returned byte array will always be the exact length of the number of bytes /// read plus the length of the prefix array. /// @throws IOException if an IO error occurs /// </summary> /// <param name="content">the input stream to read from</param> /// <param name="size">the number of bytes to try to read</param> /// <param name="prefix">the byte array to prepend to the bytes read from the specified input stream</param> /// <returns>the bytes read from the specified input stream, prefixed by the specified prefix</returns> static byte[] ReadAndPrepend(InputStream content, int size, byte[] prefix) { byte[] bytes = Read(content, size); byte[] joined = new byte[prefix.Length + bytes.Length]; Array.Copy(prefix, 0, joined, 0, prefix.Length); Array.Copy(bytes, 0, joined, prefix.Length, bytes.Length); return joined; }
/// <summary> /// Attempts to read <tt>size</tt> bytes from the specified input stream. Note that this method is not guaranteed /// to be able to read <tt>size</tt> bytes; however, the returned byte array will always be the exact length of the /// number of bytes read. /// @throws IOException if an IO error occurs /// </summary> /// <param name="content">the input stream to read from</param> /// <param name="size">size the number of bytes to try to read</param> /// <returns>the bytes read from the specified input stream</returns> static byte[] Read(InputStream content, int size) { byte[] bytes = new byte[size]; int count = content.Read(bytes, 0, size); if (count == -1) { bytes = new byte[0]; } else if (count < size) { byte[] smaller = new byte[count]; Array.Copy(bytes, 0, smaller, 0, count); bytes = smaller; } return bytes; }
public InputStreamReader(InputStream stream): base(stream) { }
public InputStreamReader(InputStream stream, Encoding encoding) : base(stream, encoding) { }