Reading & Writing in a Stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ali

    Reading & Writing in a Stream

    Hi

    I want to write (or read) to a stream, but the data is not byte array
    I converted the data to byte array manually, but it is very slow, (becuse the data is very large)
    Is another way for this kind of writing and reading

    best regard
    Ali.
  • I.hate,spam

    #2
    Re: Reading & Writing in a Stream

    Ali wrote:[color=blue]
    > Hi,
    >
    > I want to write (or read) to a stream, but the data is not byte array.
    > I converted the data to byte array manually, but it is very slow, (becuse the data is very large).
    > Is another way for this kind of writing and reading?
    >
    > best regards
    > Ali.[/color]

    Not all streams require byte arrays. Maybe try a StreamWriter for disk
    output, or a StringWriter for writing to a string.

    Comment

    • Cor

      #3
      Re: Reading & Writing in a Stream

      Hi Ali,

      Are you talking about a stream to disk, than you can just do it as strings.
      (unicode is standard)

      I hope this helps,

      Cor[color=blue]
      >
      > I want to write (or read) to a stream, but the data is not byte array.
      > I converted the data to byte array manually, but it is very slow, (becuse[/color]
      the data is very large).[color=blue]
      > Is another way for this kind of writing and reading?
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Reading & Writing in a Stream

        I.hate,spam <"I.hate,spam " <"I.hate,spam"@ my.email>> wrote:[color=blue]
        > Not all streams require byte arrays. Maybe try a StreamWriter for disk
        > output, or a StringWriter for writing to a string.[/color]

        Those aren't actually streams though - they're TextWriters, which are
        sort of the *equivalent* of streams, but for text rather than binary
        data.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Reading &amp; Writing in a Stream

          Cor <non@non.com> wrote:[color=blue]
          > Are you talking about a stream to disk, than you can just do it as strings.
          > (unicode is standard)[/color]

          No, streams deal solely with binary data. To convert between text data
          and binary data, you need to use an encoding.

          See http://www.pobox.com/~skeet/csharp/unicode.html

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Cor

            #6
            Re: Reading &amp; Writing in a Stream

            Jon,
            [color=blue]
            > No, streams deal solely with binary data. To convert between text data
            > and binary data, you need to use an encoding.[/color]

            This is maybe true, but the streamreader can be used to write direct data to
            disk. You do not need to convert that first to a byte array. I explicitly
            said "disk"

            Cor



            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Reading &amp; Writing in a Stream

              Cor <non@non.com> wrote:[color=blue][color=green]
              > > No, streams deal solely with binary data. To convert between text data
              > > and binary data, you need to use an encoding.[/color]
              >
              > This is maybe true, but the streamreader can be used to write direct data to
              > disk.[/color]

              Yes, but a StreamWriter isn't a Stream.
              [color=blue]
              > You do not need to convert that first to a byte array. I explicitly
              > said "disk"[/color]

              Actually, the disk part here is irrelevant, as a StreamWriter can write
              to any stream - a NetworkStream, MemoryStream, FileStream, whatever.
              The important thing is that you don't write strings to a Stream though
              - you write them to a TextWriter (such as StreamWriter) which might
              well in turn write bytes to an underlying Stream.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Cor

                #8
                Re: Reading &amp; Writing in a Stream

                Hi Jon,[color=blue]
                >
                > Actually, the disk part here is irrelevant, as a StreamWriter can write
                > to any stream - a NetworkStream, MemoryStream, FileStream, whatever.
                > The important thing is that you don't write strings to a Stream though
                > - you write them to a TextWriter (such as StreamWriter) which might
                > well in turn write bytes to an underlying Stream.[/color]

                Do you mind if I ask you to read this yourself again, without any comments
                of me.

                :-)

                Cor


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Reading &amp; Writing in a Stream

                  Cor <non@non.com> wrote:[color=blue][color=green]
                  > > Actually, the disk part here is irrelevant, as a StreamWriter can write
                  > > to any stream - a NetworkStream, MemoryStream, FileStream, whatever.
                  > > The important thing is that you don't write strings to a Stream though
                  > > - you write them to a TextWriter (such as StreamWriter) which might
                  > > well in turn write bytes to an underlying Stream.[/color]
                  >
                  > Do you mind if I ask you to read this yourself again, without any comments
                  > of me.[/color]

                  Sure. Have done. No problem with it as far as I can see.

                  A StreamWriter is a TextWriter. TextWriters deal with text, not binary
                  data - and a StreamWriter converts the text it is given *into* binary
                  data, which the stream it's created with can write. A stream itself
                  *only* deals with binary, not text.

                  The OP wants to know how to write data to a *stream*, so he can't do it
                  as strings - not without a StreamWriter or manually calling
                  Encoding.GetByt es etc.

                  Which bit of my post did you actually disagree with? Why do you feel
                  it's relevant whether or not the OP is writing to a disk?

                  (I missed something from your previous post, btw - a Stream*Reader*
                  doesn't write *anything* - it only *reads*.)

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Cor

                    #10
                    Re: Reading &amp; Writing in a Stream

                    Hi Jon,

                    A newsgroup keep the messages open this is what you tell that is the
                    question from the OP[color=blue]
                    > The OP wants to know how to write data to a *stream*, so he can't do it
                    > as strings - not without a StreamWriter or manually calling
                    > Encoding.GetByt es etc.
                    >[/color]

                    This is the question from the OP (copied from his message)
                    I want to write (or read) to a stream, but the data is not byte array.
                    I converted the data to byte array manually, but it is very slow, (becuse
                    the data is very large).
                    Is another way for this kind of writing and reading?
                    ---------------------------------------------------------
                    I do not know if he means to mem. to http or to disk. Therefore I gave the
                    answer that if it is to disk, it is not necessary to make first that
                    bytearray.

                    I did not tell how, that could be done when he had asked for that or maybe
                    he had answered that it was not to disk and than I had given another answer.

                    Cor


                    Comment

                    • Cor

                      #11
                      Re: Reading &amp; Writing in a Stream

                      Hi Jon,

                      I forgot to thank you for this greath answer.
                      [color=blue]
                      > (I missed something from your previous post, btw - a Stream*Reader*
                      > doesn't write *anything* - it only *reads*.)[/color]

                      I hope that I do not disappoint you, when I tell you that I did know that
                      already but made a typo.

                      :-)

                      Cor


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Reading &amp; Writing in a Stream

                        Cor <non@non.com> wrote:[color=blue]
                        > A newsgroup keep the messages open this is what you tell that is the
                        > question from the OP[/color]
                        [color=blue][color=green]
                        > > The OP wants to know how to write data to a *stream*, so he can't do it
                        > > as strings - not without a StreamWriter or manually calling
                        > > Encoding.GetByt es etc.
                        > >[/color]
                        >
                        > This is the question from the OP (copied from his message)
                        > I want to write (or read) to a stream, but the data is not byte array.
                        > I converted the data to byte array manually, but it is very slow, (becuse
                        > the data is very large).
                        > Is another way for this kind of writing and reading?
                        > ---------------------------------------------------------
                        > I do not know if he means to mem. to http or to disk. Therefore I gave the
                        > answer that if it is to disk, it is not necessary to make first that
                        > bytearray.[/color]

                        Why though? How would you do things differently if it's writing to a
                        disk compared with writing anywhere else?
                        [color=blue]
                        > I did not tell how, that could be done when he had asked for that or maybe
                        > he had answered that it was not to disk and than I had given another answer.[/color]

                        Well, you said you could write strings directly using a stream, which
                        you can't - you need to wrap it with a StreamWriter first, and that's
                        true whether the stream is to memory, the network or wherever.

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        • Cor

                          #13
                          Re: Reading &amp; Writing in a Stream

                          Hi Jon,

                          [color=blue]
                          > Well, you said you could write strings directly using a stream, which
                          > you can't - you need to wrap it with a StreamWriter first, and that's
                          > true whether the stream is to memory, the network or wherever.[/color]

                          Show me where I wrote that above, this is the X th time you are telling from
                          people what they said, while they did not.

                          So copy it where I wrote that and show it.

                          But as usualy you answer this not anymore because you cannot.

                          Cor


                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Reading &amp; Writing in a Stream

                            Cor <non@non.com> wrote:[color=blue][color=green]
                            > > Well, you said you could write strings directly using a stream, which
                            > > you can't - you need to wrap it with a StreamWriter first, and that's
                            > > true whether the stream is to memory, the network or wherever.[/color]
                            >
                            > Show me where I wrote that above, this is the X th time you are telling from
                            > people what they said, while they did not.
                            >
                            > So copy it where I wrote that and show it.
                            >
                            > But as usualy you answer this not anymore because you cannot.[/color]

                            My mistake, you didn't use the word "directly". However, you did say:

                            <quote>
                            Are you talking about a stream to disk, than you can just do it as
                            strings.
                            </quote>

                            Which you can't. You can't (directly - my word) write a string to a
                            stream. You *have* to go through a conversion process (which is what
                            the OP apparently wanted to avoid) whether that's done directly using
                            Encoding or using StreamWriter. It doesn't matter whether you're
                            writing to a disk or not.

                            Your post gave the impression (to me at least) that you thought you
                            could write strings directly to a stream, which you can't.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                            If replying to the group, please do not mail me too

                            Comment

                            • Cor

                              #15
                              Re: Reading &amp; Writing in a Stream

                              Hi Jon,
                              [color=blue]
                              >
                              > Your post gave the impression (to me at least) that you thought you
                              > could write strings directly to a stream, which you can't.
                              >[/color]

                              Did you see me write here that I thought that you said that it was only
                              possible to write bytearrays to disk?

                              That I could have been writing very easy by what you where telling and than
                              telling to you that you can only write bytes to a disk.

                              I expect that you know those basic things.


                              Cor


                              Comment

                              Working...