comparison Chronosv2/source/Extensions/StreamExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2 using System.IO;
3 using System.Text;
4
5 namespace Chronos.Extensions
6 {
7 /// <summary>
8 /// Extension methods any kind of streams
9 /// </summary>
10 public static class StreamExtensions
11 {
12 #region · Extensions ·
13
14 /// <summary>
15 /// Opens a StreamReader using the default encoding.
16 /// </summary>
17 /// <param name="stream">The stream.</param>
18 /// <returns>The stream reader</returns>
19 public static StreamReader GetReader(this Stream stream)
20 {
21 return stream.GetReader(null);
22 }
23
24 /// <summary>
25 /// Opens a StreamReader using the specified encoding.
26 /// </summary>
27 /// <param name="stream">The stream.</param>
28 /// <param name="encoding">The encoding.</param>
29 /// <returns>The stream reader</returns>
30 public static StreamReader GetReader(this Stream stream, Encoding encoding)
31 {
32 if (stream.CanRead == false)
33 {
34 throw new InvalidOperationException("Stream does not support reading.");
35 }
36
37 encoding = (encoding ?? Encoding.Default);
38
39 return new StreamReader(stream, encoding);
40 }
41
42 /// <summary>
43 /// Opens a StreamWriter using the default encoding.
44 /// </summary>
45 /// <param name="stream">The stream.</param>
46 /// <returns>The stream writer</returns>
47 public static StreamWriter GetWriter(this Stream stream)
48 {
49 return stream.GetWriter(null);
50 }
51
52 /// <summary>
53 /// Opens a StreamWriter using the specified encoding.
54 /// </summary>
55 /// <param name="stream">The stream.</param>
56 /// <param name="encoding">The encoding.</param>
57 /// <returns>The stream writer</returns>
58 public static StreamWriter GetWriter(this Stream stream, Encoding encoding)
59 {
60 if (stream.CanWrite == false)
61 {
62 throw new InvalidOperationException("Stream does not support writing.");
63 }
64
65 encoding = (encoding ?? Encoding.Default);
66
67 return new StreamWriter(stream, encoding);
68 }
69
70 /// <summary>
71 /// Reads all text from the stream using the default encoding.
72 /// </summary>
73 /// <param name="stream">The stream.</param>
74 /// <returns>The result string.</returns>
75 public static string ReadToEnd(this Stream stream)
76 {
77 return stream.ReadToEnd(null);
78 }
79
80 /// <summary>
81 /// Reads all text from the stream using a specified encoding.
82 /// </summary>
83 /// <param name="stream">The stream.</param>
84 /// <param name="encoding">The encoding.</param>
85 /// <returns>The result string.</returns>
86 public static string ReadToEnd(this Stream stream, Encoding encoding)
87 {
88 using (var reader = stream.GetReader(encoding))
89 {
90 return reader.ReadToEnd();
91 }
92 }
93
94 /// <summary>
95 /// Sets the stream cursor to the beginning of the stream.
96 /// </summary>
97 /// <param name="stream">The stream.</param>
98 /// <returns>The stream</returns>
99 public static Stream SeekToBegin(this Stream stream)
100 {
101 if (stream.CanSeek == false)
102 {
103 throw new InvalidOperationException("Stream does not support seeking.");
104 }
105
106 stream.Seek(0, SeekOrigin.Begin);
107
108 return stream;
109 }
110
111 /// <summary>
112 /// Sets the stream cursor to the end of the stream.
113 /// </summary>
114 /// <param name="stream">The stream.</param>
115 /// <returns>The stream</returns>
116 public static Stream SeekToEnd(this Stream stream)
117 {
118 if (stream.CanSeek == false)
119 {
120 throw new InvalidOperationException("Stream does not support seeking.");
121 }
122
123 stream.Seek(0, SeekOrigin.End);
124
125 return stream;
126 }
127
128 /// <summary>
129 /// Copies any stream into a local MemoryStream
130 /// </summary>
131 /// <param name="stream">The source stream.</param>
132 /// <returns>The copied memory stream.</returns>
133 public static MemoryStream CopyToMemory(this Stream stream)
134 {
135 var memoryStream = new MemoryStream((int)stream.Length);
136
137 stream.CopyTo(memoryStream);
138
139 return memoryStream;
140 }
141
142 /// <summary>
143 /// Reads the entire stream and returns a byte array.
144 /// </summary>
145 /// <param name="stream">The stream.</param>
146 /// <returns>The byte array</returns>
147 /// <remarks>Thanks to EsbenCarlsen for providing an update to this method.</remarks>
148 public static byte[] ReadAllBytes(this Stream stream)
149 {
150 using (var memoryStream = stream.CopyToMemory())
151 {
152 return memoryStream.ToArray();
153 }
154 }
155
156 #endregion
157 }
158 }