1 | unit SubStream;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TSubStream }
|
---|
11 |
|
---|
12 | TSubStream = class(TStream)
|
---|
13 | private
|
---|
14 | FSource: TStream;
|
---|
15 | FSourcePosition: Int64;
|
---|
16 | FSize, FPosition: Longint;
|
---|
17 | public
|
---|
18 | function GetSize: Int64; override;
|
---|
19 | procedure SetSize(NewSize: Longint); override;
|
---|
20 | function Seek(Offset: Longint; Origin: Word): Longint; override;
|
---|
21 | function Read(var Buffer; Count: Longint): Longint; override;
|
---|
22 | function Write(const Buffer; Count: Longint): Longint; override;
|
---|
23 | procedure Assign(ASource: TObject);
|
---|
24 | property Source: TStream read FSource write FSource;
|
---|
25 | property SourcePosition: Int64 read FSourcePosition write FSourcePosition;
|
---|
26 | end;
|
---|
27 |
|
---|
28 |
|
---|
29 | implementation
|
---|
30 |
|
---|
31 | { TSubStream }
|
---|
32 |
|
---|
33 | function TSubStream.GetSize:Int64;
|
---|
34 | begin
|
---|
35 | Result := FSize;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | procedure TSubStream.SetSize(NewSize:Longint);
|
---|
39 | begin
|
---|
40 | FSize := NewSize;
|
---|
41 | if FPosition > FSize then
|
---|
42 | FPosition := FSize;
|
---|
43 | end;
|
---|
44 |
|
---|
45 | function TSubStream.Seek(Offset:Longint;Origin:Word):Longint;
|
---|
46 | begin
|
---|
47 | case Origin of
|
---|
48 | soFromBeginning: FPosition := Offset;
|
---|
49 | soFromEnd: FPosition := FSize + Offset;
|
---|
50 | soFromCurrent: FPosition := FPosition + Offset;
|
---|
51 | end;
|
---|
52 | Result := FPosition;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | function TSubStream.Read(var Buffer; Count:Longint): Longint;
|
---|
56 | var
|
---|
57 | TempPos: Int64;
|
---|
58 | begin
|
---|
59 | TempPos := FSource.Position;
|
---|
60 | FSource.Position := FSourcePosition + Position;
|
---|
61 | Result := FSource.Read(Buffer, Count);
|
---|
62 | FSource.Position := TempPos;
|
---|
63 | Inc(FPosition, Result);
|
---|
64 | end;
|
---|
65 |
|
---|
66 | function TSubStream.Write(const Buffer;Count:Longint):Longint;
|
---|
67 | var
|
---|
68 | TempPos: Int64;
|
---|
69 | begin
|
---|
70 | TempPos := FSource.Position;
|
---|
71 | FSource.Position := FSourcePosition + Position;
|
---|
72 | Result := Source.Write(Buffer, Count);
|
---|
73 | FSource.Position := TempPos;
|
---|
74 | Inc(FPosition, Result);
|
---|
75 | end;
|
---|
76 |
|
---|
77 | procedure TSubStream.Assign(ASource: TObject);
|
---|
78 | begin
|
---|
79 | if ASource is TSubStream then begin
|
---|
80 | FSource := TSubStream(ASource).FSource;
|
---|
81 | FSourcePosition := TSubStream(ASource).FSourcePosition;
|
---|
82 | FSize := TSubStream(ASource).FSize;
|
---|
83 | FPosition := TSubStream(ASource).FPosition;
|
---|
84 | end;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | end.
|
---|
88 |
|
---|