Yogi Yang 007
Yogi Yang 007

Reputation: 5251

XE6 - How to convert TStringStream content to TStream?

I am using following code to convert a TStringStream to a TStream. But it give error when I try to copy the TStringStream's content to TStream.

It seems I am making some very obvious mistake but am not able to catch it.

My Code:

procedure TfrmMain2.Button1Click(Sender: TObject);
var
  SS:TStringStream;
  S, S2:TStream;
  PhotoStr: String;
begin
  PhotoStr := 'Hello World, Testing: /9j/4AAQSkZJRgABAQAAAQABAAD//' +
     'gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIp' +
     'LCBxdWFsaXR5ID0gODUK/9sAQwAFAwQEBAMFBAQEBQUFBgcMCAcHBwcPCwsJ' +
     'DBEPEhIRDxERExYcFxMUGhURERghGBodHR8fHxMXIiQiHiQcHh8e';

  SS := TStringStream.Create(PhotoStr);
  S := TStream.Create();
  S.CopyFrom(SS, SS.Size);  //<= This line gives error
end;

The error message I am getting is: ...class EWriteError with message 'Stream write error'.

I don't know as to why I am getting this error.

Upvotes: 0

Views: 857

Answers (1)

David Heffernan
David Heffernan

Reputation: 613511

You are creating an instance of TStream which is an abstract class. You must never instantiate TStream directly. Always instantiate a descendent class instead, such as TFileStream, TMemoryStream, etc.

Upvotes: 5

Related Questions