// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; using System.Text; namespace Test.Utility { public static class StreamExtensions { public static byte[] ReadAllBytes(this Stream stream) { var memoryStream = stream as MemoryStream; if (memoryStream != null) { return memoryStream.ToArray(); } else { using (memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); return memoryStream.ToArray(); } } } public static Stream AsStream(this string value) { return new MemoryStream(Encoding.UTF8.GetBytes(value)); } public static string ReadToEnd(this Stream stream) { using (var streamReader = new StreamReader(stream)) { return streamReader.ReadToEnd(); } } } }