How do I get out what I put into a stream?

I am surprised that this unit test fails. It fails with this message:
expected: <"expected">
but was: <"expected
I believe it's a null that's causing the failure. The second string is one character longer than the first.
Here's the unit test:
<Test> Public Sub StreamTest()
Dim sr As New StreamReader( New MemoryStream )
Dim sw As New StreamWriter( sr.BaseStream )
sw.AutoFlush = True
Dim expected As String = "expected"
sw.BaseStream.Position = 0
sw.Write( expected )
Dim chars( expected.Length ) As Char
sr.BaseStream.Position = 0
Dim length As Integer
length = sr.Read( chars, 0, chars.Length )
Dim result( length ) As Char
For i As Integer = 0 To length - 1
result(i) = chars(i)
Next
Assert.AreEqual( expected, New string( result ) )
End Sub
[874 byte] By [BobVilla] at [2007-12-16]
# 1

I believe you are missing a flush in there. The AutoFlush property only flushes the StreamWriter to it's base stream. It does not call flush on the base stream. If you call sw.BaseStream.Flush() before you reset the position of the stream it may work for you. You might want to throw some error-handling and a Close in there too.

DavidBanister at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified