Permalink
Browse files

Added ReadOnlyStream.

  • Loading branch information...
1 parent 0c92a56 commit f43bc8722bcfa805635302a168718f88764ba8c7 @bgrainger bgrainger committed Feb 10, 2010
View
71 src/Logos.Utility/IO/ReadOnlyStream.cs
@@ -0,0 +1,71 @@
+
+using System;
+using System.IO;
+
+namespace Logos.Utility.IO
+{
+ /// <summary>
+ /// A read-only stream wrapper.
+ /// </summary>
+ public sealed class ReadOnlyStream : WrappingStream
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ReadOnlyStream"/> class.
+ /// </summary>
+ /// <param name="streamBase">The wrapped stream.</param>
+ public ReadOnlyStream(Stream streamBase)
+ : base(streamBase, Ownership.None)
+ {
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the current stream supports writing.
+ /// </summary>
+ /// <returns><c>true</c> if the stream supports writing; otherwise, <c>false</c>.</returns>
+ public override bool CanWrite
+ {
+ get { return false; }
+ }
+
+ /// <summary>
+ /// Begins an asynchronous write operation.
+ /// </summary>
+ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
+ {
+ throw new NotSupportedException();
+ }
+
+ /// <summary>
+ /// Ends an asynchronous write operation.
+ /// </summary>
+ public override void EndWrite(IAsyncResult asyncResult)
+ {
+ throw new NotSupportedException();
+ }
+
+ /// <summary>
+ /// Sets the length of the current stream.
+ /// </summary>
+ public override void SetLength(long value)
+ {
+ throw new NotSupportedException();
+ }
+
+ /// <summary>
+ /// Writes a sequence of bytes to the current stream and advances the current position
+ /// within this stream by the number of bytes written.
+ /// </summary>
+ public override void Write(byte[] buffer, int offset, int count)
+ {
+ throw new NotSupportedException();
+ }
+
+ /// <summary>
+ /// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
+ /// </summary>
+ public override void WriteByte(byte value)
+ {
+ throw new NotSupportedException();
+ }
+ }
+}
View
1 src/Logos.Utility/Logos.Utility.csproj
@@ -59,6 +59,7 @@
<Compile Include="EnvironmentUtility.cs" />
<Compile Include="ExpressionUtility.cs" />
<Compile Include="HashCodeUtility.cs" />
+ <Compile Include="IO\ReadOnlyStream.cs" />
<Compile Include="IO\RebasedStream.cs" />
<Compile Include="IO\WrappingStream.cs" />
<Compile Include="NativeMethods.cs" />
View
1 tests/Logos.Utility.Tests/Logos.Utility.Tests.csproj
@@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="ReadOnlyStreamTests.cs" />
<Compile Include="RebasedStreamTests.cs" />
<Compile Include="DateTimeUtilityTests.cs" />
<Compile Include="DictionaryUtilityTests.cs" />
View
115 tests/Logos.Utility.Tests/ReadOnlyStreamTests.cs
@@ -0,0 +1,115 @@
+
+using System;
+using System.IO;
+using Logos.Utility.IO;
+using NUnit.Framework;
+
+namespace Logos.Utility.Tests
+{
+ [TestFixture]
+ public class ReadOnlyStreamTests
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ m_memStream = new MemoryStream(m_abyStreamData, true);
+ m_stream = new ReadOnlyStream(m_memStream);
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ m_stream = null;
+ m_memStream = null;
+ }
+
+ [Test]
+ public void Constructor()
+ {
+ Assert.IsTrue(m_stream.CanRead);
+ Assert.IsTrue(m_stream.CanSeek);
+ Assert.IsFalse(m_stream.CanWrite);
+ Assert.AreEqual(m_abyStreamData.Length, m_stream.Length);
+ }
+
+ [Test]
+ public void ConstructorNull()
+ {
+ Assert.Throws<ArgumentNullException>(() => { ReadOnlyStream stream = new ReadOnlyStream(null); });
+ }
+
+ [Test]
+ public void Dispose()
+ {
+ m_stream.Dispose();
+ m_stream.Dispose();
+
+ Assert.IsTrue(m_memStream.CanRead);
+ Assert.IsFalse(m_stream.CanRead);
+ Assert.IsFalse(m_stream.CanSeek);
+
+ Assert.Throws<ObjectDisposedException>(() => { long i = m_stream.Length; });
+ Assert.Throws<ObjectDisposedException>(() => { long i = m_stream.Position; });
+ Assert.Throws<ObjectDisposedException>(() => { m_stream.Position = 0; });
+ Assert.Throws<ObjectDisposedException>(() => m_stream.BeginRead(new byte[1], 0, 1, null, null));
+ Assert.Throws<ObjectDisposedException>(() => m_stream.EndRead(null));
+ Assert.Throws<ObjectDisposedException>(() => m_stream.Flush());
+ Assert.Throws<ObjectDisposedException>(() => m_stream.Read(new byte[1], 0, 1));
+ Assert.Throws<ObjectDisposedException>(() => m_stream.ReadByte());
+ Assert.Throws<ObjectDisposedException>(() => m_stream.Seek(0, SeekOrigin.Begin));
+ }
+
+ [Test]
+ public void Flush()
+ {
+ m_stream.Flush();
+ }
+
+ [Test]
+ public void Read()
+ {
+ byte[] aby = new byte[m_abyStreamData.Length];
+ Assert.AreEqual(aby.Length, m_stream.Read(aby, 0, aby.Length));
+ CollectionAssert.AreEqual(m_abyStreamData, aby);
+ }
+
+ [Test]
+ public void ReadAsync()
+ {
+ byte[] aby = new byte[m_abyStreamData.Length];
+ IAsyncResult ar = m_stream.BeginRead(aby, 0, 8, null, null);
+ Assert.AreEqual(aby.Length, m_stream.EndRead(ar));
+ CollectionAssert.AreEqual(m_abyStreamData, aby);
+ }
+
+ [Test]
+ public void ReadByte()
+ {
+ Assert.AreEqual(m_abyStreamData[0], m_stream.ReadByte());
+ Assert.AreEqual(m_abyStreamData[1], m_stream.ReadByte());
+ Assert.AreEqual(m_abyStreamData[2], m_stream.ReadByte());
+ m_stream.Seek(1, SeekOrigin.Current);
+ Assert.AreEqual(m_abyStreamData[4], m_stream.ReadByte());
+ m_stream.Position = m_stream.Position - 2;
+ Assert.AreEqual(m_abyStreamData[3], m_stream.ReadByte());
+ m_stream.Seek(7, SeekOrigin.Begin);
+ Assert.AreEqual(m_abyStreamData[7], m_stream.ReadByte());
+ m_stream.Seek(0, SeekOrigin.End);
+ Assert.AreEqual(-1, m_stream.ReadByte());
+ }
+
+ [Test]
+ public void Write()
+ {
+ Assert.Throws<NotSupportedException>(() => m_stream.Write(m_abyStreamData, 0, 1));
+ Assert.Throws<NotSupportedException>(() => m_stream.WriteByte(0));
+ Assert.Throws<NotSupportedException>(() => m_stream.BeginWrite(m_abyStreamData, 0, 1, null, null));
+ Assert.Throws<NotSupportedException>(() => m_stream.EndWrite(null));
+ Assert.Throws<NotSupportedException>(() => m_stream.SetLength(0));
+ }
+
+ Stream m_memStream;
+ Stream m_stream;
+ byte[] m_abyStreamData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
+ }
+}

0 comments on commit f43bc87

Please sign in to comment.