delphi – 带偏移量的TFileStream

前端之家收集整理的这篇文章主要介绍了delphi – 带偏移量的TFileStream前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要扩展TFileStream,以便它可以处理不是来自0偏移量的文件,而是来自用户定义的偏移量.我的意思是它必须将用户定义的偏移解释为流开始.我的代码是:
type
  TSuFileStream = class(TFileStream)
  protected
    FOffset : int64;

    procedure SetOffset(Offset : int64);

    procedure SetSize(NewSize: Longint); override;
    procedure SetSize(const NewSize: Int64); override;
  public
    constructor Create(const AFileName: string; Mode: Word); overload;
    constructor Create(const AFileName: string; Mode: Word; Rights: Cardinal); overload;

    function Seek(Offset: Longint; Origin: Word): Longint; override;
    function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;

    property Offset : int64 read FOffset write SetOffset;
  end;
...
constructor TSuFileStream.Create(const AFileName: string; Mode: Word);
begin
  inherited Create(AFileName,Mode);
  FOffset := 0;
end;

constructor TSuFileStream.Create(const AFileName: string; Mode: Word; Rights: Cardinal);
begin
  inherited Create(AFileName,Mode,Rights);
  FOffset := 0;
end;

procedure TSuFileStream.SetOffset(Offset : int64);
begin
  FOffset := Offset;
  inherited Seek(FOffset,soBeginning);
end;

procedure TSuFileStream.SetSize(NewSize: Longint);
begin
  inherited SetSize(FOffset + NewSize);
end;

procedure TSuFileStream.SetSize(const NewSize: Int64);
begin
  inherited SetSize(FOffset + NewSize);
end;

function TSuFileStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
  Result := Seek(Int64(Offset),TSeekOrigin(Origin));
end;

function TSuFileStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
  case Origin of
    soBeginning: Result := inherited Seek(FOffset + Offset,soBeginning) - FOffset;
    soCurrent: Result := inherited Seek(Offset,soCurrent) - FOffset;
    soEnd: Result := inherited Seek(Offset,soEnd) - FOffset;
  end;
end;

但它不起作用.问题在于寻求功能,但我不知道为什么.当我将这样的流传递给第三方组件时,它仅在TSuFileStream.Offset:= 0时起作用;

解决方法

使用 TGpStreamWindow,可在 my webGoogle Code获得.

用法

var
  offsetStream: TGpStreamWindow;

begin
  offsetStream := TGpStreamWindow.Create(originalStream,initialOffset,originalStream.Size - 1);
  try
    DoSomethingWith(offsetStream);
    offsetStream.SetWindow(anotherInitialOffset,originalStream.Size - 1);
    DoSomethingElseWith(offsetStream);
  finally FreeAndNil(offsetStream); end;
end;
原文链接:https://www.f2er.com/delphi/102199.html

猜你在找的Delphi相关文章