Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6693227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:57:30+00:00 2026-05-26T05:57:30+00:00

It says here msdn.microsoft.com/en-us/library/system.io.stream.read.aspx that the Stream.Read and Stream.Write methods both advance the position/offset

  • 0

It says here msdn.microsoft.com/en-us/library/system.io.stream.read.aspx that the Stream.Read and Stream.Write methods both advance the position/offset in the stream automatically so why is the examples here http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx and http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx manually changing the offset?

Do you only set the offset in a loop if you know the size of the stream and set it to 0 if you don’t know the size and using a buffer?

   // Now read s into a byte buffer.
    byte[] bytes = new byte[s.Length];
    int numBytesToRead = (int) s.Length;
    int numBytesRead = 0;
    while (numBytesToRead > 0)
    {
        // Read may return anything from 0 to 10.
        int n = s.Read(bytes, numBytesRead, 10);
        // The end of the file is reached.
        if (n == 0)
        {
            break;
        }
        numBytesRead += n;
        numBytesToRead -= n;
    }

and

using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
    const int size = 4096;
    byte[] buffer = new byte[size];
    using (MemoryStream memory = new MemoryStream())
    {
    int count = 0;
    do
    {
        count = stream.Read(buffer, 0, size);
        if (count > 0)
        {
        memory.Write(buffer, 0, count);
        }
    }
    while (count > 0);
    return memory.ToArray();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:57:30+00:00Added an answer on May 26, 2026 at 5:57 am

    Edit (to the edited question):

    In none of the code snippets you pasted into the question I see any stream offset being set.

    I think you are mistaking the calculation of bytes to read vs. bytes received. This protocol may seem funny (why would you receive fewer bytes than requested?) but it makes sense when you consider that you might be reading from a high-latency packet oriented source (think: network sockets).

    You might be receiving 6 characters in one burst (from a TCP packet) and only receive the remaining 4 characters in your next read (when the next packet has arrived).

    Edit In response to your linked example from the comment:

    using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
        { 
           // ... snip
    
            count = stream.Read(buffer, 0, size);
            if (count > 0)
            {
            memory.Write(buffer, 0, count);
            }
    

    It appears that the coders use prior knowledge about the underlying stream implementation, that stream.Read will always return 0 OR the size requested. That seems like a risky bet, to me. But if the docs for GZipStream do state that, it could be alright. However, since the MSDN samples use a generic Stream variable, it is (way) more correct to check the exact number of bytes read.


    The first linked example uses a MemoryStream in both Write and Read fashion. The position is reset in between, so the data that was written first will be read:

        Stream s = new MemoryStream();
        for (int i = 0; i < 100; i++)
        {
            s.WriteByte((byte)i);
        }
        s.Position = 0;
    

    The second example linked does not set the stream position. You’d typically have seen a call to Seek if it did. You maybe confusing the offsets into the data buffer with the stream position?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look here (Abstract Class Design): http://msdn.microsoft.com/en-us/library/ms229047.aspx It says: (1) Do not define public or
http://msdn.microsoft.com/en-us/library/ms190346.aspx It says here in the section under the statement start/end offset that you
Here is MSDN link From http://msdn.microsoft.com/en-us/library/s3f49ktz(v=VS.80).aspx It says: unsigned int : 4byte Range of
How do I repeat column header (captions) in RDLC reports? It says here http://msdn.microsoft.com/EN-US/library/735D1EE7-3C89-46D8-A346-504DB10F33E1.aspx#TableNoGroups
I tried getting AccExplorer32 from this link http://msdn.microsoft.com/en-us/library/dd631969.aspx and many others, but it says
I tried to compile http://msdn.microsoft.com/en-us/gg266450 But it says It cannot find System.ServiceModel.Syndication I have
According to this http://msdn.microsoft.com/en-us/library/cc849094(v=vs.85).aspx#OptInHighDPI you can make your web browser control use IE8-style zooming,
I'm following the directions here: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/0ce70e04-0ba0-4819-8898-87243203adb9/ to support mapping to a view that uses
I recently found a log statement in my projects codebase that says here i
While designing a table my colleague here says that I should avoid identity column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.