Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default File.IO

    I have an open FileStream and I open a BinaryWriter on it. After writing to
    this file, I want to read it to another stream.

    FileStream fs = new FileStream(Path.GetTempFileName());
    BinaryWriter bw = new BinaryWriterer(fs);

    // write data to a filestream

    bw.Close();
    fs.Position = 0; // ** Exception, Object is disposed
    BinaryReader br = new BinaryReader(fs);

    // read file and send to another stream

    However, apparently closing the BinaryWriter closes the FileStream. I don't
    understand why closing a writer on a stream closes the stream itself.
    What's the best way to accomplish what I am trying to do?

    TIA~ PJ


    PJ Guest

  2. Similar Questions and Discussions

    1. problem in binding xml file data to datagrid xml file isgenerated through JSP file
      problem it that i am creating xml file using JSP file and i want to bind DataGrid with xml file data that is created using JSP but it will not Bind...
    2. File Viewer / Bloated file sizes / What is the best file format?
      I would like to find a viewer capable of looking at the main Adobe formats as well as the standard formats such as JPG and WMF ... but yet the only...
    3. Open file, make changes, save file, close, re-open, file contents not changed
      I've now run into this several times and it's completely destroyed all of my confidence in Ilustrator CS on Mac. I'm hoping someone can confirm that...
    4. [BUG] File#rewind, File#syswrite, File#pos on Cygwin build
      On the cygwin build of ruby v1.8.0, I have encountered a strange bug when using rewind, syswrite and pos. If you open a file in read/write mode,...
    5. Confused about locking a file via file.flock(File::LOCK_EX)
      I am writing a ruby appl under AIX where I need to update the /etc/hosts table. I would like to make sure that during my update nobody else can...
  3. #2

    Default Re: File.IO

    PJ <pjwal@hotmail.com> wrote:
    > I have an open FileStream and I open a BinaryWriter on it. After writing to
    > this file, I want to read it to another stream.
    >
    > FileStream fs = new FileStream(Path.GetTempFileName());
    > BinaryWriter bw = new BinaryWriterer(fs);
    >
    > // write data to a filestream
    >
    > bw.Close();
    > fs.Position = 0; // ** Exception, Object is disposed
    > BinaryReader br = new BinaryReader(fs);
    >
    > // read file and send to another stream
    >
    > However, apparently closing the BinaryWriter closes the FileStream. I don't
    > understand why closing a writer on a stream closes the stream itself.
    Because for almost all the time, that's actually what you want. At
    least, it's almost always what *I* want :)
    > What's the best way to accomplish what I am trying to do?
    Hmm... not sure. It would be fairly easy to write a
    NonClosingFilterStream or something similar, which passes through all
    method calls apart from Close. I *think* that would do it...

    --
    Jon Skeet - <skeet@pobox.com>
    [url]http://www.pobox.com/~skeet/[/url]
    If replying to the group, please do not mail me too
    Jon Skeet Guest

  4. #3

    Default Re: File.IO

    PJ,

    The behavior of the BinaryWriter class you are describing is by design. If
    you read the documentation it says: "[BinaryWriter.Close()] Closes the
    current BinaryWriter and the underlying stream.". Actually all the Close()
    method is doing is call the Dispose() method with true as only parameter.
    When the parameter equal true the Dispose() method closes the stream
    associated with it.

    If you delay closing the BinaryWriter object you will be able to read from
    the file via the BinaryReader object created from the same file stream.

    A more complex solution would be to create a new class inheriting from
    BinaryWriter, and overload the Close() method so that it won't close the
    file stream. You will have to close it manually.

    Gabriele

    "PJ" <pjwal@hotmail.com> wrote in message
    news:%23Movw3HUDHA.1712@TK2MSFTNGP11.phx.gbl...
    > I have an open FileStream and I open a BinaryWriter on it. After writing
    to
    > this file, I want to read it to another stream.
    >
    > FileStream fs = new FileStream(Path.GetTempFileName());
    > BinaryWriter bw = new BinaryWriterer(fs);
    >
    > // write data to a filestream
    >
    > bw.Close();
    > fs.Position = 0; // ** Exception, Object is disposed
    > BinaryReader br = new BinaryReader(fs);
    >
    > // read file and send to another stream
    >
    > However, apparently closing the BinaryWriter closes the FileStream. I
    don't
    > understand why closing a writer on a stream closes the stream itself.
    > What's the best way to accomplish what I am trying to do?
    >
    > TIA~ PJ
    >
    >

    Gabriele G. Ponti Guest

  5. #4

    Default Re: File.IO

    Overrloading the Close() method w/ a boolean parameter seems like the least
    complex and the way they should have designed it to me. I guess I should
    read the documentation more, as I have always called .Close() on the stream
    as well.

    thx! PJ

    "Gabriele G. Ponti" <ggponti@hotmail.com> wrote in message
    news:epTfwtIUDHA.2252@TK2MSFTNGP12.phx.gbl...
    > PJ,
    >
    > The behavior of the BinaryWriter class you are describing is by design. If
    > you read the documentation it says: "[BinaryWriter.Close()] Closes the
    > current BinaryWriter and the underlying stream.". Actually all the Close()
    > method is doing is call the Dispose() method with true as only parameter.
    > When the parameter equal true the Dispose() method closes the stream
    > associated with it.
    >
    > If you delay closing the BinaryWriter object you will be able to read from
    > the file via the BinaryReader object created from the same file stream.
    >
    > A more complex solution would be to create a new class inheriting from
    > BinaryWriter, and overload the Close() method so that it won't close the
    > file stream. You will have to close it manually.
    >
    > Gabriele
    >
    > "PJ" <pjwal@hotmail.com> wrote in message
    > news:%23Movw3HUDHA.1712@TK2MSFTNGP11.phx.gbl...
    > > I have an open FileStream and I open a BinaryWriter on it. After
    writing
    > to
    > > this file, I want to read it to another stream.
    > >
    > > FileStream fs = new FileStream(Path.GetTempFileName());
    > > BinaryWriter bw = new BinaryWriterer(fs);
    > >
    > > // write data to a filestream
    > >
    > > bw.Close();
    > > fs.Position = 0; // ** Exception, Object is disposed
    > > BinaryReader br = new BinaryReader(fs);
    > >
    > > // read file and send to another stream
    > >
    > > However, apparently closing the BinaryWriter closes the FileStream. I
    > don't
    > > understand why closing a writer on a stream closes the stream itself.
    > > What's the best way to accomplish what I am trying to do?
    > >
    > > TIA~ PJ
    > >
    > >
    >
    >

    PJ Guest

  6. #5

    Default Re: File.IO

    > Because for almost all the time, that's actually what you want. At
    > least, it's almost always what *I* want :)
    It occurred to me that maybe it sounds like I'm trying to write a virus,
    lol. The reason I need to do this is that I am compressing a set of files
    and then sending it out to an HTTP response stream. I was sending the
    compressed bytes directly to the stream as the compression was taking
    place.....but w/out the compression being complete, I have no way of setting
    the Content-Length header, so the d/l dialog box does not show a %
    completion ( unnacceptable I'm told ).

    thx! PJ


    PJ Guest

  7. #6

    Default Re: File.IO

    good call

    > Sounds like you should just ditch the BinaryWriter, and just use the
    > FileStream's write methods directly.
    >
    >
    > David
    >
    >

    PJ Guest

  8. #7

    Default Re: File.IO

    PJ <pjwal@hotmail.com> wrote:
    > > Because for almost all the time, that's actually what you want. At
    > > least, it's almost always what *I* want :)
    >
    > It occurred to me that maybe it sounds like I'm trying to write a virus,
    > lol. The reason I need to do this is that I am compressing a set of files
    > and then sending it out to an HTTP response stream. I was sending the
    > compressed bytes directly to the stream as the compression was taking
    > place.....but w/out the compression being complete, I have no way of setting
    > the Content-Length header, so the d/l dialog box does not show a %
    > completion ( unnacceptable I'm told ).
    In that case, given that effectively you've *got* to buffer the whole
    thing anyway, you could just write to a MemoryStream first, find the
    length of that, and then dump the contents of the memory stream to the
    real response.

    --
    Jon Skeet - <skeet@pobox.com>
    [url]http://www.pobox.com/~skeet/[/url]
    If replying to the group, please do not mail me too
    Jon Skeet Guest

  9. #8

    Default Re: File.IO

    I don't understand what you're saying, I don't have to buffer the whole file
    at all. The Content-Length header is the length of the content only, not
    the whole response including the headers. The fs.Length property gives me
    the value for that header w/out buffering the whole file.

    "Jon Skeet" <skeet@pobox.com> wrote in message
    news:MPG.198840a8d5c5df0b98a1af@news.microsoft.com ...
    > PJ <pjwal@hotmail.com> wrote:
    > > > Because for almost all the time, that's actually what you want. At
    > > > least, it's almost always what *I* want :)
    > >
    > > It occurred to me that maybe it sounds like I'm trying to write a virus,
    > > lol. The reason I need to do this is that I am compressing a set of
    files
    > > and then sending it out to an HTTP response stream. I was sending the
    > > compressed bytes directly to the stream as the compression was taking
    > > place.....but w/out the compression being complete, I have no way of
    setting
    > > the Content-Length header, so the d/l dialog box does not show a %
    > > completion ( unnacceptable I'm told ).
    >
    > In that case, given that effectively you've *got* to buffer the whole
    > thing anyway, you could just write to a MemoryStream first, find the
    > length of that, and then dump the contents of the memory stream to the
    > real response.
    >
    > --
    > Jon Skeet - <skeet@pobox.com>
    > [url]http://www.pobox.com/~skeet/[/url]
    > If replying to the group, please do not mail me too

    PJ Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139