C# || How To Manually Copy Two Streams From One To Another Using C#
The following is a module with functions which demonstrates how to manually copy two steams from one to another using C#.
1. Manually Copy Stream
The example below demonstrates the use of ‘Utils.Extensions.CopyStream‘ to copy two streams.
1 2 3 4 5 6 7 8 9 |
// Manually Copy Stream using Utils; System.IO.Stream stream1; System.IO.Stream stream2; // Copy stream1 to stream2 stream1.CopyStream(stream2); |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 12, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Extensions { /// <summary> /// Reads bytes from the 'streamFrom' and writes them to 'streamTo' /// </summary> /// <param name="streamFrom">The source stream</param> /// <param name="streamTo">The destination stram</param> /// <returns>The total bytes written</returns> public static int CopyStream(this System.IO.Stream streamFrom, System.IO.Stream streamTo, int bufferSize = 4096) { int bytesWritten = 0; byte[] bytBuffer = new byte[bufferSize]; int bytesRead = 0; var tempPos = streamFrom.CanSeek ? (int?)streamFrom.Position : null; if (streamFrom.CanSeek) { streamFrom.Position = 0; } do { bytesRead = streamFrom.Read(bytBuffer, 0, bytBuffer.Length); if (bytesRead > 0) { streamTo.Write(bytBuffer, 0, bytesRead); bytesWritten += bytesRead; } } while (bytesRead > 0); if (tempPos.HasValue) { streamFrom.Position = tempPos.Value; } return bytesWritten; } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 12, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { static void Main(string[] args) { try { System.IO.Stream stream1; System.IO.Stream stream2; // Copy stream1 to stream2 stream1.CopyStream(stream2); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply