{$MODE OBJFPC}
{$MODESWITCH result}
{$MODESWITCH autoderef}
unit dstreamfpc;

interface

uses
  Classes,
  dstream;

type
TFpcStreamFromDojStream = class(Classes.Stream)
private
  FActual: dstream.TData;
public
  constructor Create(Actual: dstream.TData);
  destructor Destroy; override;
  function Read(var Buffer; Count: LongInt): LongInt; override;
  function Write(const Buffer; Count: LongInt): LongInt; override;
end;

PDojStreamFromFpcStream = ^TDojStreamFromFpcStream;
TDojStreamFromFpcStream = object(dstream.TStream)
private
  FActual: Classes.TStream;
public
  constructor Init(Actual: Classes.TStream);
  function Create(Actual: Classes.TStream): PDojStreamFromFpcStream; static;
  destructor Done; virtual;
  function Read(P: Pointer; Size: LongInt): LongInt; virtual;
  function Write(P: Pointer; Size: LongInt): LongInt; virtual;
  //function Seek(Offset: TInt64; Origin: TSeekOrigin): TInt64; virtual;
  function GetPosition: Int64; virtual;
  //function Eof: Boolean; virtual;
  function GetSize: Int64; virtual;
end;

implementation

constructor TFpcStreamFromDojStream.Create(Actual: dstream.TData);
begin
  FActual := Actual;
end;

destructor TFpcStreamFromDojStream.Destroy;
begin
  FActual.Done;
end;

function TFpcStreamFromDojStream.Read(var Buffer; Count: LongInt): LongInt;
begin
  Result := FActual.Stream.Read(@Buffer, Count);
end;

function TFpcStreamFromDojStream.Write(const Buffer; Count: LongInt): LongInt;
begin
  Result := FActual.Stream.Write(@Buffer, Count);
end;

constructor TDojStreamFromFpcStream.Init(Actual: Classes.TStream);
begin
  FActual := Actual;
end;

function TDojStreamFromFpcStream.Create(Actual: Classes.TStream): PDojStreamFromFpcStream;
begin
  Result := New(PStream, Init(Actual));
end;

destructor TDojStreamFromFpcStream.Done;
begin
  FActual.Free;
end;

function TDojStreamFromFpcStream.Read(P: Pointer; Size: LongInt): LongInt;
begin
  Result := FActual.Read(P, Size);
end;

function TDojStreamFromFpcStream.Write(P: Pointer; Size: LongInt): LongInt;
begin
  Result := FActual.Write(P, Size);
end;

//function TDojStreamFromFpcStream.Seek(Offset: TInt64; Origin: TSeekOrigin): TInt64;
//begin
//end;

function TDojStreamFromFpcStream.GetPosition: Int64;
begin
  Result := FActual.Position;
end;

//function TDojStreamFromFpcStream.Eof: Boolean;
//begin
//end;

function TDojStreamFromFpcStream.GetSize: Int64;
begin
  Result := FActual.Size;
end;

end.
