473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

MemoryStream and BinaryWriter buffering question

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 stream has been closed.

But later it says:

Attempting to manipulate a stream after it has been
closed might throw an ObjectDisposedException.

So what's the correct idiom to do write and then extract
binary data from a MemoryStream:

MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(123);
bw.Write("hello, world");
bw.Close(); // should this be flush instead???
byte[] data = ms.ToArray();
ms.Close();

Also, does one typically need to Dispose() a MemoryStream to
free it's internal buffer? (Doesn't bw.Close() call ms.Close()
so it can't free the buffer?)
Thanks,
Keith

Nov 16 '05 #1
1 8014
keithv <kv*****@gmail.com> wrote:
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 stream has been closed.
Yup.
But later it says:

Attempting to manipulate a stream after it has been
closed might throw an ObjectDisposedException.
Yup. All of the normal Stream methods will fail if you call them.
So what's the correct idiom to do write and then extract
binary data from a MemoryStream:
Call ToArray after closing the MemoryStream.
Also, does one typically need to Dispose() a MemoryStream to
free it's internal buffer? (Doesn't bw.Close() call ms.Close()
so it can't free the buffer?)


There's no real *need* to close either a MemoryStream or a
BinaryWriter, but I think it's good form to use a using statement to
dispose of both - that way if you change at a later date to use
something that really *does* need disposing, it will fit into the same
code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

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...
4
by: Paul Steele | last post by:
I am writing a client/server application that communicates over tcp. The code I use to initiate the connection is as follows: NetworkStream networkStream = new NetworkStream(ClientSocket);...
0
by: Chris Bond | last post by:
Hi, I've got the following code that works perfectly, all it does is grab a image (binary file) from a mssql db and writes it out to the filesystem. I then use a commercial object that reads...
3
by: Nuno Monteiro | last post by:
Hello! Anyone knows how to open the correct aplication to see de document that is in MemoryStream? Like: I've got an pdf in MemoryStream ( I use BinaryWriter to write it to MemoryStream), and...
11
by: Rasmus Teglgaard | last post by:
Hi I need to know how to store the OpenfileDialog.OpenFile() in a memorystream. Do I need to do some kind of conversion before this works I can't write dim mem as new memorystream( mem =...
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: vbdotnetmania | last post by:
I have a memorystream of a bitmap, I want to send this over the network to a client app so it can receive the bitmap, I am dabbling with sockets and tcpclient's and ahve got a connection going but...
2
by: John A Grandy | last post by:
Dim ms As MemoryStream = New MemoryStream(100) ms.WriteByte("=") triggers invalid cast error ... but ms.WriteByte(Asc("="c))
3
by: =?Utf-8?B?VmljdG9y?= | last post by:
Hi, Could you tell me can I keep the MemoryStream open and "close" the BinaryReader? As the MemoryStream is used for buffering the TCP data and BinaryReader is only used to read the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.