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 565539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:52:58+00:00 2026-05-13T12:52:58+00:00

I’m in the process of creating a custom stream for an API endpoint in

  • 0

I’m in the process of creating a custom stream for an API endpoint in my app. The stream needs to have custom logic that I don’t want to get into, but suffice to say I can’t use a built-in stream class.

I did the minimum necessary to implement a read-only stream (inheriting from System.IO.Stream) and I’ve verified that the System.IO.BinaryReader class can read from my stream:

Dim reader As New System.IO.BinaryReader(GenerateStream(business, logic))
Dim enc As New System.Text.ASCIIEncoding
Dim contents As String = enc.GetString(reader.ReadBytes(CType(reader.BaseStream.Length, Int32)))

The string “contents” contains the correct string for the entire stream.

However, I would like to be able allow the use of the System.IO.StreamReader class:

Dim reader As New System.IO.StreamReader(GenerateStream(business, logic), System.Text.Encoding.ASCII)
Dim contents As String = reader.ReadToEnd

but for whatever reason the ReadToEnd always returns the empty string.

Any ideas?

Here’s the stream:

Public Overrides ReadOnly Property CanRead() As Boolean
   Get
    Return True
   End Get
  End Property

  Public Overrides ReadOnly Property CanSeek() As Boolean
   Get
    Return False
   End Get
  End Property

  Public Overrides ReadOnly Property CanWrite() As Boolean
   Get
    Return False
   End Get
  End Property

  Public Overrides Sub Flush()
   'this method intentionally left blank'
  End Sub

  Public Overrides ReadOnly Property Length() As Long
   Get
    Return 'some business logic'
   End Get
  End Property

  Public Overrides Property Position() As Long
   Get
    Return bytePosition
   End Get
   Set(ByVal value As Long)
    Throw New System.NotSupportedException
   End Set
  End Property

  Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
   'I return 0 on an end of stream, otherwise the # of bytes successfully read.'
  End Function

  Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
   Throw New System.NotSupportedException
  End Function

  Public Overrides Sub SetLength(ByVal value As Long)
   Throw New System.NotSupportedException()
  End Sub

  Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
   Throw New System.NotSupportedException()
  End Sub
  • 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-13T12:52:59+00:00Added an answer on May 13, 2026 at 12:52 pm

    Looking closely at the description of StreamReader.ReadToEnd shows, quote:

    If the initial position within the
    stream is unknown or the stream does
    not support seeking, the underlying
    Stream object also needs to be
    reinitialized.

    It further goes on, saying:

    To avoid such a situation and produce
    robust code you should use the Read
    method and store the read characters
    in a pre-allocated buffer.

    But, looking under the hood, only Stream.Read is called, it seems, which should provide the output you need. But, you do know that StreamReader reads characters, it tries hard to interpret the data as a string. Not sure what happens if it cannot. What does the data consist of?


    Update: Consider the following test code (sorry, C#, but use a converter to get your code), I did not include the non-implemented methods from the abstract class so as to not distract from the core. Just as a proof of concept, this seems to work with ASCII encoding and without the need to re-init or an implementation for Seek.

    public class AsciiStream : Stream
    {
        private string dataStream = "abcdefghijklmnopqrstuvwxyz";
        private long position = 0;
    
        public override bool CanRead
        {
            get { return true; }
        }
    
        public override bool CanSeek
        {
            get { return false; }
        }
    
        public override bool CanWrite
        {
            get { return false; }
        }
    
    
        public override long Length
        {
            get { return dataStream.Length; }
        }
    
        public override long Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value < this.Length ? value : this.Length;
            }
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            long bufferPos = offset;
            long max = this.position + count;
            max = max < this.Length ? max : this.Length;
            for (; this.position < max; this.position++)
            {
                buffer[bufferPos] = Convert.ToByte(this.dataStream[(int) this.position]);
                bufferPos++;
            }
    
            return (int) bufferPos - offset;
        }
    }
    
    
    // call the code like as follows:
    StreamReader sReader = new StreamReader(new AsciiStream(), Encoding.ASCII);
    string sToEnd = sReader.ReadToEnd();
    

    Point being: the problem must lie in your actual Read-code or in the business logic you don’t show us. The approach you’ve chosen, on itself, should simply work. Have you tried implementing, by hand, a “read-to-end”? Or with the BinaryReader, reading past the end with ReadBytes(many)“?

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.