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