System.IO.Stream Extension Methods

Stream.ReadToEnd Method

A Stream extension method that reads a stream to the end.

Syntax

string ReadToEnd(Stream @this)

Parameters

@this
The @this to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_IO_Stream_ReadToEnd
     {
         [TestMethod]
         public void ReadToEnd()
         {
             var fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_ReadToEnd.txt"));
 
             // Examples
             string value = "";
             using (FileStream @this = fileInfo.Create())
             {
                 @this.WriteByte(0);
                 @this.Position = 0;
                 value = @this.ReadToEnd(); // return "\0";
             }
 
             // Unit Test
             Assert.AreEqual("\0", value);
         }
     }
 }
                

Stream.ReadToEnd Method

A Stream extension method that reads a stream to the end.

Syntax

string ReadToEnd(Stream @this,long position)

Parameters

@this
The @this to act on.
position
The position to set in the stream.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_IO_Stream_ReadToEnd
     {
         [TestMethod]
         public void ReadToEnd()
         {
             var fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_ReadToEnd.txt"));
 
             // Examples
             string value = "";
             using (FileStream @this = fileInfo.Create())
             {
                 @this.WriteByte(0);
                 @this.Position = 0;
                 value = @this.ReadToEnd(); // return "\0";
             }
 
             // Unit Test
             Assert.AreEqual("\0", value);
         }
     }
 }
                

Stream.ToByteArray Method

A Stream extension method that converts the Stream to a byte array.

Syntax

byte[] ToByteArray(Stream @this)

Parameters

@this
The Stream to act on.

Examples


 // Copyright (c) 2013 Jonathan Magnan (http://zzzportal.com)
 // All rights reserved.
 // Licensed under MIT License (MIT)
 // License can be found here: https://zextensionmethods.codeplex.com/license
 
 using System;
 using System.IO;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Z;
 
 namespace ExtensionMethods.Examples
 {
     [TestClass]
     public class System_IO_Stream_ToByteArray
     {
         [TestMethod]
         public void ToByteArray()
         {
             var fileInfo = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Examples_System_IO_FileInfo_ReadToEnd.txt"));
 
             // Examples
             byte[] value;
             using (FileStream @this = fileInfo.Create())
             {
                 @this.WriteByte(0);
                 @this.Position = 0;
                 value = @this.ToByteArray(); // return "0";
             }
 
             // Unit Test
             Assert.AreEqual(0, value[0]);
         }
     }
 }
                

Last edited Nov 23, 2013 at 6:55 PM by Jonathan_Magnan, version 1