Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2003

Tip: Looking for answers? Try searching our database.

Stream I/O: what about arrays of records, etc (why only byte[])?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ted Miller - 03 Nov 2003 12:50 GMT
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
byte[] just so I can call Stream.Write. If there was an overload of
Stream.Write that took a System.Array, an index, and a count, this would be
trivially simple.

Or am I just overlooking something obvious?
Jay B. Harlow [MVP - Outlook] - 03 Nov 2003 19:55 GMT
Ted,
Rather then use a System.IO.Stream directly have you looked at using a
System.IO.StreamWriter or a System.IO.BinaryWriter.

As the higher order Writer objects accept higher order objects an properly
convert/encode them for the underlying stream.

Note you may still need to iterate over your array and writer out each array
element.

The System.Buffer class is useful to take an array of ints or uints & copy
to an array of bytes.

Hope this helps
Jay

> 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?
Ted Miller - 03 Nov 2003 21:52 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.