/// <summary> /// <para>Decompresses data from a stream, using GZip algorithm.</para> /// </summary> /// <param name="self">Stream to read and decompress data from.</param> /// <returns>Decompressed contents of current <paramref name="self"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="GZipStream"/> /// <seealso cref="GZip(Stream, CompressionMode)"/> /// <seealso cref="GZip{T}(T, byte[])"/> public static byte[] GZip(this Stream self) { Assertion.NotNull(self); return(new GZipStream(self, CompressionMode.Decompress, true).Bytes(true)); }
/// <summary> /// <para>Reads text using specified <see cref="TextReader"/> and returns it as a list of strings, using default system-dependent string separator.</para> /// </summary> /// <param name="self"><see cref="TextReader"/> which is used to read text from its underlying source.</param> /// <returns>List of strings which have been read from a <paramref name="self"/>.</returns> /// <param name="close">Whether to close a <paramref name="self"/> after all texts have been read.</param> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> public static string[] Lines(this TextReader self, bool close = false) { Assertion.NotNull(self); return(self.Text(close).Lines()); }
/// <summary> /// <para>Returns a <see cref="BinaryWriter"/> for writing data to specified <see cref="Stream"/>.</para> /// </summary> /// <param name="self">Target stream to write to.</param> /// <param name="encoding">Text encoding to use by <see cref="BinaryWriter"/>. If not specified, default <see cref="Encoding.UTF8"/> will be used.</param> /// <returns>Binary writer instance that wraps <see cref="self"/> stream.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="BinaryWriter"/> /// <seealso cref="BinaryReader(Stream, Encoding)"/> public static BinaryWriter BinaryWriter(this Stream self, Encoding encoding = null) { Assertion.NotNull(self); return(encoding != null ? new BinaryWriter(self, encoding) : new BinaryWriter(self)); }
/// <summary> /// <para>Creates a GZip compression stream from a specified one, using either compression or uncompression logic.</para> /// </summary> /// <param name="self">Wrapped stream which contents will be compressed/uncompressed.</param> /// <param name="mode">GZip algorithm working mode, specifying whether to automatically compress data when writing to a stream, or uncompress data when reading from it.</param> /// <returns>GZip compression stream which wraps original <paramref name="self"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="GZipStream"/> /// <seealso cref="GZip{T}(T, byte[])"/> /// <seealso cref="GZip(Stream)"/> public static GZipStream GZip(this Stream self, CompressionMode mode) { Assertion.NotNull(self); return(new GZipStream(self, mode)); }
/// <summary> /// <para>Creates a buffered version of <see cref="Stream"/> from specified one.</para> /// </summary> /// <param name="self">Original stream that should be buffered.</param> /// <param name="bufferSize">Size of buffer in bytes. If not specified, default buffer size will be used.</param> /// <returns>Buffer version of stream that wraps original <paramref name="self"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="BufferedStream"/> public static BufferedStream Buffered(this Stream self, int?bufferSize = null) { Assertion.NotNull(self); return(bufferSize != null ? new BufferedStream(self, bufferSize.Value) : new BufferedStream(self)); }
/// <summary> /// <para>Creates a Deflate compression stream from a specified one, using either compression or uncompression logic.</para> /// </summary> /// <param name="self">Wrapped stream which contents will be compressed/uncompressed.</param> /// <param name="mode">Deflate algorithm working mode, specifying whether to automatically compress data when writing to a stream, or uncompress data when reading from it.</param> /// <returns>Deflate compression stream which wraps original <paramref name="self"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="DeflateStream"/> /// <seealso cref="Deflate{T}(T, byte[])"/> /// <seealso cref="Deflate(Stream)"/> public static DeflateStream Deflate(this Stream self, CompressionMode mode) { Assertion.NotNull(self); return(new DeflateStream(self, mode)); }
/// <summary> /// <para>Returns all available text data from a source stream.</para> /// </summary> /// <param name="self">Source stream to read from.</param> /// <param name="close">Whether to automatically close source stream when all available data has been successfully read from it.</param> /// <param name="encoding">Encoding to be used for bytes-to-text conversion. If not specified, default <see cref="Encoding.UTF8"/> will be used.</param> /// <returns>Text data from a <see cref="self"/> stream.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> public static string Text(this Stream self, bool close = false, Encoding encoding = null) { Assertion.NotNull(self); return(self.CanRead ? self.TextReader(encoding).Text(close) : string.Empty); }
/// <summary> /// <para>Returns a <see cref="TextWriter"/> for writing text data to specified <see cref="Stream"/>.</para> /// </summary> /// <param name="self">Target stream to write to.</param> /// <param name="encoding">Text encoding to use by <see cref="TextWriter"/>. If not specified, default <see cref="Encoding.UTF8"/> will be used.</param> /// <returns>Text writer instance that wraps <see cref="self"/> stream.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="TextWriter"/> /// <seealso cref="TextReader(Stream, Encoding)"/> public static TextWriter TextWriter(this Stream self, Encoding encoding = null) { Assertion.NotNull(self); return(encoding != null ? new StreamWriter(self, encoding) : new StreamWriter(self, Encoding.UTF8)); }
/// <summary> /// <para>Reads entire contents of file and returns it as a byte array.</para> /// </summary> /// <param name="self">File to read data from.</param> /// <returns>Byte content of specified <paramref name="self"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> public static byte[] Bytes(this FileInfo self) { Assertion.NotNull(self); return(self.OpenRead().Bytes(true)); }
public void assert_not_null() { Assert.Throws <ArgumentNullException>(() => Assertion.NotNull(null)); Assertion.NotNull(new object()); }
/// <summary> /// <para>Translates XML content from specified <see cref="XmlReader"/> into a dictionary.</para> /// <para>Attributes in XML document are translated to string keys and values, nodes become dictionaries with string keys themselves.</para> /// </summary> /// <param name="self"><see cref="XmlReader"/> used for reading of XML data which is to be converted to <see cref="IDictionary{TKey,TValue}"/> instance.</param> /// <returns>Dictionary that follows the structure of XML data from <see cref="XmlReader"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> public static IDictionary <string, object> Dictionary(this XmlReader self) { Assertion.NotNull(self); return(System.Xml.Linq.XDocument.Load(self).Dictionary()); }
/// <summary> /// <para>Translates specified <see cref="XDocument"/> into a dictionary.</para> /// <para>Attributes in XML document are translated to string keys and values, nodes become dictionaries with string keys themselves.</para> /// </summary> /// <param name="self"><see cref="XDocument"/>, whose structure is to be converted to <see cref="IDictionary{TKey,TValue}"/> instance.</param> /// <returns>Dictionary that follows the structure of <see cref="XDocument"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="Dictionary(XElement)"/> public static IDictionary <string, object> Dictionary(this XDocument self) { Assertion.NotNull(self); return(self.Root.Dictionary()); }
/// <summary> /// <para>Deserializes XML data from <see cref="XmlReader"/> into object of specified type.</para> /// </summary> /// <typeparam name="T">Type of object which is to be the result of deserialization process.</typeparam> /// <param name="self"><see cref="XmlReader"/> used for retrieving XML data for deserialization.</param> /// <param name="types">Additional types to be used by <see cref="XmlSerializer"/> for deserialization purposes.</param> /// <returns>Deserialized XML contents from a <paramref name="self"/> as the object (or objects graph with a root element) of <paramref name="self"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="XmlSerializer"/> /// <seealso cref="Deserialize(XmlReader, Type, IEnumerable{Type})"/> public static T Deserialize <T>(this XmlReader self, IEnumerable <Type> types = null) { Assertion.NotNull(self); return(self.Deserialize(typeof(T), types).As <T>()); }