[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

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

Ted Miller

11/3/2003 12:50:00 PM

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?


3 Answers

Jay B. Harlow [MVP - Outlook]

11/3/2003 7:56:00 PM

0

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

"Ted Miller" <ted@nwlink.com> wrote in message
news:vqcjnmigknr181@corp.supernews.com...
> 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?
>
>


Ted Miller

11/3/2003 9:52:00 PM

0

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!


"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in message
news:OXvNASkoDHA.2512@TK2MSFTNGP09.phx.gbl...
> 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
>
> "Ted Miller" <ted@nwlink.com> wrote in message
> news:vqcjnmigknr181@corp.supernews.com...
> > 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]

11/4/2003 12:31:00 AM

0

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/frlrfSystemIOBinaryWriterClassctor...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOStreamWriterClassctor...

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

"Ted Miller" <ted@nwlink.com> wrote in message
news:vqdjfqmslbkj82@corp.supernews.com...
> 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!
>
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in
message
> news:OXvNASkoDHA.2512@TK2MSFTNGP09.phx.gbl...
> > 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
> >
> > "Ted Miller" <ted@nwlink.com> wrote in message
> > news:vqcjnmigknr181@corp.supernews.com...
> > > 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?
> > >
> > >
> >
> >
>
>