-
Notifications
You must be signed in to change notification settings - Fork 49
/
OBufferedStreams.pas
393 lines (322 loc) · 11.5 KB
/
OBufferedStreams.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
unit OBufferedStreams;
{
Author:
Ondrej Pokorny, http://www.kluug.net
All Rights Reserved.
License:
MPL 1.1 / GPLv2 / LGPLv2 / FPC modified LGPLv2
Please see the /license.txt file for more information.
}
{
OBufferedStreams.pas
TOBufferedReadStream -> read streams with buffer.
Can be used e.g. for reading data from a file stream.
Supports seeking within the temp buffer range and also outside the temp buffer.
TOBufferedWriteStream -> write data to a destination stream with buffer.
E.g. to a file stream.
}
{$I OXml.inc}
{$IFDEF O_DELPHI_XE4_UP}
{$ZEROBASEDSTRINGS OFF}
{$ENDIF}
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$BOOLEVAL OFF}
interface
uses
{$IFDEF O_NAMESPACES}
System.SysUtils, System.Classes,
{$ELSE}
SysUtils, Classes,
{$ENDIF}
OWideSupp;
const
OBUFFEREDSTREAMS_DEFBUFFERSIZE = 16*1024;//16 KB
OBUFFEREDSTREAMS_DEFCHARBUFFERSIZE = OBUFFEREDSTREAMS_DEFBUFFERSIZE div SizeOf(OWideChar);//16 KB
type
TOBufferedWriteStream = class(TStream)
private
fStream: TStream;
fStreamPosition: ONativeInt;
fStreamSize: ONativeInt;
{$IFDEF O_GENERICBYTES}
fTempBuffer: TBytes;
{$ELSE}
fTempBuffer: Array of Byte;
{$ENDIF}
fTempBufferUsedLength: ONativeInt;
fBufferSize: ONativeInt;
protected
function GetSize: Int64; {$IF DEFINED(O_DELPHI_7_UP) OR DEFINED(FPC)}override;{$IFEND}
public
constructor Create(const aStream: TStream; const aBufferSize: ONativeInt = OBUFFEREDSTREAMS_DEFBUFFERSIZE);
destructor Destroy; override;
function Write(const Buffer; Count: LongInt): LongInt; override;
{$IFDEF O_DELPHI_XE3_UP}
function Write(const Buffer: TBytes; Offset, Count: LongInt): LongInt; override;
{$ENDIF}
function Read(var {%H-}Buffer; {%H-}Count: LongInt): LongInt; override;
{$IFDEF O_DELPHI_XE3_UP}
function Read(Buffer: TBytes; Offset, Count: LongInt): LongInt; override;
{$ENDIF}
function Seek(Offset: LongInt; Origin: Word): LongInt; override;
public
//write the whole temporary buffer to the destination stream
procedure EnsureTempBufferWritten;
end;
TOBufferedReadStream = class(TStream)
private
fStream: TStream;
fStreamPosition: ONativeInt;
fStreamSize: ONativeInt;
{$IFDEF O_GENERICBYTES}
fTempBuffer: TBytes;
{$ELSE}
fTempBuffer: Array of Byte;
{$ENDIF}
fTempBufferPosition: ONativeInt;
fTempBufferUsedLength: ONativeInt;
fBlockFlushTempBuffer: Integer;
fBufferSize: ONativeInt;
procedure CheckTempBuffer;
protected
function GetSize: Int64; {$IF DEFINED(O_DELPHI_7_UP) OR DEFINED(FPC)}override;{$IFEND}
public
constructor Create(const aStream: TStream; const aBufferSize: ONativeInt = OBUFFEREDSTREAMS_DEFBUFFERSIZE);
function Write(const {%H-}Buffer; {%H-}Count: LongInt): LongInt; override;
{$IFDEF O_DELPHI_XE3_UP}
function Write(const Buffer: TBytes; Offset, Count: LongInt): LongInt; override;
{$ENDIF}
function Read(var Buffer; Count: LongInt): LongInt; override;
{$IFDEF O_DELPHI_XE3_UP}
function Read(Buffer: TBytes; Offset, Count: LongInt): LongInt; override;
{$ENDIF}
function Seek(Offset: LongInt; Origin: Word): LongInt; override;
public
//disallow clearing temporary buffer -> use if you need to seek back within the temp buffer.
//use if you read from original streams that do not support seeking (e.g. a zip stream).
procedure BlockFlushTempBuffer;
procedure UnblockFlushTempBuffer;
end;
EOBufferedWriteStream = class(Exception);
EOBufferedReadStream = class(Exception);
implementation
resourcestring
OBufferedStreams_NilStream = 'The aStream parameter must be assigned when creating a buffered stream.';
OBufferedStreams_ReadingNotPossible = 'You can''t read from TOBufferedWriteStream';
OBufferedStreams_SeekingNotPossible = 'You can''t use seek in TOBufferedWriteStream';
OBufferedStreams_WritingNotPossible = 'You can''t write to TOBufferedReadStream';
{ TOBufferedWriteStream }
constructor TOBufferedWriteStream.Create(const aStream: TStream;
const aBufferSize: ONativeInt);
begin
inherited Create;
if not Assigned(aStream) then
raise EOBufferedWriteStream.Create(OBufferedStreams_NilStream);
fStream := aStream;
fStreamPosition := fStream.Position;
fStreamSize := fStream.Size;
fBufferSize := aBufferSize;
SetLength(fTempBuffer, fBufferSize);
end;
destructor TOBufferedWriteStream.Destroy;
begin
EnsureTempBufferWritten;
inherited;
end;
procedure TOBufferedWriteStream.EnsureTempBufferWritten;
begin
if fTempBufferUsedLength > 0 then begin
fStream.WriteBuffer(fTempBuffer[0], fTempBufferUsedLength);
fStreamSize := fStreamSize + fTempBufferUsedLength;
fStreamPosition := fStreamPosition + fTempBufferUsedLength;
fTempBufferUsedLength := 0;
end;
end;
function TOBufferedWriteStream.GetSize: Int64;
begin
Result {%H-}:= fStreamSize + fTempBufferUsedLength;
end;
function TOBufferedWriteStream.Read(var Buffer; Count: LongInt): LongInt;
begin
{$IFDEF FPC}
Result := 0;//JUST TO OMIT WARNING MESSAGES
{$ENDIF}
raise EOBufferedWriteStream.Create(OBufferedStreams_ReadingNotPossible);
end;
{$IFDEF O_DELPHI_XE3_UP}
function TOBufferedWriteStream.Read(Buffer: TBytes; Offset,
Count: LongInt): LongInt;
begin
Result := Self.Read(Buffer[Offset], Count);
end;
{$ENDIF}
function TOBufferedWriteStream.Seek(Offset: LongInt; Origin: Word): LongInt;
begin
if (Origin = soFromCurrent) and (Offset = 0) then begin
Result := fStreamPosition + fTempBufferUsedLength;
{$IFDEF O_DELPHI_6_DOWN}
//BECAUSE OF GetSize function!!!
end else if (Origin = soFromEnd) and (Offset = 0) then begin
Result := GetSize;
end else if (Origin = soFromBeginning) and (Offset = fStreamPosition + fTempBufferUsedLength) then begin//CURRENT POSITION
Result := fStreamPosition + fTempBufferUsedLength;
{$ENDIF}
end else begin
raise EOBufferedWriteStream.Create(OBufferedStreams_SeekingNotPossible);
end;
end;
function TOBufferedWriteStream.Write(const Buffer; Count: LongInt): LongInt;
begin
Result := Count;
if fTempBufferUsedLength+Result > fBufferSize then
EnsureTempBufferWritten;//WRITE TEMP BUFFER
if Result > fBufferSize then begin
//count to write bigger then buffer -> write directly
fStream.WriteBuffer(Buffer, Result);
end else if Result > 0 then begin
//store to temp!
if Result > fBufferSize-fTempBufferUsedLength then//store only what has space in buffer
Result := fBufferSize-fTempBufferUsedLength;
Move(Buffer, fTempBuffer[fTempBufferUsedLength], Result);
fTempBufferUsedLength := fTempBufferUsedLength + Result;
end;
{$IFNDEF O_DELPHI_XE2_UP}
//older delphi versions need to return Result = Count !!!
if (0 < Result) and (Result < Count) then begin
Result := Write({%H-}Pointer({%H-}ONativeUInt(@Buffer)+ONativeUInt(Result))^, Count-Result) + Result;
end;
{$ENDIF}
end;
{$IFDEF O_DELPHI_XE3_UP}
function TOBufferedWriteStream.Write(const Buffer: TBytes; Offset,
Count: LongInt): LongInt;
begin
Result := Self.Write(Buffer[Offset], Count);
end;
{$ENDIF}
{ TOBufferedReadStream }
procedure TOBufferedReadStream.BlockFlushTempBuffer;
begin
Inc(fBlockFlushTempBuffer);
end;
procedure TOBufferedReadStream.CheckTempBuffer;
var
xReadBytes, xRemainingTempLength, xNewLength: ONativeInt;
begin
if fTempBufferPosition = fTempBufferUsedLength then begin
//we reached end of the tempbuffer, clear or grow and read from stream
if fBlockFlushTempBuffer = 0 then begin
fTempBufferPosition := 0;
fTempBufferUsedLength := 0;
end;
xReadBytes := fBufferSize;
if xReadBytes > fStreamSize-fStreamPosition then//don't read from stream more than possible
xReadBytes := fStreamSize-fStreamPosition;
//CHECK THAT WE HAVE ALL NECESSARY BYTES IN TEMP BUFFER
xRemainingTempLength := Length(fTempBuffer)-fTempBufferPosition;
if xRemainingTempLength < xReadBytes then begin
//tempbuffer has to grow
xNewLength := Length(fTempBuffer) + xReadBytes - xRemainingTempLength;
SetLength(fTempBuffer, xNewLength);
end;
fStream.ReadBuffer(fTempBuffer[fTempBufferPosition], xReadBytes);
fStreamPosition := fStreamPosition + xReadBytes;
fTempBufferUsedLength := fTempBufferUsedLength + xReadBytes;
end;
end;
constructor TOBufferedReadStream.Create(const aStream: TStream;
const aBufferSize: ONativeInt);
begin
inherited Create;
if not Assigned(aStream) then
raise EOBufferedReadStream.Create(OBufferedStreams_NilStream);
fStream := aStream;
fStreamPosition := fStream.Position;
fStreamSize := fStream.Size;
fBufferSize := aBufferSize;
SetLength(fTempBuffer, fBufferSize);
end;
function TOBufferedReadStream.GetSize: Int64;
begin
Result := fStreamSize;
end;
{$IFDEF O_DELPHI_XE3_UP}
function TOBufferedReadStream.Read(Buffer: TBytes; Offset,
Count: LongInt): LongInt;
begin
Result := Self.Read(Buffer[Offset], Count);
end;
{$ENDIF}
function TOBufferedReadStream.Read(var Buffer; Count: LongInt): LongInt;
begin
if Count < 0 then begin
Result := 0;
Exit;
end;
CheckTempBuffer;
Result := fTempBufferUsedLength - fTempBufferPosition;
if Result > Count then
Result := Count;
if Result > 0 then begin
Move(fTempBuffer[fTempBufferPosition], Buffer, Result);
fTempBufferPosition := fTempBufferPosition + Result;
end;
{$IFNDEF O_DELPHI_XE2_UP}
//older delphi versions need to return Result = Count !!!
if (0 < Result) and (Result < Count) then begin
Result := Read({%H-}Pointer({%H-}ONativeUInt(@Buffer)+ONativeUInt(Result))^, Count-Result) + Result;
end;
{$ENDIF}
end;
function TOBufferedReadStream.Seek(Offset: LongInt; Origin: Word): LongInt;
var
xAbsolutePosition: ONativeInt;
begin
if (Origin = soFromCurrent) and (Offset = 0) then begin
//CURRENT POSITION
Result := fStreamPosition - fTempBufferUsedLength + fTempBufferPosition;
end else begin
//SEEK TO POSITION AND CLEAR TEMP STREAM
case Origin of
soFromCurrent: xAbsolutePosition := fStreamPosition - fTempBufferUsedLength + fTempBufferPosition + Offset;
soFromEnd: xAbsolutePosition := fStreamSize + Offset;
else
//soFromBeginning
xAbsolutePosition := Offset;
end;
if (xAbsolutePosition >= (fStreamPosition - fTempBufferUsedLength))
then begin
//WITHIN TEMP RANGE
fTempBufferPosition := xAbsolutePosition - (fStreamPosition - fTempBufferUsedLength);
Result := fStreamPosition - fTempBufferUsedLength + fTempBufferPosition;
end else begin
//OUTSIDE TEMP RANGE, CLEAR TEMP STREAM
Result := fStream.Seek(soFromBeginning, xAbsolutePosition);
fStreamPosition := Result;
fTempBufferUsedLength := 0;
fTempBufferPosition := 0;
end;
end;
end;
procedure TOBufferedReadStream.UnblockFlushTempBuffer;
begin
if fBlockFlushTempBuffer > 0 then
Dec(fBlockFlushTempBuffer);
end;
{$IFDEF O_DELPHI_XE3_UP}
function TOBufferedReadStream.Write(const Buffer: TBytes; Offset,
Count: LongInt): LongInt;
begin
Result := Self.Write(Buffer[Offset], Count);
end;
{$ENDIF}
function TOBufferedReadStream.Write(const Buffer; Count: LongInt): LongInt;
begin
{$IFDEF FPC}
Result := 0;//JUST TO OMIT WARNING MESSAGES
{$ENDIF}
raise EOBufferedReadStream.Create(OBufferedStreams_WritingNotPossible);
end;
end.