unit Unit2;
interface
uses
System.Classes,
System.SysUtils;
type
TStreamData =
class abstract( TInterfacedPersistent, IStreamPersist )
private
function SupportsStreamPersist( Persistent: TPersistent;
var StreamPersist: IStreamPersist ): Boolean;
procedure LoadFromStreamPersist(
const Source: IStreamPersist );
procedure SaveToStreamPersist(
const Dest: IStreamPersist );
protected
function GetDataStream(
const Writeable: Boolean ): TStream;
virtual;
abstract;
procedure AssignTo( Dest: TPersistent );
override;
public
procedure Assign( Source: TPersistent );
override;
procedure LoadFromStream( Stream: TStream );
procedure SaveToStream( Stream: TStream );
end;
TFileStreamData =
class( TStreamData )
private
FFileName:
string;
protected
function GetDataStream(
const Writeable: Boolean ): TStream;
override;
public
constructor Create(
const FileName:
string );
end;
implementation
{ TStreamData }
procedure TStreamData.Assign( Source: TPersistent );
var
lSource: IStreamPersist;
begin
if SupportsStreamPersist( Source, lSource )
then
LoadFromStreamPersist( lSource )
else
inherited;
end;
procedure TStreamData.AssignTo( Dest: TPersistent );
var
lDest: IStreamPersist;
begin
if SupportsStreamPersist( Dest, lDest )
then
SaveToStreamPersist( lDest )
else
inherited;
end;
procedure TStreamData.LoadFromStream( Stream: TStream );
var
lDest: TStream;
begin
lDest := GetDataStream( true );
try
lDest.CopyFrom( Stream, -1 );
finally
lDest.Free;
end;
end;
procedure TStreamData.LoadFromStreamPersist(
const Source: IStreamPersist );
var
lDest: TStream;
begin
lDest := GetDataStream( true );
try
Source.SaveToStream( lDest );
finally
lDest.Free;
end;
end;
procedure TStreamData.SaveToStream( Stream: TStream );
var
lSource: TStream;
begin
lSource := GetDataStream( false );
try
Stream.CopyFrom( lSource, -1 );
finally
lSource.Free;
end;
end;
procedure TStreamData.SaveToStreamPersist(
const Dest: IStreamPersist );
var
lSource: TStream;
begin
lSource := GetDataStream( false );
try
Dest.LoadFromStream( lSource );
finally
lSource.Free;
end;
end;
function TStreamData.SupportsStreamPersist( Persistent: TPersistent;
var StreamPersist: IStreamPersist ): Boolean;
begin
Result := Supports( Persistent, IStreamPersist, StreamPersist );
end;
{ TFileStreamData }
constructor TFileStreamData.Create(
const FileName:
string );
begin
inherited Create;
FFileName := ExpandFileName( FileName );
end;
function TFileStreamData.GetDataStream(
const Writeable: Boolean ): TStream;
begin
if Writeable
then
begin
ForceDirectories( ExtractFilePath( FFileName ) );
Result := TFileStream.Create( FFileName, fmCreate
or fmOpenWrite
or fmShareDenyRead );
end
else
Result := TFileStream.Create( FFileName, fmOpenRead
or fmShareDenyWrite );
end;
end.