source: trunk/Packages/CoolStreaming/SubStream.pas

Last change on this file was 323, checked in by chronos, 6 months ago
File size: 2.1 KB
Line 
1unit SubStream;
2
3interface
4
5uses
6 Classes, SysUtils;
7
8type
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
29implementation
30
31{ TSubStream }
32
33function TSubStream.GetSize:Int64;
34begin
35 Result := FSize;
36end;
37
38procedure TSubStream.SetSize(NewSize:Longint);
39begin
40 FSize := NewSize;
41 if FPosition > FSize then
42 FPosition := FSize;
43end;
44
45function TSubStream.Seek(Offset:Longint;Origin:Word):Longint;
46begin
47 case Origin of
48 soFromBeginning: FPosition := Offset;
49 soFromEnd: FPosition := FSize + Offset;
50 soFromCurrent: FPosition := FPosition + Offset;
51 end;
52 Result := FPosition;
53end;
54
55function TSubStream.Read(var Buffer; Count:Longint): Longint;
56var
57 TempPos: Int64;
58begin
59 TempPos := FSource.Position;
60 FSource.Position := FSourcePosition + Position;
61 Result := FSource.Read(Buffer, Count);
62 FSource.Position := TempPos;
63 Inc(FPosition, Result);
64end;
65
66function TSubStream.Write(const Buffer;Count:Longint):Longint;
67var
68 TempPos: Int64;
69begin
70 TempPos := FSource.Position;
71 FSource.Position := FSourcePosition + Position;
72 Result := Source.Write(Buffer, Count);
73 FSource.Position := TempPos;
74 Inc(FPosition, Result);
75end;
76
77procedure TSubStream.Assign(ASource: TObject);
78begin
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;
85end;
86
87end.
88
Note: See TracBrowser for help on using the repository browser.