How to copy one stream to another in C# and VB.NET

To copy one stream to another in C# and VB.NET you can use the following snippet.

Sample C#

//.NET 3.5
public static void CopyStream(Stream inputStream, Stream outputStream)
{
	var bytes = new byte[4096];
	int count;
	while ((count = inputStream.Read(bytes, 0, bytes.Length)) > 0)
	{
		outputStream.Write(bytes, 0, count);
	}
}

//.NET 4.0 +
public static void CopyStream(Stream inputStream, Stream outputStream)
{
	inputStream.CopyTo(outputStream, 4096);
}

//.NET 4.5+
 inputStream.CopyToAsync(outputStream, 4096);

Sample VB.NET

'.NET 3.5
Public Shared Sub CopyStream(inputStream As Stream, outputStream As Stream)
	Dim bytes = New Byte(4095) {}
	Dim count As Integer
	While (InlineAssignHelper(count, inputStream.Read(bytes, 0, bytes.Length))) > 0
		outputStream.Write(bytes, 0, count)
	End While
End Sub

'.NET 4.0 +
Public Shared Sub CopyStream(inputStream As Stream, outputStream As Stream)
	inputStream.CopyTo(outputStream, 4096)
End Sub

'.NET 4.5 +
inputStream.CopyToAsync(outputStream, 4096)

How to convert Stream to ByteArray in C# and VB.NET

To convert Stream to ByteArray in C# and VB.NET you can use the following snippet.

Samples

Sample C#

Sample VB.NET

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

How to Convert String to Stream in C# and VB.NET

To Convert String to Stream in C# and VB.NET you can use the following snippet.

Sample C#

//when using this method, put StringToStream in a using construct
public static Stream StringToStream(string input, Encoding enc)
{
	var memoryStream = new MemoryStream();
	var streamWriter = new StreamWriter(memoryStream,enc);
	streamWriter.Write(input);
	streamWriter.Flush();
	memoryStream.Position = 0;
	return memoryStream;
}

Sample VB.NET

'when using this method, put StringToStream in a using construct
Public Shared Function StringToStream(input As String, enc As Encoding) As Stream
	Dim memoryStream = New MemoryStream()
	Dim streamWriter = New StreamWriter(memoryStream, enc)
	streamWriter.Write(input)
	streamWriter.Flush()
	memoryStream.Position = 0
	Return memoryStream
End Function

How to read a File to Textreader in C# and VB.NET

To read a File to Textreader in C# and VB.NET you can use the following snippet.

Sample C#

TextReader reader = File.OpenText(@"C:\dummy.txt");

Sample VB.NET

Dim reader As TextReader = File.OpenText(@"C:\dummy.txt")

How to read a file to string in Java

To read a file to string in Java you can use the following snippet.

Sample Java

public static string readFileToString(FileInputStream _fileInputStream)
{
	try {  
		DataInputStream dataInputStream = new DataInputStream (_fileInputStream);  
		byte[] Bytes = new byte[dataInputStream.available ()];  
		dataInputStream.readFully (Bytes);  
		dataInputStream.close ();  
		String result = new String (Bytes, 0, Bytes.length, "Cp850");  
		return result;  
	}  
	catch (Exception e) {  
		e.printStackTrace();  
		return "";
	} 
}