> So I've got a class that wants to write out an array of uints to a Stream.
> It seems that in the most general case I am forced to copy my uint[] into a
[quoted text clipped - 3 lines]
>
> Or am I just overlooking something obvious?
Thanks for the reply.
Yes, a "higher order" stream might help -- but this particular class deals
in plain IO.Stream objects -- preserving the ability of the caller to use a
FileStream or a MemoryStream or...
I am in fact currently using System.Buffer to get the data out into a byte
array for this.
My point was that it seems kinda broken to have to all this copying just to
get around the type system.
Thanks again though!
> Ted,
> Rather then use a System.IO.Stream directly have you looked at using a
[quoted text clipped - 21 lines]
> >
> > Or am I just overlooking something obvious?
Jay B. Harlow [MVP - Outlook] - 04 Nov 2003 00:30 GMT
Ted,
> Yes, a "higher order" stream might help -- but this particular class deals
> in plain IO.Stream objects -- preserving the ability of the caller to use a
> FileStream or a MemoryStream or...
I hope you realize that your class can create a StreamWriter or a
BinaryWriter based on the plain IO.Stream variables. In fact a BinaryWriter
needs a previously open IO.Stream in order to use it. A StreamWriter can
accept either a file name or a Io.Stream in its constructor.
Which is part of the beauty of the System.IO namespace and having the
IO.Stream independent of the BinaryWriter & StreamWriter classes. The
writers are not tied to a specific stream & the stream is not tied to a
specific writer!
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemIOBinaryWriterClassctorTopic2.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemIOStreamWriterClassctorTopic1.asp
The trick is making sure that you only flush the writer without closing the
underlying Stream.
Something like:
Public Sub Main()
Dim stream As New FileStream("myfile.txt", FileMode.Create)
Line1(stream)
Line2(stream)
stream.Close()
End Sub
Public Sub Line1(ByVal stream As Stream)
Dim writer As New StreamWriter(stream)
writer.WriteLine("This is the first line")
writer.Flush()
End Sub
Public Sub Line2(ByVal stream As Stream)
Dim writer As New StreamWriter(stream)
writer.WriteLine("This is the second line")
writer.Flush()
End Sub
What I'm not sure is how calling StreamWriter.Flush relates to the
underlying Encoding object associated with the StreamWriter. Based on the
help for StreamWriter.Flush.
Hope this helps
Jay
> Thanks for the reply.
>
[quoted text clipped - 38 lines]
> > >
> > > Or am I just overlooking something obvious?