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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:07:07+00:00 2026-06-17T06:07:07+00:00

We have a library function that goes like this: class function TFileUtils.ReadTextStream(const AStream: TStream):

  • 0

We have a library function that goes like this:

class function TFileUtils.ReadTextStream(const AStream: TStream): string;
var
  StringStream: TStringStream;
begin
  StringStream := TStringStream.Create('', TEncoding.Unicode);
  try
    // This is WRONG since CopyFrom might rewind the stream (see Remys comment)
    StringStream.CopyFrom(AStream, AStream.Size - AStream.Position);
    Result := StringStream.DataString;
  finally
    StringStream.Free;
  end;
end;

When I check the string that is returned by the function the first Char is the (little-endian) BOM.

Why doesn’t TStringStream ignore the BOM?

Is there a better way to do this? I don’t need backwards compatibility with older Delphi versions, a working solution for XE2 would be fine.

  • 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-06-17T06:07:08+00:00Added an answer on June 17, 2026 at 6:07 am

    The BOM has to be coming from the source TStream, as TStringStream does not write a BOM. If you want to ignore the BOM if it is present in the source, you have to do it manually before then copying the data, eg:

    class function TFileUtils.ReadTextStream(const AStream: TStream): string;
    var
      StreamPos, StreamSize: Int64;
      Buf: TBytes;
      NumBytes: Integer;
      Encoding: TEncoding;
    begin
      Result := '';
    
      StreamPos := AStream.Position;
      StreamSize := AStream.Size - StreamPos;
    
      // Anything available to read?
      if StreamSize < 1 then Exit;
    
      // Read the first few bytes from the stream...
      SetLength(Buf, 4);
      NumBytes := AStream.Read(Buf[0], Length(Buf));
      if NumBytes < 1 then Exit;
      Inc(StreamPos, NumBytes);
      Dec(StreamSize, NumBytes);
    
      // Detect the BOM. If you know for a fact what the TStream data is encoded as, 
      // you can assign the Encoding variable to the appropriate TEncoding object and 
      // GetBufferEncoding() will check for that encoding's BOM only...
      SetLength(Buf, NumBytes);
      Encoding := nil;
      Dec(NumBytes, TEncoding.GetBufferEncoding(Buf, Encoding));
    
      // If any non-BOM bytes were read than rewind the stream back to that position...
      if NumBytes > 0 then
      begin
        AStream.Seek(-NumBytes, soCurrent);
        Dec(StreamPos, NumBytes);
        Inc(StreamSize, NumBytes);
      end else
      begin
        // Anything left to read after the BOM?
        if StreamSize < 1 then Exit;
      end;
    
      // Now read and decode whatever is left in the stream...
      StringStream := TStringStream.Create('', Encoding);
      try
        StringStream.CopyFrom(AStream, StreamSize);
        Result := StringStream.DataString;
      finally
        StringStream.Free;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
I have a library function that takes parameters as a text string (it's a
We have a macro for signalling errors in a common utilities library that goes
I have a library in CodeIgniter called someclass. class someclass extends CI_Controller { function
Lets say I have a library function that I cannot change that produces an
Does R6RS or Chez Scheme v7.9.4 have a library function to check if a
I have a failure in some inner library function in Octave. I want to
I have a .dll library which exports a function in the following format: _Java_folder1_folder2_folder3_JavaClassName_javamethodname@16
Suppose we have a shared library named libtest.so, there is one function foo in
I have been trying to call the NewPixelRegionIterator function in ImageMagick's library from C#

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.