-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StreamWindow.pas
286 lines (238 loc) · 10.1 KB
/
StreamWindow.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
StreamWindow
Provides very simple class TStreamWindow (descendant of TStream) that is
intended to be used as a substream within larger stream.
It is designed for situation where you want to limit access to only small
part of a larger stream.
For example, if you want to read stored string list that is embedded into
larger stream. Normally, the method LoadFromStream would read everything
from current position to the end of the stream, producing bad results if
there is anything stored behind the actual list data. Stream window can be
used in this situation to limit reading to only the list data. Let's say
the list data starts in aStream at position 1024 and are 256 bytes long,
then you can do something like...
StrWnd := TStreamWindow.Create(aStream,1024,256);
try
StringList.LoadFromStream(StrWnd);
finally
StrWnd.Free;
end;
...this ensures that only at most 256 bytes (from position 1024) can be
read, forcing the reader/parser to stop even when end of the underlying
stream was not reached.
WARNING - do not directly access (seek, read, write, size set, ...) the
target stream while it is bound to any stream window, as there
is no implicit position synchronization (the window keeps its own
position that is synchronized with position of target stream only
at creation).
Or use method SynchonizePosition before switching operation to
the window every time you change target stream.
Version 1.0.1 (2024-05-03)
Last change (2024-05-03)
©2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.StreamWindow
Dependencies:
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
Library AuxExceptions is required only when rebasing local exception classes
(see symbol StreamWindow_UseAuxExceptions for details).
Indirect dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit StreamWindow;
{
StreamWindow_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
StreamWindow_UseAuxExceptions to achieve this.
}
{$IF Defined(StreamWindow_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF FPC}
{$MODE ObjFPC}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes
{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ESWExceptin = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESWInvalidValue = class(ESWExceptin);
{===============================================================================
--------------------------------------------------------------------------------
TStreamWindow
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TStreamWindow - class declaration
===============================================================================}
type
TStreamWindow = class(TStream)
protected
fOwnsTarget: Boolean;
fTarget: TStream;
fStart: Int64;
fLength: Int64;
fWindowPosition: Int64;
procedure Initialize(Target: TStream; Start,Length: Int64); virtual;
procedure Finalize; virtual;
public
constructor Create(Target: TStream; Start,Length: Int64); overload;
constructor Create(Target: TStream; Length: Int64); overload;
destructor Destroy; override;
Function Read(var Buffer; Count: LongInt): LongInt; override;
Function Write(const Buffer; Count: LongInt): LongInt; override;
Function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
procedure SynchonizePosition; virtual;
property OwnsTarget: Boolean read fOwnsTarget write fOwnsTarget;
property Target: TStream read fTarget;
end;
implementation
{===============================================================================
--------------------------------------------------------------------------------
TStreamWindow
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TStreamWindow - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TStreamWindow - protected methods
-------------------------------------------------------------------------------}
procedure TStreamWindow.Initialize(Target: TStream; Start,Length: Int64);
begin
fOwnsTarget := False;
fTarget := Target;
fStart := Start;
fLength := Length;
fWindowPosition := 0;
fTarget.Seek(fStart,soBeginning);
end;
//------------------------------------------------------------------------------
procedure TStreamWindow.Finalize;
begin
If fOwnsTarget then
FreeAndNil(fTarget);
end;
{-------------------------------------------------------------------------------
TStreamWindow - public methods
-------------------------------------------------------------------------------}
constructor TStreamWindow.Create(Target: TStream; Start,Length: Int64);
begin
inherited Create;
Initialize(Target,Start,Length);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TStreamWindow.Create(Target: TStream; Length: Int64);
begin
inherited Create;
Initialize(Target,Target.Position,Length);
end;
//------------------------------------------------------------------------------
destructor TStreamWindow.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
Function TStreamWindow.Read(var Buffer; Count: LongInt): LongInt;
begin
If fWindowPosition < 0 then
Seek(0,soBeginning);
If fWindowPosition >= fLength then
begin
Result := 0;
Exit;
end
else
begin
If fWindowPosition + Count > fLength then
Count := LongInt(fLength - fWindowPosition);
Result := fTarget.Read(Buffer,Count);
Inc(fWindowPosition,Result);
end;
end;
//------------------------------------------------------------------------------
Function TStreamWindow.Write(const Buffer; Count: LongInt): LongInt;
begin
If fWindowPosition < 0 then
Seek(0,soBeginning);
If fWindowPosition >= fLength then
begin
Result := 0;
Exit;
end
else
begin
If fWindowPosition + Count > fLength then
Count := LongInt(fLength - fWindowPosition);
Result := fTarget.Write(Buffer,Count);
Inc(fWindowPosition,Result);
end;
end;
//------------------------------------------------------------------------------
Function TStreamWindow.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
case Origin of
soBeginning: begin
If Offset < 0 then
fWindowPosition := fTarget.Seek(fStart,soBeginning) - fStart
else If Offset > fLength then
fWindowPosition := fTarget.Seek(fStart + fLength,soBeginning) - fStart
else
fWindowPosition := fTarget.Seek(fStart + Offset,soBeginning) - fStart;
end;
soCurrent: begin
If Offset < -fWindowPosition then
fWindowPosition := fTarget.Seek(-fWindowPosition,soCurrent) - fStart
else If Offset > (fLength - fWindowPosition) then
fWindowPosition := fTarget.Seek(fLength - fWindowPosition,soCurrent) - fStart
else
fWindowPosition := fTarget.Seek(Offset,soCurrent) - fStart;
end;
soEnd: begin
If Offset < -fLength then
fWindowPosition := fTarget.Seek(fStart,soBeginning) - fStart
else If Offset > 0 then
fWindowPosition := fTarget.Seek(fStart + fLength,soBeginning) - fStart
else
fWindowPosition := fTarget.Seek(fStart + fLength + Offset,soBeginning) - fStart
end;
else
raise ESWInvalidValue.CreateFmt('TStreamWindow.Seek: Invalid seek origin (%d).',[Ord(Origin)]);
end;
Result := fWindowPosition;
end;
//------------------------------------------------------------------------------
procedure TStreamWindow.SynchonizePosition;
begin
If fWindowPosition < 0 then
fWindowPosition := 0
else If fWindowPosition > fLength then
fWindowPosition := fLength;
fTarget.Seek(fStart + fWindowPosition,soBeginning);
end;
end.