473,325 Members | 2,771 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,325 software developers and data experts.

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("output.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 3406
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("output.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 SeekingBinaryReader 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 BinaryReaderWriter - 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.GetString to convert.

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

Hmmm... if you seek each time (not that you can with BinaryReader -
I've got a SeekingBinaryReader 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 BinaryReaderWriter - it's fairly
straightforward to do - which would obviate some of the problems.
Thanks for responding. Do I really need to get special
SeekingBinaryReader and BinaryReaderWriter classes?
Can I just seek directly on the underlying stream, with
br.BaseStream.Seek(), or is this unreliable?

Can I possibly use br.BaseStream.Flush() 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("output.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.GetString to convert.

Nov 15 '05 #3

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

Similar topics

1
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...
7
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...
15
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 =...
1
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...
3
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...
5
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...
28
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...
0
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...
23
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.