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

  • SEARCH
  • Home
  • 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 6719627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:06:55+00:00 2026-05-26T09:06:55+00:00

I am currently in the process of writing a BinaryReader that caches the BaseStream.Position

  • 0

I am currently in the process of writing a BinaryReader that caches the BaseStream.Position and BaseStream.Length properties. Here is what I have so far:

public class FastBinaryReader
{
    BinaryReader reader;

    public long Length { get; private set; }
    public long Position { get; private set; }

    public FastBinaryReader(Stream stream)
    {
        reader = new BinaryReader(stream);
        Length = stream.Length;
        Position = 0;
    }

    public void Seek(long newPosition)
    {
        reader.BaseStream.Position = newPosition;
        Position = newPosition;
    }

    public byte[] ReadBytes(int count)
    {
        if (Position + count >= Length)
            Position = Length;
        else
            Position += count;

        return reader.ReadBytes(count);
    }

    public void Close()
    {
        reader.Close();
    }
}

Instead of providing a Length and Position property, I would like to create a BaseStream property that allows me to expose my Position and Length properties as FastBinaryReader.BaseStream.Position and FastBinaryReader.BaseStream.Length, so that my existing code will stay compatible with the original BinaryReader class.

How would I go about doing this?

  • 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-26T09:06:55+00:00Added an answer on May 26, 2026 at 9:06 am

    I wouldn’t do this exactly the way you have it here.

    Consider that you need to expose a property of type Stream (what BinaryReader.BaseStream is). So you ‘ll need to create your own class deriving from Stream. This class would need to:

    • take a reference to FastBinaryReader so that it can override Stream.Length and Stream.Offset by delegating to a FastBinaryReader member
    • take a reference to a Stream (the same one passed in the FastBinaryReader constructor) in order to delegate all other operations to that stream (you could have these throw new NotImplementedException() instead, but you never know which library method is going to call them!)

    You can imagine how it’d look:

    private class StreamWrapper : Stream
    {
        private readonly FastBinaryReader reader;
    
        private readonly Stream baseStream;
    
        public StreamWrapper(FastBinaryReader reader, Stream baseStream)
        {
            this.reader = reader;
            this.baseStream = baseStream;
        }
        public override long Length
        {
            get { return reader.Length; }
        }
    
        public override long Position
        {
            get { return reader.Position; }
            set { reader.Position = value; }
        }
    
        // Override all other Stream virtuals as well
    }
    

    This would work, but it seems to me to be slightly clumsy. The logical continuation would be to put the caching in StreamWrapper instead of inside FastBinaryReader itself:

    private class StreamWrapper : Stream
    {
        private readonly Stream baseStream;
    
        public StreamWrapper(Stream baseStream)
        {
            this.baseStream = baseStream;
        }
    
        public override long Length
        {
            get { /* caching implementation */ }
        }
    
        public override long Position
        {
            get { /* caching implementation */ }
            set { /* caching implementation */ }
        }
    
        // Override all other Stream virtuals as well
    }
    

    This would allow you to use StreamWrapper transparently and keep the caching behavior. But it raises the question: is the Stream you work with so dumb that it doesn’t cache this by itself?

    And if it isn’t, maybe the performance gain you see is the result of that if statement inside ReadBytes and not of caching Length and Position?

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

Sidebar

Related Questions

I'm currently in the process of writing an application that has quite a number
For personal use I am currently writing a recursive PHP function that should process
I am currently in the process of writing some software in Java that I
I'm currently writing a process that generates Powerpoint reports programmatically from a given template
I'm currently in the process of writing a steganography application with Qt. I am
We are currently going through the long process of writing some coding standards for
We currently have a process where we receive an inventory file from a vendor
I am currently in the process of creating my own blog and I have
I'm currently in the process of writing a CGI blog engine in C (fun
I'm currently in the process of writing a little blog / generic posting system

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.