Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Blocking Stream.Read

Blocking Stream.Read

Scheduled Pinned Locked Moved C#
comquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HosamAly
    wrote on last edited by
    #1

    Hello, I'm using Stream.Read(byte[] buffer, int offset, int count). Is there an alternative to that method (or a property to set) so that the method won't return until all count is read (or end of stream is reached)? Or should I do something like this:

    int n = 0, readCount = 0;
    while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
    readCount += n;

    My LinkedIn Profile

    G 1 Reply Last reply
    0
    • H HosamAly

      Hello, I'm using Stream.Read(byte[] buffer, int offset, int count). Is there an alternative to that method (or a property to set) so that the method won't return until all count is read (or end of stream is reached)? Or should I do something like this:

      int n = 0, readCount = 0;
      while ((n = myStream.Read(buffer, readCount, countToRead - readCount)) > 0)
      readCount += n;

      My LinkedIn Profile

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      No, you can't force it to read the number of bytes that you request. You have to use a loop with the Read method if you want to read the entire stream. You should check if the return value from the method is zero, not only if the total number of bytes has reached the expected. If the stream happens to be smaller than you expect, your code will go into an eternal loop.

      Despite everything, the person most likely to be fooling you next is yourself.

      H 1 Reply Last reply
      0
      • G Guffa

        No, you can't force it to read the number of bytes that you request. You have to use a loop with the Read method if you want to read the entire stream. You should check if the return value from the method is zero, not only if the total number of bytes has reached the expected. If the stream happens to be smaller than you expect, your code will go into an eternal loop.

        Despite everything, the person most likely to be fooling you next is yourself.

        H Offline
        H Offline
        HosamAly
        wrote on last edited by
        #3

        Yes, I edited the post and checked the return value, probably at the same time you were writing. :) Thanks.

        My LinkedIn Profile

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups