// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AutomationTool { /// /// Extension methods for Stream classes /// public static class StreamUtils { /// /// Read a stream into another, buffering in 4K chunks. /// /// this /// the Stream to read from /// same stream for expression chaining. public static Stream ReadFrom(this Stream output, Stream input) { long bytesRead; return output.ReadFrom(input, out bytesRead); } /// /// Read a stream into another, buffering in 4K chunks. /// /// this /// the Stream to read from /// returns bytes read /// same stream for expression chaining. public static Stream ReadFrom(this Stream output, Stream input, out long totalBytesRead) { totalBytesRead = 0; const int BytesToRead = 4096; byte[] buf = new byte[BytesToRead]; int bytesReadThisTime = 0; do { bytesReadThisTime = input.Read(buf, 0, BytesToRead); totalBytesRead += bytesReadThisTime; output.Write(buf, 0, bytesReadThisTime); } while (bytesReadThisTime != 0); return output; } /// /// Read stream into a new MemoryStream. Useful for chaining together expressions. /// /// Stream to read from. /// memory stream that contains the stream contents. public static MemoryStream ReadIntoMemoryStream(this Stream input) { MemoryStream data = new MemoryStream(4096); data.ReadFrom(input); return data; } /// /// Writes the entire contents of a byte array to the stream. /// /// /// /// public static Stream Write(this Stream stream, byte[] arr) { stream.Write(arr, 0, arr.Length); return stream; } } }