-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUStreamHelper.pas
542 lines (472 loc) · 17.1 KB
/
UStreamHelper.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
(*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* http://www.opensource.org/licenses/cddl1.php. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying * information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Portions Copyright 2007 Andreas Schneider
*)
{@abstract(This unit contains procedures and classes to help with stream handling.
It can be used to ease copying of streams and to assist in writing and reading
specialized types to/from streams.)
@bold(Warning!!!)@br
Due to a problem with generics in FPC 2.2.0 I introduced @link(TStreamType) as
workaround to reference the actual type of stream used inside the
@link(TStreamWrapper).
@author(Andreas Schneider <[email protected]>)
@created(2007-07-08)
@lastmod(2007-11-14)}
unit UStreamHelper;
{$mode objfpc}{$H+}
interface
uses
Classes, RtlConsts, SysUtils;
type
{@name is the stub for the method which will handle the OnProgress callbacks.
@param(ATotal Specifies the complete size of the operation.)
@param(ACurrent Specifies the current position during the operation.)}
TOnProgressEvent = procedure(ATotal, ACurrent: Cardinal) of object;
{ TFifoStream }
{@abstract(The @name contains special handling for queuing and dequeing. It is
meant to be used as a network queue.)}
TFifoStream = class(TStream)
destructor Destroy; override;
protected
FMemory: Pointer;
FSize, FRealSize, FPosition, FLockOffset: Longint;
FCapacity: Longint;
procedure SetCapacity(ANewCapacity: Longint);
procedure SetPointer(APtr: Pointer; ASize: Longint);
function GetOptimalCapacity(ANewCapacity: Longint): Longint;
function Realloc(var NewCapacity: Longint): Pointer; virtual;
property Capacity: Longint read FCapacity write SetCapacity;
public
function GetSize: Int64; override;
function Read(var Buffer; ACount: Longint): Longint; override;
function Write(const Buffer; ACount: Longint): Longint; override;
function Seek(AOffset: Longint; AOrigin: Word): Longint; override;
procedure LoadFromStream(AStream: TStream);
procedure SaveToStream(AStream: TStream);
procedure LoadFromFile(const FileName: string);
procedure SaveToFile(const FileName: string);
procedure SetSize(ANewSize: Longint); override;
procedure Clear;
procedure Dequeue(ACount: Longint); //<Removes a specified number of bytes from the queue. @param(ACount The number of bytes to remove from the queue.)
procedure Enqueue(const Buffer; ACount: Longint); //<Adds a specified number of bytes from a given buffer to the end of the queue. @param(Buffer The buffer containing the data to enqueue.) @param(ACount The number of bytes to enqueue from the buffer.)
procedure Lock(AOffset, ASize: Longint); //<Restricts the visible area of the stream. @param(AOffset is the starting position of the area.) @param(ASize The size of the area.)
procedure Unlock; //<Removes the restrictions from the stream and resets the visible area to the original size.
property Memory: Pointer read FMemory;
end;
{ TStreamWrapper }
{The @name is just a placeholder for the type used in the
@link(TStreamWrapper). It is currently in place to work around a problem with
generics in fpc 2.2.0}
TStreamType = TFifoStream;
//generic TStreamWrapper<TStreamType> = class(TObject{, IStream})
{@abstract(@name implements @link(IStream) and offers a bunch of functions to
ease reading and writing special types (like @link(Integer)s or @link(String)s.))}
TStreamWrapper = class(TObject)
constructor Create(AStream: TStreamType; AOwnsStream: Boolean = True); //<Creates a new instance of @classname. @param(AStream The underlying stream to perform the actual operations on.) @param(AOwnsStream Defines wheather to free the stream on destruction of @classname or not. Defaults to @false.)
destructor Destroy; override; //<Is called when the current instance of @classname is destroyed. If it owns the underlying stream it is destroyed aswell.
protected
FStream: TStream;
FOwnsStream: Boolean;
function GetStream: TStreamType;
procedure SetStream(AStream: TStreamType);
public
property Raw: TStreamType read GetStream write SetStream; //<Provides raw access to the underlying stream. Useful for manipulation of the stream position and other class specific calls.
function ReadBoolean: Boolean; //<Implementation of @link(IStream.ReadBoolean).
function ReadByte: Byte; //<Implementation of @link(IStream.ReadByte).
function ReadCardinal: Cardinal; //<Implementation of @link(IStream.ReadCardinal).
function ReadInteger: Integer; //<Implementation of @link(IStream.ReadInteger).
function ReadInt64: Int64; //<Implementation of @link(IStream.ReadInt64).
function ReadSmallInt: SmallInt; //<Implementation of @link(IStream.ReadSmallInt).
function ReadWord: Word; //<Implementation of @link(IStream.ReadWord).
function ReadString: string; //<Implementation of @link(IStream.ReadString).
function ReadStringFixed(ALength: Integer): string; //<Implementation of @link(IStream.ReadStringFixed).
procedure WriteBoolean(AValue: Boolean); //<Implementation of @link(IStream.WriteBoolean).
procedure WriteByte(AValue: Byte); //<Implementation of @link(IStream.WriteByte).
procedure WriteCardinal(AValue: Cardinal); //<Implementation of @link(IStream.WriteCardinal).
procedure WriteInteger(AValue: Integer); //<Implementation of @link(IStream.WriteInteger).
procedure WriteInt64(AValue: Int64); //<Implementation of @link(IStream.WriteInt64).
procedure WriteSmallInt(AValue: SmallInt); //<Implementation of @link(IStream.WriteSmallInt).
procedure WriteWord(AValue: Word); //<Implementation of @link(IStream.WriteWord).
procedure WriteString(AValue: string); //<Implementation of @link(IStream.WriteString).
procedure WriteStringFixed(AValue: string; ALength: Integer); //<Implementation of @link(IStream.WriteStringFixed).
function Read(ABuffer: PByte; ACount: Cardinal): Cardinal; //<Implementation of @link(IStream.Read).
function Write(ABuffer: PByte; ACount: Cardinal): Cardinal; //<Implementation of @link(IStream.Write).
procedure Skip(ACount: Cardinal); //<Implementation of @link(IStream.Skip).
end;
{@name is used to have a progress (see @link(TOnProgressEvent)) for a copy
action of the content of one stream into another. This is especially useful
for writing and reading to @link(TFileStream).
@param(ASource The stream from which the content is copied.)
@param(ATarget The stream to which the content is copied.)
@param(ACount Specifies the amount to copy. 0 means, that the whole stream is processed.)
@param(AOnProgress The callback for the @link(TOnProgressEvent). Defaults to @nil.)
@returns(The amount of bytes copied.)}
function StreamCopy(ASource, ATarget: TStream; ACount: Int64; AOnProgress: TOnProgressEvent = nil): Int64;
implementation
function StreamCopy(ASource, ATarget: TStream; ACount: Int64; AOnProgress: TOnProgressEvent = nil): Int64;
var
i, targetSize: Int64;
buffer: array[0..4095] of byte;
begin
Result := 0;
if (ACount = 0) then
begin
//This WILL fail for non-seekable streams...
ASource.Position := 0;
ACount := ASource.Size;
end;
targetSize := ACount;
while ACount > 0 do
begin
if (ACount > SizeOf(buffer)) then
i := SizeOf(Buffer)
else
i := ACount;
i := ASource.Read(buffer, i);
i := ATarget.Write(buffer, i);
if i = 0 then break;
Dec(ACount, i);
Inc(Result, i);
if Assigned(AOnProgress) then
AOnProgress(targetSize, Result);
end;
end;
{ TFifoStream }
const TMSGrow = 4096; { Use 4k blocks. }
destructor TFifoStream.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TFifoStream.SetCapacity(ANewCapacity: Longint);
begin
SetPointer(Realloc(ANewCapacity), FSize);
FCapacity := ANewCapacity;
end;
procedure TFifoStream.SetPointer(APtr: Pointer; ASize: Longint);
begin
FMemory := APtr;
FSize := ASize;
end;
function TFifoStream.GetOptimalCapacity(ANewCapacity: Longint): Longint;
begin
Result := ANewCapacity;
if Result <= 0 then
Result := 0
else
begin
// if growing, grow at least a quarter
if (Result > FCapacity) and (Result < (5 * FCapacity) div 4) then
Result := (5 * FCapacity) div 4;
// round off to block size.
Result := (Result + (TMSGrow-1)) and not (TMSGROW-1);
end;
end;
function TFifoStream.Realloc(var NewCapacity: Longint): Pointer;
begin
NewCapacity := GetOptimalCapacity(NewCapacity);
// Only now check !
if NewCapacity = FCapacity then
Result := FMemory
else
begin
Result := ReAllocMem(FMemory, NewCapacity);
if (Result = nil) and (NewCapacity > 0) then
raise EStreamError.Create(SMemoryStreamError);
end;
end;
function TFifoStream.GetSize: Int64;
begin
Result := FSize;
end;
function TFifoStream.Read(var Buffer; ACount: Longint): Longint;
begin
Result := 0;
If (FSize > 0) and (FPosition - FLockOffset < FSize) then
begin
Result := FSize - (FPosition - FLockOffset);
if Result > ACount then Result := ACount;
Move((FMemory + FPosition)^, Buffer, Result);
FPosition := FPosition + Result;
end;
end;
function TFifoStream.Write(const Buffer; ACount: Longint): Longint;
var
NewPos: Longint;
begin
Unlock;
if ACount = 0 then
Exit(0);
NewPos := FPosition + ACount;
if NewPos > FSize then
begin
if NewPos > FCapacity then
SetCapacity(NewPos);
FSize := NewPos;
end;
System.Move(Buffer, (FMemory + FPosition)^, ACount);
FPosition := NewPos;
Result := ACount;
end;
function TFifoStream.Seek(AOffset: Longint; AOrigin: Word): Longint;
begin
case AOrigin of
soFromBeginning : FPosition := AOffset + FLockOffset;
soFromEnd : FPosition := FSize + AOffset + FLockOffset;
soFromCurrent : FPosition := FPosition + AOffset;
end;
Result := FPosition - FLockOffset;
end;
procedure TFifoStream.LoadFromStream(AStream: TStream);
begin
Unlock;
AStream.Position := 0;
SetSize(AStream.Size);
If FSize > 0 then AStream.ReadBuffer(FMemory^,FSize);
end;
procedure TFifoStream.SaveToStream(AStream: TStream);
begin
if FSize > 0 then AStream.WriteBuffer((FMemory + FLockOffset)^, FSize);
end;
procedure TFifoStream.LoadFromFile(const FileName: string);
var
S: TFileStream;
begin
S := TFileStream.Create(FileName, fmOpenRead);
try
LoadFromStream(S);
finally
S.free;
end;
end;
procedure TFifoStream.SaveToFile(const FileName: string);
var
S: TFileStream;
begin
S := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(S);
finally
S.free;
end;
end;
procedure TFifoStream.SetSize(ANewSize: Longint);
begin
Unlock;
SetCapacity(ANewSize);
FSize := ANewSize;
if FPosition > FSize then
FPosition := FSize;
end;
procedure TFifoStream.Clear;
begin
FSize := 0;
FRealSize := 0;
FPosition := 0;
FLockOffset := 0;
SetCapacity(0);
end;
procedure TFifoStream.Dequeue(ACount: Longint);
var
newCapacity, newSize: Longint;
queue, newMemory: Pointer;
begin
Unlock;
if ACount >= FSize then
begin
Size := 0;
Exit;
end;
queue := FMemory + ACount;
newSize := FSize - ACount;
newCapacity := GetOptimalCapacity(newSize);
if newCapacity <> FCapacity then
begin
newMemory := GetMem(newCapacity);
System.Move(queue^, newMemory^, newSize);
if (newMemory = nil) and (newCapacity > 0) then
raise EStreamError.Create(SMemoryStreamError);
FreeMem(FMemory);
FMemory := newMemory;
FCapacity := newCapacity;
end else
System.Move(queue^, FMemory^, newSize);
FSize := newSize;
if FPosition > ACount then
Dec(FPosition, ACount)
else
FPosition := 0;
end;
procedure TFifoStream.Enqueue(const Buffer; ACount: Longint);
var
oldPos: Int64;
begin
Unlock;
oldPos := FPosition;
FPosition := FSize;
Write(Buffer, ACount);
FPosition := oldPos;
end;
procedure TFifoStream.Lock(AOffset, ASize: Longint);
begin
if (FLockOffset <> 0) or (FRealSize <> 0) then Exit;
FLockOffset := AOffset;
FRealSize := FSize;
FSize := ASize;
end;
procedure TFifoStream.Unlock;
begin
if (FLockOffset = 0) and (FRealSize = 0) then Exit;
FLockOffset := 0;
FSize := FRealSize;
FRealSize := 0;
end;
{ TStreamWrapper }
constructor TStreamWrapper.Create(AStream: TStreamType; AOwnsStream: Boolean);
begin
inherited Create;
FStream := TStream(AStream);
FOwnsStream := AOwnsStream;
end;
destructor TStreamWrapper.Destroy;
begin
if FOwnsStream and Assigned(FStream) then FreeAndNil(FStream);
inherited Destroy;
end;
function TStreamWrapper.GetStream: TStreamType;
begin
Result := TStreamType(FStream);
end;
procedure TStreamWrapper.SetStream(AStream: TStreamType);
begin
FStream := TStream(AStream);
end;
function TStreamWrapper.ReadBoolean: Boolean;
begin
if not Assigned(FStream) then Exit(False);
FStream.Read(Result, SizeOf(Boolean));
end;
function TStreamWrapper.ReadByte: Byte;
begin
if not Assigned(FStream) then Exit(0);
FStream.Read(Result, SizeOf(Byte));
end;
function TStreamWrapper.ReadCardinal: Cardinal;
begin
if not Assigned(FStream) then Exit(0);
FStream.Read(Result, SizeOf(Cardinal));
end;
function TStreamWrapper.ReadInteger: Integer;
begin
if not Assigned(FStream) then Exit(0);
FStream.Read(Result, SizeOf(Integer));
end;
function TStreamWrapper.ReadInt64: Int64;
begin
if not Assigned(FStream) then Exit(0);
FStream.Read(Result, SizeOf(Int64));
end;
function TStreamWrapper.ReadSmallInt: SmallInt;
begin
if not Assigned(FStream) then Exit(0);
FStream.Read(Result, SizeOf(SmallInt));
end;
function TStreamWrapper.ReadWord: Word;
begin
if not Assigned(FStream) then Exit(0);
FStream.Read(Result, SizeOf(Word));
end;
function TStreamWrapper.ReadString: string;
begin
if not Assigned(FStream) then Exit('');
Result := ReadStringFixed(ReadInteger);
end;
function TStreamWrapper.ReadStringFixed(ALength: Integer): string;
begin
if not Assigned(FStream) then Exit('');
SetLength(Result, ALength);
FStream.Read(PChar(Result)^, ALength);
end;
procedure TStreamWrapper.WriteBoolean(AValue: Boolean);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(Boolean));
end;
procedure TStreamWrapper.WriteByte(AValue: Byte);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(Byte));
end;
procedure TStreamWrapper.WriteCardinal(AValue: Cardinal);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(Cardinal));
end;
procedure TStreamWrapper.WriteInteger(AValue: Integer);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(Integer));
end;
procedure TStreamWrapper.WriteInt64(AValue: Int64);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(Int64));
end;
procedure TStreamWrapper.WriteSmallInt(AValue: SmallInt);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(SmallInt));
end;
procedure TStreamWrapper.WriteWord(AValue: Word);
begin
if not Assigned(FStream) then Exit;
FStream.Write(AValue, SizeOf(Word));
end;
procedure TStreamWrapper.WriteString(AValue: string);
var
stringLength: Integer;
begin
if not Assigned(FStream) then Exit;
stringLength := Length(AValue);
WriteInteger(stringLength);
WriteStringFixed(AValue, stringLength);
end;
procedure TStreamWrapper.WriteStringFixed(AValue: string; ALength: Integer);
begin
if not Assigned(FStream) then Exit;
FStream.Write(PChar(AValue)^, ALength);
end;
function TStreamWrapper.Read(ABuffer: PByte; ACount: Cardinal): Cardinal;
begin
Result := FStream.Read(ABuffer^, ACount);
end;
function TStreamWrapper.Write(ABuffer: PByte; ACount: Cardinal): Cardinal;
begin
Result := FStream.Write(ABuffer^, ACount);
end;
procedure TStreamWrapper.Skip(ACount: Cardinal);
begin
FStream.Seek(ACount, soFromCurrent);
end;
end.