public static async Task <string> ReadBytesAsync(byte[] source, int selector = 0) { switch (selector) { /* BOM will be removed. */ case 0: // MemoryStream constructor and StreamReader constructor then ReadToEndAsync using (var ms = new MemoryStream(source)) using (var sr = new StreamReader(ms)) return(await sr.ReadToEndAsync()); case 1: // MemoryStream constructor and StreamReader constructor with encoding then ReadToEndAsync using (var ms = new MemoryStream(source)) using (var sr = new StreamReader(ms, Encoding.UTF8)) return(await sr.ReadToEndAsync()); case 2: // StreamReader constructor then MemoryStream.WriteAsync then ReadToEndAsync using (var ms = new MemoryStream()) using (var sr = new StreamReader(ms)) { await ms.WriteAsync(source, 0, source.Length); ms.Seek(0, SeekOrigin.Begin); return(await sr.ReadToEndAsync()); } case 3: // StreamReader constructor then BinaryWriter.Write then ReadToEndAsync using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) using (var sr = new StreamReader(ms)) { bw.Write(source); ms.Seek(0, SeekOrigin.Begin); return(await sr.ReadToEndAsync()); } case 4: // MemoryStream constructor then ReadAsync then UnicodeStringReader.Read using (var ms = new MemoryStream(source)) { var buff = new byte[ms.Length]; await ms.ReadAsync(buff, 0, buff.Length); return(UnicodeStringReader.Read(buff)); } /* BOM will remain as unreadable character. */ case 5: // MemoryStream constructor then ReadAsync then Encoding.GetString using (var ms = new MemoryStream(source)) { var buff = new byte[ms.Length]; await ms.ReadAsync(buff, 0, buff.Length); return(Encoding.UTF8.GetString(buff)); } case 6: // MemoryStream WriteAsync then ToArray then Encoding.GetString using (var ms = new MemoryStream()) { await ms.WriteAsync(source, 0, source.Length); return(Encoding.UTF8.GetString(ms.ToArray())); } case 7: // MemoryStream WriteAsync then GetBuffer then Encoding.GetString using (var ms = new MemoryStream()) { await ms.WriteAsync(source, 0, source.Length); return(Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length)); } default: throw new ArgumentOutOfRangeException(nameof(selector)); } }
public static string ReadBytes(byte[] source, int selector = 0) { switch (selector) { /* BOM will be removed. */ case 0: // MemoryStream constructor and StreamReader constructor then ReadToEnd using (var ms = new MemoryStream(source)) using (var sr = new StreamReader(ms)) return(sr.ReadToEnd()); case 1: // MemoryStream constructor and StreamReader constructor with encoding then ReadToEnd using (var ms = new MemoryStream(source)) using (var sr = new StreamReader(ms, Encoding.UTF8)) return(sr.ReadToEnd()); case 2: // StreamReader constructor then MemoryStream.Write then ReadToEnd using (var ms = new MemoryStream()) using (var sr = new StreamReader(ms)) { ms.Write(source, 0, source.Length); ms.Seek(0, SeekOrigin.Begin); return(sr.ReadToEnd()); } case 3: // StreamReader constructor with Encoding then MemoryStream.Write then ReadToEnd using (var ms = new MemoryStream()) using (var sr = new StreamReader(ms, Encoding.UTF8)) { ms.Write(source, 0, source.Length); ms.Seek(0, SeekOrigin.Begin); return(sr.ReadToEnd()); } case 4: // StreamReader constructor then BinaryWriter.Write then ReadToEnd using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) using (var sr = new StreamReader(ms)) { bw.Write(source); ms.Seek(0, SeekOrigin.Begin); return(sr.ReadToEnd()); } case 5: // MemoryStream constructor then ToArray then UnicodeStringReader.Read using (var ms = new MemoryStream(source)) { return(UnicodeStringReader.Read(ms.ToArray())); } case 6: // MemoryStream constructor then GetBuffer then UnicodeStringReader.Read using (var ms = new MemoryStream(source, 0, source.Length, false, true)) { // When a MemoryStream is instantiated with a byte array, MemoryStream.GetBuffer // method returns the identical byte array and needs not to specify its length. return(UnicodeStringReader.Read(ms.GetBuffer())); } /* BOM will remain as unreadable character. */ case 7: // Encoding.GetString return(Encoding.UTF8.GetString(source)); case 8: // MemoryStream constructor then ToArray then Encoding.GetString using (var ms = new MemoryStream(source)) { return(Encoding.UTF8.GetString(ms.ToArray())); } case 9: // MemoryStream constructor then GetBuffer then Encoding.GetString using (var ms = new MemoryStream(source, 0, source.Length, false, true)) { // When a MemoryStream is instantiated with a byte array, MemoryStream.GetBuffer // method returns the identical byte array and needs not to specify its length. return(Encoding.UTF8.GetString(ms.GetBuffer())); } default: throw new ArgumentOutOfRangeException(nameof(selector)); } }