-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Stream.pas
658 lines (555 loc) · 16.2 KB
/
GS.Stream.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
///-------------------------------------------------------------------------------
/// Title : GS.Stream
/// Short Desc : Lowlevel stream type write.
/// Source : https://github.com/VincentGsell
/// Aim : - Store type in stream in fast and easy way.
///-------------------------------------------------------------------------------
unit GS.Stream;
{$I GSCore.inc}
interface
{$ifdef FPC}
uses Sysutils, Classes;
{$else}
uses System.Sysutils, System.Classes;
{$endif}
procedure WriteLongInt(Stream: TStream; const Value: LongInt);
procedure WriteInteger(Stream: TStream; const Value: Integer);
procedure WriteInt32(Stream: TStream; const Value: Int32);
procedure WriteInt64(Stream: TStream; const Value: Int64);
procedure WriteUInt64(Stream: TStream; const Value: UInt64);
procedure WriteUInt32(Stream: TStream; const Value: UINT32);
procedure WriteByte(Stream: TStream; const Value: Byte);
procedure WriteBoolean(Stream: TStream; const Value: Boolean);
procedure WriteDouble(Stream: TStream; const Value: Double);
procedure WriteSingle(Stream: TStream; const Value: Single);
procedure WriteDateTime(Stream: TStream; const Value: TDateTime);
function ReadLongInt(Stream: TStream): LongInt;
function ReadInteger(Stream: TStream): Integer;
function ReadInt32(Stream: TStream): Int32;
function ReadInt64(Stream: TStream): Int64;
function ReadUInt64(Stream: TStream): UInt64;
function ReadUInt32(Stream: TStream): UInt32;
function ReadByte(Stream: TStream): Byte;
function ReadBoolean(Stream: TStream): Boolean;
function ReadDouble(Stream: TStream): Double;
function ReadSingle(Stream: TStream): Single;
function ReadDateTime(Stream: TStream): TDateTime;
Type TGSStringEncoding = (ASCIIEncoding, AnsiEncoding, UTF7Enconding, UTF8Encoding, UnicodeEncoding);
procedure WriteString(Stream: TStream; Const Data: String; const Encoding : TGSStringEncoding = TGSStringEncoding.UTF8Encoding);
function ReadString(Stream: TStream; const Encoding : TGSStringEncoding = TGSStringEncoding.UTF8Encoding): String;
function ReadBytes(Stream : TStream) : TBytes; //Bytes from stream with size signature.
procedure WriteBytes(stream : TStream; const aBytes : TBytes);
function StreamToBytes(Stream : TStream) : TBytes; //Pure conversion
procedure BytesToStream(stream : TStream; const aBytes : TBytes);
Procedure WriteStream(Stream : TStream; const SourceStream : TStream);
Procedure ReadStream(Stream : TStream; var DestinationStream : TStream);
//Write without prefixed size by default.
procedure WriteRAWStringUTF8(Stream : TStream; Const Data : String; Const ByteLenPrefix : Boolean = false);
function ReadRAWStringUTF8(Stream : TStream; Const ByteLenPrefix : Boolean = false) : String;
//Procedure Write_UTF82Bytes(var Buffer : TBytes; const Data : UTF8String); Inline;
Type TUint32Array = Array of Uint32;
function ReadArrayOfUINT32(Stream : TStream) : TUint32Array;
procedure WriteArrayOfUINT32(Stream : TStream; Const Source : Array of UINT32);
Type
TGSStream = class helper For TStream
procedure stringWrite(str : String);
procedure int32write(aInt : Int32);
procedure int64write(aInt : Int64);
procedure singleWrite(aSingle : single);
procedure singleArrayWrite(aSingleArray : TArray<Single>);
function stringRead : string;
function int32Read : Int32;
function int64Read : Int64;
function singleRead: single;
function singleArrayRead : TArray<Single>;
end;
IGSStream = interface
procedure stringWrite(str : String);
procedure int32write(aInt : Int32);
procedure int64write(aInt : Int64);
procedure singleWrite(aSingle : single);
procedure singleArrayWrite(aSingleArray : TArray<Single>);
function stringRead : string;
function int32Read : Int32;
function int64Read : Int64;
function singleRead: single;
function singleArrayRead : TArray<Single>;
procedure loadFromStream(aStream : TStream);
procedure loadFromBytes(aBytes : TBytes);
procedure saveToStream(aStream : TStream);
procedure loadFromFile(aFile : String);
procedure SaveToFile(aFile : String);
procedure setPos(pos : Int64);
function getPos : Int64;
function AsStream : TStream;
function AsBytes : TBytes;
procedure clear;
end;
TGSStreamImpl = class(TInterfacedObject, IGSStream)
private
protected
FMustDispose : Boolean;
FMem : TStream;
public
Constructor Create(Const SourceStream : TStream = Nil); reintroduce; overload;
Constructor Create(Const SourceFile : String); reintroduce; Overload;
Destructor Destroy; Override;
procedure stringWrite(str : String);
procedure int32write(aInt : Int32);
procedure int64write(aInt : Int64);
procedure singleWrite(aSingle : single);
procedure singleArrayWrite(aSingleArray : TArray<Single>);
function stringRead : string;
function int32Read : Int32;
function int64Read : Int64;
function singleRead: single;
function singleArrayRead : TArray<Single>;
procedure loadFromStream(aStream : TStream);
procedure loadFromBytes(aBytes : TBytes);
procedure saveToStream(aStream : TStream);
procedure loadFromFile(aFile : String);
procedure SaveToFile(aFile : String);
procedure setPos(pos : Int64);
function getPos : Int64;
procedure clear;
function AsStream : TStream;
function AsBytes : TBytes;
end;
implementation
//Procedure Write_UTF82Bytes(var Buffer : TBytes; const Data : UTF8String);
//begin
// Move(Data[1], Buffer[0], Length(Data));
// buffer := TEncoding.UTF8.GetBytes(Data);
//end;
function ReadArrayOfUINT32(Stream : TStream) : TUint32Array;
var l : Uint32;
begin
l := ReadUint32(Stream);
if l>0 then
begin
SetLength(result,l);
Stream.Read(result[0],l*SizeOf(UInt32));
end;
end;
procedure WriteArrayOfUINT32(Stream : TStream; Const Source : Array of UINT32);
var l : Uint32;
begin
l := Length(Source);
WriteUint32(Stream,l);
if l>0 then
begin
Stream.Write(source[0],l*SizeOf(Uint32));
end;
end;
Procedure WriteRAWStringUTF8(Stream : TStream; Const Data : String; Const ByteLenPrefix : Boolean = false);
var b : TStringStream;
l : UInt32;
begin
b := TStringStream.Create(UTF8String(Data));
try
if ByteLenPrefix then
begin
l := UInt32(Abs(b.Size));
Stream.Write(l,SizeOf(UINT32));
end;
Stream.CopyFrom(b,b.Size);
finally
FreeAndNil(b);
end;
end;
function ReadRAWStringUTF8(Stream : TStream; Const ByteLenPrefix : Boolean = false) : String;
var b : TStringStream;
l : UInt32;
begin
l := 0;
b := TStringStream.Create(UTF8String(' ')); //Force UTF8 encoding.
try
if ByteLenPrefix then
begin
Stream.read(l,sizeOf(UINT32));
b.CopyFrom(Stream,l);
end
else
begin
{$ifdef FPC}
//b.WriteString('');
b.CopyFrom(Stream,Stream.Size);
{$else}
b.LoadFromStream(Stream);
{$endif}
end;
result := b.DataString;
finally
FreeAndNil(b);
end;
end;
procedure WriteDateTime(Stream: TStream; const Value: TDateTime);
begin
Stream.Write(Value, SizeOf(TDateTime));
end;
procedure WriteLongInt(Stream: TStream; const Value: LongInt);
begin
Stream.Write(Value, SizeOf(LongInt));
end;
procedure WriteInteger(Stream: TStream; const Value: Integer);
begin
Stream.Write(Value, SizeOf(Integer));
end;
procedure WriteInt32(Stream: TStream; const Value: Int32);
begin
Stream.Write(Value, SizeOf(Int32));
end;
procedure WriteInt64(Stream: TStream; const Value: Int64);
begin
Stream.Write(Value, SizeOf(Int64));
end;
procedure WriteUInt64(Stream: TStream; const Value: UInt64);
begin
Stream.Write(Value, SizeOf(UInt64));
end;
procedure WriteUINT32(Stream: TStream; const Value: UINT32);
begin
Stream.Write(Value, SizeOf(UINT32));
end;
procedure WriteByte(Stream: TStream; const Value: Byte);
begin
Stream.Write(Value,1);
end;
procedure WriteBoolean(Stream: TStream; const Value: Boolean);
var b : Byte;
begin
b := 0;
if Value then
b := 1;
Stream.Write(b, 1);
end;
procedure WriteDouble(Stream: TStream; const Value: Double);
begin
Stream.Write(Value, SizeOf(Double));
end;
procedure WriteSingle(Stream: TStream; const Value: Single);
begin
Stream.Write(Value, SizeOf(Single));
end;
function ReadLongInt(Stream: TStream): LongInt;
begin
Stream.Read(Result, SizeOf(LongInt));
end;
function ReadInteger(Stream: TStream): Integer;
begin
Stream.Read(Result, SizeOf(Integer));
end;
function ReadInt32(Stream: TStream): Int32;
begin
Stream.Read(Result, SizeOf(Int32));
end;
function ReadInt64(Stream: TStream): Int64;
begin
Stream.Read(Result, SizeOf(Int64));
end;
function ReadUInt64(Stream: TStream): UInt64;
begin
Stream.Read(Result, SizeOf(UInt64));
end;
function ReadUint32(Stream: TStream): UINT32;
begin
Stream.Read(Result, SizeOf(UINT32));
end;
function ReadByte(Stream: TStream): Byte;
begin
Stream.Read(Result, 1);
end;
function ReadBoolean(Stream: TStream): Boolean;
var b : Byte;
begin
Stream.Read(b, 1);
Result := b > 0;
end;
function ReadDouble(Stream: TStream): Double;
begin
Stream.Read(Result, SizeOf(Double));
end;
function ReadSingle(Stream: TStream): Single;
begin
Stream.Read(Result, SizeOf(Single));
end;
function ReadDateTime(Stream: TStream): TDateTime;
begin
Stream.Read(Result, SizeOf(TDateTime));
end;
function GetEncoding(a : TGSStringEncoding) : TEncoding;
begin
result := TEncoding.UTF8;
case a of
TGSStringEncoding.ASCIIEncoding: result := TEncoding.ASCII;
TGSStringEncoding.AnsiEncoding: result := TEncoding.ANSI;
TGSStringEncoding.UTF7Enconding: result := TEncoding.UTF7;
TGSStringEncoding.UTF8Encoding: result := TEncoding.UTF8;
TGSStringEncoding.UnicodeEncoding: result := TEncoding.Unicode;
end;
end;
procedure WriteString(Stream: TStream; Const Data: String; const Encoding : TGSStringEncoding = TGSStringEncoding.UTF8Encoding);
var b : TStringStream;
l : UINT32;
begin
{$ifdef FPC}
b := TStringStream.Create(UTF8String(Data)); //TODO lencoding , EncodingUTF8, GetEncoding(Encoding)));
{$else}
b := TStringStream.Create(Data,GetEncoding(Encoding));
{$endif}
try
l := b.Size;
Stream.Write(l,SizeOf(UINT32));
Stream.CopyFrom(b,b.Size);
finally
FreeAndNil(b);
end;
end;
function ReadString(Stream: TStream; const Encoding : TGSStringEncoding = TGSStringEncoding.UTF8Encoding): String;
var
b : TStringStream;
i : UINT32;
begin
result := '';
Stream.read(i,sizeOf(UINT32));
if i>0 then
begin
{$ifdef FPC}
b := TStringStream.Create; //(UTF8String(' ')); //TODO lencoding , EncodingUTF8, GetEncoding(Encoding)));
{$else}
b := TStringStream.Create(' ',GetEncoding(Encoding));
{$endif}
try
b.CopyFrom(Stream,i);
Result := b.DataString;
finally
FreeAndNil(b);
end;
end;
end;
function ReadBytes(Stream: TStream): TBytes;
var li : Int64;
begin
if Assigned(Stream) then
begin
li := ReadInt64(Stream);
if li > 0 then
begin
SetLength(result, li);
Stream.Read(pointer(result)^, length(result));
end;
end
else
SetLength(result, 0);
end;
procedure WriteBytes(stream : TStream; const aBytes : TBytes);
begin
WriteInt64(Stream, length(aBytes));
if length(abytes) > 0 then
stream.Write(pointer(aBytes)^, length(aBytes));
end;
function StreamToBytes(Stream : TStream) : TBytes; //Pure conversion
begin
if Assigned(Stream) then
begin
SetLength(result, Stream.Size-Stream.Position);
Stream.Read(pointer(result)^, length(result));
end
else
SetLength(result, 0);
end;
procedure BytesToStream(stream : TStream; const aBytes : TBytes);
begin
if length(abytes) > 0 then
stream.Write(pointer(aBytes)^, length(aBytes));
end;
Procedure WriteStream(Stream : TStream; const SourceStream : TStream);
var l : Uint32;
begin
l := SourceStream.Size-SourceStream.Position;
WriteInt64(Stream, l);
if l>0 then
Stream.CopyFrom(SourceStream,SourceStream.Size-SourceStream.Position);
end;
Procedure ReadStream(Stream : TStream; var DestinationStream : TStream);
var il : Int64;
begin
il := ReadInt64(Stream);
if il>0 then
DestinationStream.CopyFrom(Stream,il);
end;
{ TGSStream }
function TGSStream.int32Read: Int32;
begin
result := ReadInt32(Self);
end;
procedure TGSStream.int32write(aInt: Int32);
begin
WriteInt32(Self,aInt);
end;
function TGSStream.int64Read: Int64;
begin
result := ReadInt64(Self);
end;
procedure TGSStream.int64write(aInt: Int64);
begin
WriteInt64(Self,aInt);
end;
function TGSStream.singleArrayRead: TArray<Single>;
var l : single;
li : Int32;
i : Integer;
begin
i := ReadInt32(Self);
SetLength(result,i);
for i := 0 to length(result)-1 do
result[i] := readInt32(self);
end;
procedure TGSStream.singleArrayWrite(aSingleArray: TArray<Single>);
var l : single;
begin
WriteInt32(Self,length(aSingleArray));
for l in aSingleArray do
WriteSingle(Self,l);
end;
function TGSStream.singleRead: single;
begin
result := ReadSingle(self);
end;
procedure TGSStream.singleWrite(aSingle: single);
begin
WriteSingle(Self,aSingle);
end;
function TGSStream.stringRead: string;
begin
result := ReadString(self);
end;
procedure TGSStream.stringWrite(str: String);
begin
WriteString(Self,str);
end;
{ TGSStreamImpl }
function TGSStreamImpl.AsBytes: TBytes;
var l : int64;
begin
l := FMem.Position;
FMem.Position := 0;
result := StreamToBytes(FMem);
FMem.Position := l;
end;
function TGSStreamImpl.AsStream: TStream;
begin
result := FMem;
end;
constructor TGSStreamImpl.Create(const SourceStream: TStream);
begin
Inherited Create;
FMustDispose := Not Assigned(SourceStream);
if Assigned(SourceStream) then
FMem := SourceStream
else
FMem := TMemoryStream.Create;
end;
procedure TGSStreamImpl.clear;
begin
FMem.Size := 0;
end;
constructor TGSStreamImpl.Create(const SourceFile: String);
begin
Inherited Create;
FMustDispose := True;
FMem := TFileStream.Create(SourceFile,fmOpenReadWrite);
end;
destructor TGSStreamImpl.Destroy;
begin
if FMustDispose then
FreeAndNil(FMem);
inherited;
end;
function TGSStreamImpl.getPos: Int64;
begin
result := FMem.Position;
end;
function TGSStreamImpl.int32Read: Int32;
begin
result := FMem.int32Read;
end;
procedure TGSStreamImpl.int32write(aInt: Int32);
begin
FMem.int32write(aInt);
end;
function TGSStreamImpl.int64Read: Int64;
begin
result := FMem.int64Read;
end;
procedure TGSStreamImpl.int64write(aInt: Int64);
begin
FMem.int64write(aInt);
end;
procedure TGSStreamImpl.loadFromBytes(aBytes: TBytes);
begin
BytesToStream(FMem,aBytes);
FMem.Position := 0;
end;
procedure TGSStreamImpl.loadFromFile(aFile: String);
var l : TMemoryStream;
begin
l := TMemoryStream.Create;
try
l.LoadFromFile(aFile);
LoadFromStream(l);
setPos(0);
finally
FreeAndNil(l);
end;
end;
procedure TGSStreamImpl.loadFromStream(aStream: TStream);
begin
FMem.CopyFrom(aStream);
end;
procedure TGSStreamImpl.SaveToFile(aFile: String);
var l : TMemoryStream;
begin
l := TMemoryStream.Create;
try
setPos(0);
saveToStream(l);
l.SaveToFile(aFile);
finally
FreeAndNil(l);
end;
end;
procedure TGSStreamImpl.saveToStream(aStream: TStream);
begin
FMem.Position := 0;
aStream.CopyFrom(FMem);
end;
procedure TGSStreamImpl.setPos(pos: Int64);
begin
FMem.Position := pos;
end;
function TGSStreamImpl.singleArrayRead: TArray<Single>;
begin
result := FMem.singleArrayRead;
end;
procedure TGSStreamImpl.singleArrayWrite(aSingleArray: TArray<Single>);
begin
FMem.singleArrayWrite(aSingleArray);
end;
function TGSStreamImpl.singleRead: single;
begin
result := FMem.singleRead;
end;
procedure TGSStreamImpl.singleWrite(aSingle: single);
begin
FMem.singleWrite(aSingle);
end;
function TGSStreamImpl.stringRead: string;
begin
result := FMem.stringRead;
end;
procedure TGSStreamImpl.stringWrite(str: String);
begin
FMem.stringWrite(str);
end;
end.