using System.Text; namespace System.IO { #pragma warning disable CS1591 // O comentário XML ausente não foi encontrado para o tipo ou membro visível publicamente public static class Stream_CB_Extension { #pragma warning restore CS1591 // O comentário XML ausente não foi encontrado para o tipo ou membro visível publicamente /// /// When overridden in a derived class, reads a sequence of bytes from the current /// stream and advances the position within the stream by the number of bytes read. /// /// Target stream. /// /// /// /// /// /// public static byte[] Read(this Stream F) { byte[] Res = new byte[F.Length]; int numDeBytesPraLer = Res.Length; int numDeBytesLidos = 0; while (numDeBytesPraLer > 0) { int n = F.Read(Res, numDeBytesLidos, numDeBytesPraLer); if (n == 0) break; numDeBytesLidos += n; numDeBytesPraLer -= n; } return Res; } /// /// When overridden in a derived class, writes a sequence of bytes to the current /// stream and advances the current position within this stream by the number of /// bytes written. /// /// Target stream. /// The text that will be written in the current stream. /// The Encoding used to write to the current stream. /// /// /// /// /// public static void Write(this Stream F, string text, Encoding encoding) => Write(F, text.ToCharArray(), encoding); /// /// When overridden in a derived class, writes a sequence of bytes to the current /// stream and advances the current position within this stream by the number of /// bytes written. /// .UTF8 is used by default in the method. /// /// Target stream. /// The text that will be written in the current stream. public static void Write(this Stream F, string text) => Write(F, text, Encoding.UTF8); /// /// When overridden in a derived class, writes a sequence of bytes to the current /// stream and advances the current position within this stream by the number of /// bytes written. /// /// Target stream. /// The list of characters that will be written to the current stream. /// The Encoding used to write to the current stream. /// /// /// /// /// /// public static void Write(this Stream F, char[] chars, Encoding encoding) => Write(F, encoding.GetBytes(chars)); /// /// When overridden in a derived class, writes a sequence of bytes to the current /// stream and advances the current position within this stream by the number of /// bytes written. /// .UTF8 is used by default in the method. /// /// Target stream. /// The list of characters that will be written to the current stream. /// /// /// /// /// /// public static void Write(this Stream F, char[] chars) => Write(F, chars, Encoding.UTF8); /// /// When overridden in a derived class, writes a sequence of bytes to the current /// stream and advances the current position within this stream by the number of /// bytes written. /// /// Target stream. /// An array of bytes. This method copies count bytes from buffer to the current stream. /// /// /// /// /// /// public static void Write(this Stream F, byte[] bytes) => F.Write(bytes, 0, bytes.Length); /// /// Gets an array of characters from the current stream. /// /// Target stream. /// The Encoding used to write to the current stream. public static char[] GetChars(this Stream F, Encoding encoding) => encoding.GetChars(Read(F)); /// /// Gets an array of characters from the current stream. /// .UTF8 is used by default in the method. /// /// Target stream. public static char[] GetChars(this Stream F) => GetChars(F, Encoding.UTF8); /// /// Gets a string from the current stream. /// /// Target stream. /// The Encoding used to write to the current stream. public static string GetString(this Stream F, Encoding encoding) => new string(GetChars(F, encoding)); /// /// Gets a string from the current stream. /// .UTF8 is used by default in the method. /// /// Target stream. public static string GetString(this Stream F) => GetString(F, Encoding.UTF8); /// /// Generates a object from the current flow. /// /// Target stream. /// The method returns a Guid value by reading a copy of the current stream. public static Guid GenerateGuid(this Stream F) { MemoryStream memory = new MemoryStream(); F.CopyTo(memory); byte[] guid = new byte[16]; byte[] content = Read(memory); for (int I = 0, g = 0; I < content.Length; I++, g++) guid[(g >= 16 ? g = 0 : g)] ^= content[I]; return new Guid(guid); } } }