Files
Ryan Durand 9ef3748747 Updating copyrights for Engine Programs.
#rnx
#rb none
#jira none

#ROBOMERGE-OWNER: ryan.durand
#ROBOMERGE-AUTHOR: ryan.durand
#ROBOMERGE-SOURCE: CL 10869242 in //Fortnite/Release-12.00/... via CL 10869536
#ROBOMERGE-BOT: FORTNITE (Main -> Dev-EngineMerge) (v613-10869866)

[CL 10870955 by Ryan Durand in Main branch]
2019-12-26 23:01:54 -05:00

76 lines
2.1 KiB
C#

// 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
{
/// <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;
}
}
}