/// <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 > 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(); } }
/// <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 > 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(); } }
/// <summary>Instantiates version information.</summary> /// <remarks>Instantiates version information.</remarks> /// <param name="pckg">the package</param> /// <param name="module">the module, or <code>null</code></param> /// <param name="release">the release, or <code>null</code></param> /// <param name="time">the build time, or <code>null</code></param> /// <param name="clsldr">the class loader, or <code>null</code></param> protected internal VersionInfo(string pckg, string module, string release, string time, string clsldr) { // the property names Args.NotNull(pckg, "Package identifier"); infoPackage = pckg; infoModule = (module != null) ? module : Unavailable; infoRelease = (release != null) ? release : Unavailable; infoTimestamp = (time != null) ? time : Unavailable; infoClassloader = (clsldr != null) ? clsldr : Unavailable; }
/// <summary>Converts the specified string to byte array of ASCII characters.</summary> /// <remarks>Converts the specified string to byte array of ASCII characters.</remarks> /// <param name="data">the string to be encoded</param> /// <returns>The string as a byte array.</returns> public static byte[] GetAsciiBytes(string data) { Args.NotNull(data, "Input"); try { return(Sharpen.Runtime.GetBytesForString(data, Consts.Ascii.Name())); } catch (UnsupportedEncodingException) { throw new Error("ASCII not supported"); } }
/// <summary>Converts the specified string to a byte array.</summary> /// <remarks> /// Converts the specified string to a byte array. If the charset is not supported the /// default system charset is used. /// </remarks> /// <param name="data">the string to be encoded</param> /// <param name="charset">the desired character encoding</param> /// <returns>The resulting byte array.</returns> public static byte[] GetBytes(string data, string charset) { Args.NotNull(data, "Input"); Args.NotEmpty(charset, "Charset"); try { return(Sharpen.Runtime.GetBytesForString(data, charset)); } catch (UnsupportedEncodingException) { return(Sharpen.Runtime.GetBytesForString(data)); } }
/// <summary>Converts the byte array of ASCII characters to a string.</summary> /// <remarks> /// Converts the byte array of ASCII characters to a string. This method is /// to be used when decoding content of HTTP elements (such as response /// headers) /// </remarks> /// <param name="data">the byte array to be encoded</param> /// <param name="offset">the index of the first byte to encode</param> /// <param name="length">the number of bytes to encode</param> /// <returns>The string representation of the byte array</returns> public static string GetAsciiString(byte[] data, int offset, int length) { Args.NotNull(data, "Input"); try { return(Sharpen.Runtime.GetStringForBytes(data, offset, length, Consts.Ascii.Name( ))); } catch (UnsupportedEncodingException) { throw new Error("ASCII not supported"); } }
/// <summary>Converts the byte array of HTTP content characters to a string.</summary> /// <remarks> /// Converts the byte array of HTTP content characters to a string. If /// the specified charset is not supported, default system encoding /// is used. /// </remarks> /// <param name="data">the byte array to be encoded</param> /// <param name="offset">the index of the first byte to encode</param> /// <param name="length">the number of bytes to encode</param> /// <param name="charset">the desired character encoding</param> /// <returns>The result of the conversion.</returns> public static string GetString(byte[] data, int offset, int length, string charset ) { Args.NotNull(data, "Input"); Args.NotEmpty(charset, "Charset"); try { return(Sharpen.Runtime.GetStringForBytes(data, offset, length, charset)); } catch (UnsupportedEncodingException) { return(Sharpen.Runtime.GetStringForBytes(data, offset, length)); } }
/// <summary>Obtains MIME type of the entity, if known.</summary> /// <remarks>Obtains MIME type of the entity, if known.</remarks> /// <param name="entity">must not be null</param> /// <returns>the character set, or null if not found</returns> /// <exception cref="Org.Apache.Http.ParseException">if header elements cannot be parsed /// </exception> /// <exception cref="System.ArgumentException">if entity is null</exception> /// <since>4.1</si [System.ObsoleteAttribute(@"(4.1.3) use Org.Apache.Http.Entity.ContentType.GetOrDefault(Org.Apache.Http.HttpEntity)")]y)" public static string GetContentMimeType(HttpEntity entity) { Args.NotNull(entity, "Entity"); string mimeType = null; if (entity.GetContentType() != null) { HeaderElement[] values = entity.GetContentType().GetElements(); if (values.Length > 0) { mimeType = values[0].GetName(); } } return(mimeType); }
public static void FormatAddress(StringBuilder buffer, EndPoint socketAddress) { Args.NotNull(buffer, "Buffer"); Args.NotNull(socketAddress, "Socket address"); if (socketAddress is IPEndPoint) { IPEndPoint socketaddr = ((IPEndPoint)socketAddress); IPAddress inetaddr = socketaddr.Address; buffer.Append(inetaddr != null ? inetaddr.GetHostAddress() : inetaddr).Append(':' ).Append(socketaddr.Port); } else { buffer.Append(socketAddress); } }
/// <summary>Obtains character set of the entity, if known.</summary> /// <remarks>Obtains character set of the entity, if known.</remarks> /// <param name="entity">must not be null</param> /// <returns>the character set, or null if not found</returns> /// <exception cref="Org.Apache.Http.ParseException">if header elements cannot be parsed /// </exception> /// <exception cref="System.ArgumentException">if entity is null</exception> [System.ObsoleteAttribute(@"(4.1.3) use Org.Apache.Http.Entity.ContentType.GetOrDefault(Org.Apache.Http.HttpEntity)")] public static string GetContentCharSet(HttpEntity entity) { Args.NotNull(entity, "Entity"); string charset = null; if (entity.GetContentType() != null) { HeaderElement[] values = entity.GetContentType().GetElements(); if (values.Length > 0) { NameValuePair param = values[0].GetParameterByName("charset"); if (param != null) { charset = param.GetValue(); } } } return(charset); }
/// <summary>Updates an entity in a response by first consuming an existing entity, then setting the new one. /// </summary> /// <remarks>Updates an entity in a response by first consuming an existing entity, then setting the new one. /// </remarks> /// <param name="response">the response with an entity to update; must not be null.</param> /// <param name="entity">the entity to set in the response.</param> /// <exception cref="System.IO.IOException"> /// if an error occurs while reading the input stream on the existing /// entity. /// </exception> /// <exception cref="System.ArgumentException">if response is null.</exception> /// <since>4.3</since> public static void UpdateEntity(HttpResponse response, HttpEntity entity) { Args.NotNull(response, "Response"); Consume(response.GetEntity()); response.SetEntity(entity); }
/// <summary>Converts the byte array of HTTP content characters to a string.</summary> /// <remarks> /// Converts the byte array of HTTP content characters to a string. If /// the specified charset is not supported, default system encoding /// is used. /// </remarks> /// <param name="data">the byte array to be encoded</param> /// <param name="charset">the desired character encoding</param> /// <returns>The result of the conversion.</returns> public static string GetString(byte[] data, string charset) { Args.NotNull(data, "Input"); return(GetString(data, 0, data.Length, charset)); }
/// <summary>Converts the byte array of ASCII characters to a string.</summary> /// <remarks> /// Converts the byte array of ASCII characters to a string. This method is /// to be used when decoding content of HTTP elements (such as response /// headers) /// </remarks> /// <param name="data">the byte array to be encoded</param> /// <returns>The string representation of the byte array</returns> public static string GetAsciiString(byte[] data) { Args.NotNull(data, "Input"); return(GetAsciiString(data, 0, data.Length)); }