RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStream Class

TStream is the base class type for stream objects that can read from or write to various kinds of storage media, such as disk files, dynamic memory, and so on.

Pascal
TStream = class(TObject);
C++
class TStream : public TObject;

Classes

Use specialized stream objects to read from, write to, or copy information stored in a particular medium. Each descendant of TStream implements methods for transferring information to and from a particular storage medium, such as a disk file, dynamic memory, and so on. In addition to methods for reading, writing, and copying bytes to and from the stream, stream objects permit applications to seek to an arbitrary position in the stream. Properties of TStream provide information about the stream, such as its size and the current position in the stream. 

TStream also introduces methods that work in conjunction with components and filers for loading and saving components in simple and inherited forms. These methods are called automatically by global routines that initiate component streaming. They can also be called directly to initiate the streaming process. Note, however, that component streaming always involves two additional objects: 

A component object that is passed as a parameter to the stream's methods. 

A filer object that is automatically created by the stream, and associated with the stream. 

TStream is an abstract or, in C++ terminology, pure virtual class. It should not be instantiated; it relies on abstract or pure virtual methods that must be overridden in descendant classes. Descendant stream objects, such as memory and file streams used for component streaming, are created automatically by the global functions ReadComponentRes and WriteComponentRes. For streaming other kinds of information, choose a descendant class according to the specific data and storage needs. These include 

TFileStream (for working with files) 

TStringStream (for manipulating in-memory strings) 

TMemoryStream (for working with a memory buffer) 

TBlobStream (for working with BLOB fields)  

TWinSocketStream (for reading and writing over a socket connection)  

TOleStream (for using a COM interface to read and write)  

C++ Examples: 

 

/*
This example shows how to use the built-in component
streaming support to convert any component into a string and
convert that string back into a component.
*/
class MyScrollBar : public TScrollBar
{
__published:    // IDE-managed Components
private:    // User declarations
public:     // User declarations
    __fastcall MyScrollBar(TComponent* Owner);
};

__fastcall MyScrollBar::MyScrollBar(TComponent* Owner)
    : TScrollBar(Owner)
{
}

void RegisterClassesWithStreamingSystem(void)
{
  // Make sure that as part of the startup
  // code our streaming classes are registered
  // with the streaming system.

  #pragma startup RegisterClassesWithStreamingSystem

  Classes::RegisterClass(__classid(MyScrollBar));
}

MyScrollBar *ScrollBar1;

AnsiString __fastcall ComponentToString(TComponent *c)
{
 AnsiString as;
 TMemoryStream* pms = new TMemoryStream();
 TStringStream* pss = new TStringStream(as);

 try
 {
    pms->WriteComponent(c);
    pms->Seek(0, soFromBeginning);
    ObjectBinaryToText(pms, pss);
    pss->Seek(0, soFromBeginning);
    as = pss->DataString;
 }
 catch(...)
 {
    ShowMessage("Streaming error.");
 }

 delete pms;
 delete pss;
 return as;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Memo1->Text = ComponentToString(ScrollBar1);
}

TComponent *__fastcall StringToComponent(AnsiString as)

{
 TMemoryStream* pms = new TMemoryStream();
 TStringStream* pss = new TStringStream(as);
 TComponent *pc;

 try
 {
    ObjectTextToBinary(pss, pms);
    pms->Seek(0, soFromBeginning);
 }
 catch(...)
 {
    ShowMessage("Streaming error.");
 }

 pc = pms->ReadComponent(NULL);
 delete pms;
 delete pss;
 return pc;
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  TComponent *temp = StringToComponent(Memo1->Text);
  ScrollBar1->Free();

  ScrollBar1 = dynamic_cast<MyScrollBar *>(temp);
  ScrollBar1->Parent = Form1;
  ScrollBar1->Visible = TRUE;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  ScrollBar1 = new MyScrollBar(Form1);
  ScrollBar1->Parent = Form1;
  ScrollBar1->Visible = TRUE;
  ScrollBar1->Top = 48;
  ScrollBar1->Left = 250;
  ScrollBar1->Name = "Ricksbar";
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  delete ScrollBar1;
}

 

Delphi Examples: 

{
This example shows how to use the built-in component
streaming support to convert any component into a string and
convert that string back into a component.
}
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  MyScrollBar = class(TScrollBar)
  end;

var
  Form1: TForm1;
  ScrollBar1: MyScrollBar;
  
implementation

{$R *.dfm}

function ComponentToStringProc(Component: TComponent): string;
var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryStream.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStream.Free;
    end;
  finally
    BinStream.Free
  end;
end;

function StringToComponentProc(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result:= BinStream.ReadComponent(nil);
    finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text:= ComponentToStringProc(ScrollBar1);
end;

// Edit the scrollbar parameters in the memo, click this
// button and see the scrollbar change.
procedure TForm1.Button2Click(Sender: TObject);
begin
  ScrollBar1.Free;
  ScrollBar1:= (StringToComponentProc(Memo1.Text) as MyScrollBar);
  ScrollBar1.Parent:= Form1;
  ScrollBar1.Visible:= TRUE;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ScrollBar1:= MyScrollBar.Create(Form1);
  ScrollBar1.Parent:= Form1;
  ScrollBar1.Visible:= TRUE;
  ScrollBar1.Top:= 48;
  ScrollBar1.Left:= 250;
  ScrollBar1.Name:= 'Ricksbar';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ScrollBar1.Free;
end;

initialization

RegisterClass(MyScrollBar);

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!