Type TStreamPart =
Class(TStream)
Private
_Stream: TStream;
_Offset, _Len: Int64;
Protected
Function GetSize: Int64;
Override;
Procedure SetSize(NewSize: LongInt);
Overload;
Override;
Procedure SetSize(
Const NewSize: Int64);
Overload;
Override;
Public
Constructor Create(Stream: TStream;
Const Start, Length: Int64);
Function Read(
Var Buffer; Count: LongInt): LongInt;
Override;
Function Write(
Const Buffer; Count: LongInt): LongInt;
Override;
Function Seek(Offset: LongInt; Origin: Word): LongInt;
Overload;
Override;
Function Seek(
Const Offset: Int64; Origin: TSeekOrigin): Int64;
Overload;
Override;
End;
Function TStreamPart.GetSize: Int64;
Begin
Result := Min(Max(_Stream.Size - _Offset, 0), _Len);
End;
Procedure TStreamPart.SetSize(NewSize: LongInt);
Begin
SetSize(Int64(NewSize));
End;
Procedure TStreamPart.SetSize(
Const NewSize: Int64);
Begin
Raise Exception.Create('
read only');
End;
Constructor TStreamPart.Create(Stream: TStream;
Const Start, Length: Int64);
Begin
_Stream := Stream;
_Offset := Start;
_Len := Length;
End;
Function TStreamPart.
Read(
Var Buffer; Count: LongInt): LongInt;
Begin
Result := _Stream.
Read(Buffer, Min(Int64(Count), Size - Position));
End;
Function TStreamPart.
Write(
Const Buffer; Count: LongInt): LongInt;
Begin
Result := _Stream.
Write(Buffer, Min(Int64(Count), Size - Position));
End;
Function TStreamPart.Seek(Offset: LongInt; Origin: Word): LongInt;
Begin
Result := _Stream.Seek(LongInt(Min(Max(Int64(Offset), 0) + _Offset, MaxInt)), Origin);
End;
Function TStreamPart.Seek(
Const Offset: Int64; Origin: TSeekOrigin): Int64;
Begin
Result := _Stream.Seek(Max(Offset, 0) + _Offset, Origin);
End;