public void TestWriteLengthSpecified() { var inner = new MemoryStream(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); using (var stream = new PartialStream(inner, 2, 4, false)) { Assert.AreEqual(8, stream.InnerStream.Length); Assert.AreEqual(2, stream.InnerStream.Position); Assert.AreEqual(4, stream.Length); Assert.AreEqual(0, stream.Position); stream.Write(new byte[] {0x02, 0x03}, 0, 2); stream.WriteByte(0x04); Assert.AreEqual(3, stream.Position); try { stream.Write(new byte[] {0x05, 0x06}, 0, 2); Assert.Fail("IOException not thrown"); } catch (IOException) { } Assert.AreEqual(3, stream.Position); stream.Write(new byte[] {0x05}, 0, 1); Assert.AreEqual(4, stream.Position); try { stream.WriteByte(0x06); Assert.Fail("IOException not thrown"); } catch (IOException) { } Assert.AreEqual(4, stream.Position); } Assert.AreEqual(new byte[] {0x00, 0x00, 0x02, 0x03, 0x04, 0x05, 0x00, 0x00}, inner.ToArray()); }
public void TestConstructReadOnlyWithWritableStream() { var readOnly = true; var inner = new MemoryStream(8); var stream = new PartialStream(inner, 4, 4, readOnly /*readonly*/, true); Assert.AreEqual(4, inner.Position); if (!inner.CanWrite) Assert.Fail("inner stream is not writable"); Assert.IsFalse(stream.CanWrite); try { stream.Write(new byte[] {0x00, 0x01, 0x02, 0x03}, 0, 4); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { } try { stream.WriteByte(0x00); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { } try { stream.Flush(); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { } }
public void TestWriteLengthNotSpecified() { var inner = new MemoryStream(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); using (var stream = new PartialStream(inner, 2, false)) { Assert.AreEqual(8, stream.InnerStream.Length); Assert.AreEqual(2, stream.InnerStream.Position); Assert.AreEqual(6, stream.Length); Assert.AreEqual(0, stream.Position); stream.Write(new byte[] {0x02, 0x03, 0x04}, 0, 3); stream.WriteByte(0x05); Assert.AreEqual(4, stream.Position); try { stream.Write(new byte[] {0x06, 0x07, 0x08}, 0, 3); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { // cannot expand MemoryStream } Assert.AreEqual(4, stream.Position); stream.Write(new byte[] {0x06, 0x07}, 0, 2); Assert.AreEqual(6, stream.Position); try { stream.WriteByte(0x08); Assert.Fail("NotSupportedException not thrown"); } catch (NotSupportedException) { // cannot expand MemoryStream } Assert.AreEqual(6, stream.Position); } Assert.AreEqual(new byte[] {0x00, 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, inner.ToArray()); }