RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStream.CopyFrom Method

Copies a specified number of bytes from one stream to another.

Pascal
function CopyFrom(Source: TStream; Count: Int64): Int64;
C++
__fastcall Int64 CopyFrom(TStream Source, Int64 Count);

Use CopyFrom to copy data to the stream from a different stream. Using CopyFrom eliminates the need for the user to create, read into, write from, and free a buffer when copying data. 

CopyFrom copies Count bytes from the stream specified by Source into the stream. It then moves the current position by Count bytes, and returns the number of bytes copied.  

If Count is 0, CopyFrom sets Source position to 0 before reading and then copies the entire contents of Source into the stream. If Count is greater than or less than 0, CopyFrom reads from the current position in Source.  

C++ Examples: 

 

/*
This example copies a specified old file to a new file.  Add
a TSaveDialog to the form.  Also add two TEdits, two TLabels
and a TButton with the OnClick event named Save1Click. Files
can be saved to and from the app directory by using relative
paths.
*/
void __fastcall TForm1::Save1Click(TObject *Sender)
{
  AnsiString NewFileName =
    ExtractFilePath(Application->ExeName) +
    ExtractFileName(Edit1->Text);
  AnsiString OldFileName =
    ExtractFilePath(Application->ExeName) +
    ExtractFileName(Edit2->Text);
  AnsiString Msg = Format("Copy %s to %s", ARRAYOFCONST((OldFileName, NewFileName)));
  if (MessageDlg(Msg, mtCustom, TMsgDlgButtons() << mbOK << mbCancel, 0) == mrOk)
  {
    TFileStream *OldFile = new TFileStream(OldFileName, fmOpenRead);
    try
    {
      TFileStream *NewFile = new TFileStream(NewFileName, fmCreate);
      try
      {
        NewFile->CopyFrom(OldFile, OldFile->Size);
      }
      __finally
      {
        FreeAndNil(&NewFile);
      }
    }
    __finally
    {
      FreeAndNil(&OldFile);
    }
  }
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Label3->Caption = "Current directory: " + ExtractFilePath(Application->ExeName);
}

 

Delphi Examples: 

{
The following example copies the data in the Notes field of
Table1 or SQLDataSet1 to the Remarks field of ClientDataSet1.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  Stream1: TBlobStream;
  Stream2: TStream;
  blobType : TBlobType;
begin
  blobType := Table1Notes.BlobType;
  Stream1 := TBlobStream.Create(Table1Notes, bmRead);
  try
    CDS2.Edit;
    { here’s a different way to create a blob stream }
    Stream2 := CDS2.CreateBlobStream(CDS2.FieldByName('Remarks'), bmReadWrite);
    try
      Stream2.CopyFrom(Stream1, Stream1.Size);
//    CDS2.Post;
//    CDS2.Active := True;
    finally
      Stream2.Free;
    end;
  finally
    Stream1.Free;
  end;
end;
{
This example copies a specified old file to a new file.  Add
a TSaveDialog to the form.  Also add two TEdits, two TLabels
and a TButton with the OnClick event named Save1Click. Files
can be saved to and from the app directory by using relative
paths.
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  Label3.Caption:= 'Current directory: ' + ExtractFilePath(Application.ExeName);
end;

procedure TForm1.Save1Click(Sender: TObject);
var
  NewFileName, OldFileName: string;
  Msg: string;
  NewFile: TFileStream;
  OldFile: TFileStream;
begin
  NewFileName :=
    ExtractFilePath(Application.ExeName) +
    ExtractFileName(Edit1.Text);
  OldFileName := 
    ExtractFilePath(Application.ExeName) + 
    ExtractFileName(Edit2.Text);
  Msg := Format('Copy %s to %s?', [OldFileName, NewFileName]);
  if MessageDlg(Msg, mtCustom, mbOKCancel, 0) = mrOK then
  begin
    OldFile := TFileStream.Create(
      OldFileName, fmOpenRead or fmShareDenyWrite);
    try
      NewFile := TFileStream.Create(
        NewFileName, fmCreate or fmShareDenyRead);
      try
        NewFile.CopyFrom(OldFile, OldFile.Size);
      finally
        FreeAndNil(NewFile);
      end;
    finally
      FreeAndNil(OldFile);
    end;
  end;
end; 

 

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