473,785 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

two objects contain same stream?

Hi,

Any advice about both a BinaryReader and BinaryWriter
containing same FileStream at same time? Like:

Stream fs = new FileStream("out put.dbf", FileMode.Create );
BinaryReader br = new BinaryReader(fs );
BinaryWriter bw = new BinaryWriter(fs );
/* BinaryWriter writes, flushes, etc */
/* BinaryReader seeks, reads, etc */
/* BinaryWriter seeks, writes, etc */

Same question for StreamReader and StreamWriter containing
same FileStream at same time.

Also, is it asking for trouble to have combination of text
and binary writers (SW and BW) contain and fiddle with same
FS at the same time? I could change some file formats instead.

FS object doesn't know which objects contain it, doesn't care
how many containing objects call methods on it. As long as I
make sure no other process is accessing the Stream when it is
being written to, there will be no problem. Correct?

Any books/tutorials that discuss multiple objects containing
same stream at same time?

Thanks,
Daniel Goldman
Seattle, WA

Background - I'm porting a C program to C#. It reads and
writes various binary and text formats. I want to make C#
non-interface program flow parallel existing C logic (I'm not
discarding the C program). I've read and experimented a fair
amount with System.IO, but I have not found mention of
multiple objects containing same stream, which would make
it easier to mimic C IO logic. It seems to work, but I'd
like to know more before going further.
Nov 15 '05 #1
2 3432
Daniel Goldman <hh******@yahoo .com> wrote:
Any advice about both a BinaryReader and BinaryWriter
containing same FileStream at same time? Like:

Stream fs = new FileStream("out put.dbf", FileMode.Create );
BinaryReader br = new BinaryReader(fs );
BinaryWriter bw = new BinaryWriter(fs );
/* BinaryWriter writes, flushes, etc */
/* BinaryReader seeks, reads, etc */
/* BinaryWriter seeks, writes, etc */

Same question for StreamReader and StreamWriter containing
same FileStream at same time.
Hmmm... if you seek each time (not that you can with BinaryReader -
I've got a SeekingBinaryRe ader if you'd like) it should be okay. Bear
in mind that readers may buffer data, so you need to make sure you seek
through the enclosing objects rather than directly on the underlying
stream, otherwise it might get confused. If I were you, I'd be quite
tempted to write my own BinaryReaderWri ter - it's fairly
straightforward to do - which would obviate some of the problems.
Also, is it asking for trouble to have combination of text
and binary writers (SW and BW) contain and fiddle with same
FS at the same time? I could change some file formats instead.


While I would generally consider it a bad idea, it's not as problematic
as with readers, as you can simply flush after each operation.

Personally I would try to make sure that in your binary format, you
know which sections represent text, read them as a byte array, and then
use Encoding.GetStr ing to convert.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet <sk***@pobox.co m> wrote in message news:<MP******* *************** **@news.microso ft.com>...

Hmmm... if you seek each time (not that you can with BinaryReader -
I've got a SeekingBinaryRe ader if you'd like) it should be okay. Bear
in mind that readers may buffer data, so you need to make sure you seek
through the enclosing objects rather than directly on the underlying
stream, otherwise it might get confused. If I were you, I'd be quite
tempted to write my own BinaryReaderWri ter - it's fairly
straightforward to do - which would obviate some of the problems.
Thanks for responding. Do I really need to get special
SeekingBinaryRe ader and BinaryReaderWri ter classes?
Can I just seek directly on the underlying stream, with
br.BaseStream.S eek(), or is this unreliable?

Can I possibly use br.BaseStream.F lush() to reliably take care
of the reader buffering data? The documentation for Flush says:

"If CanSeek is true and data was previously copied from the
file to the buffer for reading, the current position within
the file is decremented by the number of unread bytes in the
buffer. The buffer is then cleared."

CanSeek is true. What does it mean "the buffer is then cleared"?
Cleared to where? Hopefully not to the bit bucket. Anybody know?
Daniel Goldman <hh******@yahoo .com> wrote:
Any advice about both a BinaryReader and BinaryWriter
containing same FileStream at same time? Like:

Stream fs = new FileStream("out put.dbf", FileMode.Create );
BinaryReader br = new BinaryReader(fs );
BinaryWriter bw = new BinaryWriter(fs );
/* BinaryWriter writes, flushes, etc */
/* BinaryReader seeks, reads, etc */
/* BinaryWriter seeks, writes, etc */

Same question for StreamReader and StreamWriter containing
same FileStream at same time.

Also, is it asking for trouble to have combination of text
and binary writers (SW and BW) contain and fiddle with same
FS at the same time? I could change some file formats instead.


While I would generally consider it a bad idea, it's not as problematic
as with readers, as you can simply flush after each operation.

Personally I would try to make sure that in your binary format, you
know which sections represent text, read them as a byte array, and then
use Encoding.GetStr ing to convert.

Nov 15 '05 #3

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

Similar topics

1
1221
by: Tom Rahav | last post by:
Hi! My question is whether it's possible to serialize an object into a binary file from one application, and then deserialize this object in a different application that has the same class. I tried to do so, but when I try to seserialize, I get an error message saying that the application doesn't contain a refference to the first application. Is it possible to serialize object in one app and deserialize it in a different one?
7
1648
by: nog | last post by:
What's the best approach to creating many objects of a class? I have in mind using something analogous to a table to hold the data - which is in a form similar to (char name, char address, date joinDate) - and being able to declare each row, one after another by looping. With about 30 instances to create, I feel there must be a better way than making an individual declaration for each one. TIA.
15
6795
by: zealotcat | last post by:
Hi, all: I want to implement a function (using std::string and std::vector) split a string contain special token into vector. eg: string = "alex delia john" ==> vector = "alex"; vector = "delia";
1
12063
by: BH | last post by:
I'm trying a simple object serialization and deserialization, and keep getting this error: System.Runtime.Serialization.SerializationException: Binary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization. Here's my code. it does nothing but to serialize a DataTable object into a byte array, and then read the byte array back for...
3
2290
by: Peter Rilling | last post by:
Is there anyway of being notified when a Stream is modified? Basically, If one of my objects is passed a stream, I would like to initialize an object cache. Then when the Stream changes, I would like to update the cache. Does not look like the stream has any event that I can hook.
5
13467
by: Ahmad Jalil Qarshi | last post by:
Hi! I have a class named CClassToSerialize that contain some boolean, string and few custom variable types. Now when I create only a single Object and Serialize/Deserialize everything is fine. But I want to save more than one Object of this class into same file.
28
1609
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something like that?
0
1928
by: Vince Filby | last post by:
Hi, We are working with distributing Lucene.net. We have Master Index Server which takes responsibility of distributing the index searching to multiple Index Servers by calling the remote method Search(...) of the Index Server. It is when the Master Server de-serializing the response message from the Index Server, we got the following SerializationException. The Search method is overloaded:
23
5741
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.