Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/AutomationUtils/StreamUtils.cs
Ryan Gerleve e7612e89b2 Merging //UE4/Dev-Main to Dev-Networking (//UE4/Dev-Networking)
#rb none
#rnx

[CL 4341874 by Ryan Gerleve in Dev-Networking branch]
2018-09-04 14:32:41 -04:00

74 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomationTool
{
/// <summary>
/// Extension methods for Stream classes
/// </summary>
public static class StreamUtils
{
/// <summary>
/// Read a stream into another, buffering in 4K chunks.
/// </summary>
/// <param name="output">this</param>
/// <param name="input">the Stream to read from</param>
/// <returns>same stream for expression chaining.</returns>
public static Stream ReadFrom(this Stream output, Stream input)
{
long bytesRead;
return output.ReadFrom(input, out bytesRead);
}
/// <summary>
/// Read a stream into another, buffering in 4K chunks.
/// </summary>
/// <param name="output">this</param>
/// <param name="input">the Stream to read from</param>
/// <param name="totalBytesRead">returns bytes read</param>
/// <returns>same stream for expression chaining.</returns>
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;
}
/// <summary>
/// Read stream into a new MemoryStream. Useful for chaining together expressions.
/// </summary>
/// <param name="input">Stream to read from.</param>
/// <returns>memory stream that contains the stream contents.</returns>
public static MemoryStream ReadIntoMemoryStream(this Stream input)
{
MemoryStream data = new MemoryStream(4096);
data.ReadFrom(input);
return data;
}
/// <summary>
/// Writes the entire contents of a byte array to the stream.
/// </summary>
/// <param name="stream"></param>
/// <param name="arr"></param>
/// <returns></returns>
public static Stream Write(this Stream stream, byte[] arr)
{
stream.Write(arr, 0, arr.Length);
return stream;
}
}
}