473,466 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

BinaryWriter, don't close stream

Can I use a BinaryWriter without it closing the underlying stream?

I want to save information to a MemoryStream using a BinaryWriter but I want
to dispose of the BinaryWriter and keep the MemoryStream in case I wish to
reload information from the MemoryStream using BinaryReader.


Thanks

Pete
Oct 23 '07 #1
7 5174
Peter,

No, you can't, but you can get the contents of the MemoryStream before
the BinaryWriter is finished by calling ToArray on the MemoryStream. You
can then pass this to a new MemoryStream when you want to use it in a
BinaryReader again.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Morris" <su*****@NOdroopySPAMeyes.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Can I use a BinaryWriter without it closing the underlying stream?

I want to save information to a MemoryStream using a BinaryWriter but I
want to dispose of the BinaryWriter and keep the MemoryStream in case I
wish to reload information from the MemoryStream using BinaryReader.


Thanks

Pete

Oct 23 '07 #2
Hi,

did you try it?

IIRC Close close the underlined stream. but as you are using a MemoryStream,
well "closing" it has not too much sense.

I would give it a try and post back if you have any problem.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Peter Morris" <su*****@NOdroopySPAMeyes.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Can I use a BinaryWriter without it closing the underlying stream?

I want to save information to a MemoryStream using a BinaryWriter but I
want to dispose of the BinaryWriter and keep the MemoryStream in case I
wish to reload information from the MemoryStream using BinaryReader.


Thanks

Pete

Oct 23 '07 #3
>Can I use a BinaryWriter without it closing the underlying stream?

I want to save information to a MemoryStream using a BinaryWriter but I want
to dispose of the BinaryWriter and keep the MemoryStream in case I wish to
reload information from the MemoryStream using BinaryReader.

What I've done in the past is to create a proxy Stream class that does
nothing in its Close method, and just forwards all other method calls
to the stream it wraps. Then use it something like

new BinaryWriter(new NonClosingStream(yourActualStream))
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 23 '07 #4
Jon provides such a stream in his "misc utils" library:

http://www.yoda.arachsys.com/csharp/miscutil/

It might not appear on the write-up, but I'm pretty sure it is
there... NonClosingStream or some-such; essentially acts as a
"decorator" for a stream and ignores Close() and Dispose() [it might
call Flush() instead, I can't recall]

Marc
Oct 23 '07 #5
Marc Gravell <ma**********@gmail.comwrote:
http://.../miscutil/

Alternative url: http://www.pobox.com/~skeet/csharp/miscutil/
And yes, it's there. (I nearly posted about it myself, but thought I
might as well let you. After all, you seem to know MiscUtil better than
I do half the time!)

NonClosingStreamWrapper is the type in question. I'll put it on the
list some time :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 23 '07 #7
My problem is that I want a MyClass.SaveToStream

but as the stream is passed to me I have no (right) to close it. This is
because I have no knowledge of what the application wants to do with the
stream. For example, the app might save 4 objects to the same stream but
the first object will close the stream. It's a bit silly really isn't it
:-) Anyway, I just changed it to

SaveToStream(BinaryWriter writer);

Pete
Nov 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
6
by: ThunderMusic | last post by:
Hi, In my app, I open a file using a FileStream then pass it to a BinaryWriter. I then use the BinaryWriter instance to write to my file. But a problem arose : The file never gets bigger than 1kb....
17
by: Filip Strugar | last post by:
Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter...
1
by: Claire | last post by:
I'm writing from a serial port into a MemoryStream via a BinaryWriter in my protocol object. After I've filled the memorystream, which is approx 200KB in size, I then pass it up to my datahandler...
1
by: Barguast | last post by:
Is it necessary to call the close method for every BinaryWriter that I create? Or is it just a way to close the underlying stream? For example is it OK to do the following: private void...
5
by: Boris | last post by:
// Example BinaryWriter bw = new BinaryWriter(new FileStream("c:\Test.bin",FileMode.Append)); BinaryWriter.Write writes data to the stream. However, it doesn't automatically flush data any...
1
by: keithv | last post by:
Hi, The msdn doc for MemoryStream has two conflicting statements about accessing a MemoryStream's buffer after it's been closed: The buffer is still available on a MemoryStream once the...
1
by: Lisa | last post by:
Using C# .NET, I want to create a BinaryWriter to read Int32 data from a file, but I don't see any facility to set the file pointer to a specific point within the file. Thanks for any help. ...
3
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.