Menu

[r3204]: / branches / experimental / Src / TrunkSrc / UDataStreamIO.pas  Maximize  Restore  History

Download this file

419 lines (362 with data), 13.4 kB

  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
{
* 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/
*
* Copyright (C) 2006-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Stream wrappers read / write data from / to a formatted data stream. Classes
* are provided to read from text or binary streams that conform to the data
* formatting rules. Another class writes data to formatted binary streams.
}
unit UDataStreamIO;
interface
uses
// Delphi
SysUtils, Classes,
// DelphiDabbler library
PJStreamWrapper;
type
/// Set of values indicating whether a stream wrapper object owns, and hence
/// frees, its stream and encoding objects.
TDataStreamOwnerships = set of (
dsOwnsStream, // object owns stream
dsOwnsEncoding // object owns encoding: ignored for standard encodings
);
/// <summary>
/// Base class of all classes that read and write a formatted data stream.
/// Deals with encoding used for stream.
/// </summary>
TDataStreamIO = class(TPJStreamWrapper)
strict private
/// Value of Encoding property.
var fEncoding: TEncoding;
/// Indicates if object owns encoding.
var fOwnsEncoding: Boolean;
strict protected
/// Encoding used when reading / writing strings.
property Encoding: TEncoding read fEncoding;
public
/// Creates object that wraps a specified stream. If OwnsObject is true
/// object will free the stream on destruction. Uses UTF-8 encoding.
constructor Create(const Stream: TStream;
const OwnsStream: Boolean = False); overload; override;
/// Creates object that wraps a specified stream and uses a specified
/// encoding to read or write strings. Ownerships indicate if object owns
/// either the stream or the encoding: owned objects will be freed on
/// destruction. If Encoding is nil UTF-8 will be used.
constructor Create(const Stream: TStream; const Encoding: TEncoding;
const OwnerShips: TDataStreamOwnerships); reintroduce; overload; virtual;
/// Tears down object, freeing any owned objects. NOTE: standard encodings
/// are never freed regardless of ownership.
destructor Destroy; override;
end;
/// <summary>
/// Abstract base class for all object that read formatted data from a
/// stream. Defines a standard interface and supplies common functionality.
/// </summary>
TDataStreamReader = class abstract(TDataStreamIO)
strict protected
/// Reads CharCount bytes and converts them to a Unicode string.
function ReadString(CharCount: Integer): UnicodeString;
public
/// Reads a single byte.
function ReadByte: Byte; virtual; abstract;
/// Reads a 16 bit integer value.
function ReadInt16: SmallInt; virtual; abstract;
/// Reads a 32 bit integer value.
function ReadInt32: LongInt; virtual; abstract;
/// Reads a 64 bit integer value.
function ReadInt64: Int64; virtual; abstract;
/// Reads ByteCount bytes and returns them unprocessed.
function ReadRawData(ByteCount: Integer): TBytes;
/// Reads a number of bytes specified by a preceding 16 bit integer and
/// returns them unprocessed.
function ReadSizedRawData16: TBytes;
/// Reads a number of bytes specified by a preceding 32 bit integer and
/// returns them unprocessed.
function ReadSizedRawData32: TBytes;
/// Reads ByteCount formatted bytes.
function ReadBytes(ByteCount: Integer): TBytes; virtual; abstract;
/// Reads a number of formatted bytes specified by a preceding 16 bit
/// integer.
function ReadSizedBytes16: TBytes;
/// Reads a number of formatted bytes specified by a preceding 32 bit
/// integer.
function ReadSizedBytes32: TBytes;
/// Reads a number of bytes specified by a preceding 16 bit integer and
/// converts them into a Unicode string using the encoding specified by
/// the Encoding property.
function ReadSizedString16: UnicodeString;
/// Reads a number of bytes specified by a preceding 32 bit integer and
/// converts them into a Unicode string using the encoding specified by
/// the Encoding property.
function ReadSizedString32: UnicodeString;
end;
/// <summary>
/// Class that reads binary data from a stream.
/// </summary>
/// <remarks>
/// Byte array and string data may be preceded by integer values that provide
/// the length of the following data. String data is converted from its
/// binary representation using the encoding specified by the Encoding
/// property.
/// </remarks>
TBinaryStreamReader = class(TDataStreamReader)
public
/// Reads a single byte from its raw binary representation
function ReadByte: Byte; override;
/// Reads a 16 bit integer value from its raw binary representation.
function ReadInt16: SmallInt; override;
/// Reads a 32 bit integer value from its raw binary representation.
function ReadInt32: LongInt; override;
/// Reads a 64 bit integer value from its raw binary representation.
function ReadInt64: Int64; override;
/// Reads ByteCount bytes from their raw binary representation. In this
/// class this method is the same as ReadRawData.
function ReadBytes(ByteCount: Integer): TBytes; override;
end;
/// <summary>
/// Class that reads text formatted data from a stream.
/// </summary>
/// <remarks>
/// <para>Numeric data is represented by hex characters that are stored as
/// ASCII characters. Therefore only encodings in which the hex characters
/// are the same as the ASCII characters can be used.</para>
/// <para>Byte array and string data may be preceded by hex encoded integer
/// values that provide the length of the following data. String data is
/// converted from its binary representation using the encoding specified
/// by the Encoding property.</para>
/// </remarks>
TTextStreamReader = class(TDataStreamReader)
strict private
function ReadHexDigits(const Count: Integer): LongInt;
public
/// Creates object that wraps a specified stream and uses a specified
/// encoding to read or write strings. Ownerships indicate if object owns
/// either the stream or the encoding: owned objects will be freed on
/// destruction. NOTE: The encoding must be a TMBCSEncoding where hex
/// digits each encode as single bytes.
constructor Create(const Stream: TStream; const Encoding: TEncoding;
const OwnerShips: TDataStreamOwnerships); overload; override;
/// Reads a single byte encoded as 2 hex characters.
function ReadByte: Byte; override;
/// Reads a 16 bit integer encoded as 4 hex characters.
function ReadInt16: SmallInt; override;
/// Reads a 32 bit integer encoded as 8 hex characters.
function ReadInt32: LongInt; override;
/// Reads a 64 bit integer encoded as 4 hex characters.
function ReadInt64: Int64; override;
/// Reads ByteCount bytes. Each byte is encoded as 2 hex characters. In
/// this class this method is *not* the same as ReadRawData.
function ReadBytes(ByteCount: Integer): TBytes; override;
end;
/// <summary>
/// Class that writes data to a stream using its internal binary
/// representation.
/// </summary>
/// <remarks>
/// Byte array and string data may have size information prepended. Strings
/// are converted to bytes arrays before being written using the encoding
/// specified by the Encoding property.
/// </remarks>
TBinaryStreamWriter = class(TDataStreamIO)
public
/// Writes a 16 bit integer in binary.
procedure WriteInt16(const I: SmallInt);
/// Writes a 32 bit integer in binary.
procedure WriteInt32(const I: LongInt);
/// Writes an array of bytes in binary. The size of the array is not
/// recorded.
procedure WriteBytes(const B: TBytes);
/// Writes an array of bytes in binary, prepended by a 16 bit integer
/// specifying the number of bytes in the array.
procedure WriteSizedBytes16(const B: TBytes);
/// Writes an array of bytes in binary, prepended by a 32 bit integer
/// specifying the number of bytes in the array.
procedure WriteSizedBytes32(const B: TBytes);
/// Encodes the string into an array of bytes and writes it, prepended by
/// a 16 bit integer specifying the number of bytes written.
procedure WriteSizedString16(const Str: UnicodeString);
/// Encodes the string into an array of bytes and writes it, prepended by
/// a 32 bit integer specifying the number of bytes written.
procedure WriteSizedString32(const Str: UnicodeString);
end;
implementation
uses
// Project
UEncodings;
{ TDataStreamIO }
constructor TDataStreamIO.Create(const Stream: TStream;
const OwnsStream: Boolean);
var
Ownerships: TDataStreamOwnerships; // object ownership requirements
begin
Ownerships := [];
if OwnsStream then
Include(Ownerships, dsOwnsStream);
Create(Stream, nil, Ownerships);
end;
constructor TDataStreamIO.Create(const Stream: TStream;
const Encoding: TEncoding; const Ownerships: TDataStreamOwnerships);
begin
inherited Create(Stream, dsOwnsStream in Ownerships);
if Assigned(Encoding) then
begin
fEncoding := Encoding;
fOwnsEncoding := (dsOwnsEncoding in OwnerShips);
end
else
begin
fEncoding := TEncoding.UTF8;
fOwnsEncoding := True;
end;
end;
destructor TDataStreamIO.Destroy;
begin
inherited;
if fOwnsEncoding then
TEncodingHelper.FreeEncoding(fEncoding);
end;
{ TDataStreamReader }
function TDataStreamReader.ReadRawData(ByteCount: Integer): TBytes;
begin
if (ByteCount < 0) then
ByteCount := 0;
SetLength(Result, ByteCount);
if ByteCount = 0 then
Exit;
BaseStream.ReadBuffer(Pointer(Result)^, ByteCount);
end;
function TDataStreamReader.ReadSizedBytes16: TBytes;
begin
Result := ReadBytes(ReadInt16);
end;
function TDataStreamReader.ReadSizedBytes32: TBytes;
begin
Result := ReadBytes(ReadInt32);
end;
function TDataStreamReader.ReadSizedRawData16: TBytes;
begin
Result := ReadRawData(ReadInt16);
end;
function TDataStreamReader.ReadSizedRawData32: TBytes;
begin
Result := ReadRawData(ReadInt32);
end;
function TDataStreamReader.ReadSizedString16: UnicodeString;
begin
Result := ReadString(ReadInt16);
end;
function TDataStreamReader.ReadSizedString32: UnicodeString;
begin
Result := ReadString(ReadInt32);
end;
function TDataStreamReader.ReadString(CharCount: Integer): UnicodeString;
var
Bytes: TBytes;
begin
if CharCount <= 0 then
Exit('');
Bytes := ReadRawData(CharCount);
Result := Encoding.GetString(Bytes);
end;
{ TTextStreamReader }
constructor TTextStreamReader.Create(const Stream: TStream;
const Encoding: TEncoding; const OwnerShips: TDataStreamOwnerships);
begin
Assert(not Assigned(Encoding) or (Encoding is TMBCSEncoding),
ClassName + '.Create: Encoding must be TMBCSEncoding descendant');
inherited Create(Stream, Encoding, OwnerShips);
end;
function TTextStreamReader.ReadByte: Byte;
begin
Result := ReadHexDigits(2);
end;
function TTextStreamReader.ReadBytes(ByteCount: Integer): TBytes;
var
I: Integer;
begin
if ByteCount < 0 then
ByteCount := 0;
SetLength(Result, ByteCount);
if ByteCount = 0 then
Exit;
for I := 0 to Pred(ByteCount) do
Result[I] := ReadByte;
end;
function TTextStreamReader.ReadHexDigits(const Count: Integer): LongInt;
begin
Result := StrToInt('$' + ReadString(Count));
end;
function TTextStreamReader.ReadInt16: SmallInt;
begin
Result := ReadHexDigits(4);
end;
function TTextStreamReader.ReadInt32: LongInt;
begin
Result := ReadHexDigits(8);
end;
function TTextStreamReader.ReadInt64: Int64;
begin
Int64Rec(Result).Hi := ReadInt32;
Int64Rec(Result).Lo := ReadInt32;
end;
{ TBinaryStreamReader }
function TBinaryStreamReader.ReadByte: Byte;
begin
BaseStream.ReadBuffer(Result, SizeOf(Result));
end;
function TBinaryStreamReader.ReadBytes(ByteCount: Integer): TBytes;
begin
Result := ReadRawData(ByteCount);
end;
function TBinaryStreamReader.ReadInt16: SmallInt;
begin
BaseStream.ReadBuffer(Result, SizeOf(Result));
end;
function TBinaryStreamReader.ReadInt32: LongInt;
begin
BaseStream.ReadBuffer(Result, SizeOf(Result));
end;
function TBinaryStreamReader.ReadInt64: Int64;
begin
BaseStream.ReadBuffer(Result, SizeOf(Result));
end;
{ TBinaryStreamWriter }
procedure TBinaryStreamWriter.WriteBytes(const B: TBytes);
begin
if Length(B) = 0 then
Exit;
BaseStream.WriteBuffer(Pointer(B)^, Length(B));
end;
procedure TBinaryStreamWriter.WriteInt16(const I: SmallInt);
begin
BaseStream.WriteBuffer(I, SizeOf(I));
end;
procedure TBinaryStreamWriter.WriteInt32(const I: Integer);
begin
BaseStream.WriteBuffer(I, SizeOf(I));
end;
procedure TBinaryStreamWriter.WriteSizedBytes16(const B: TBytes);
begin
WriteInt16(Length(B));
WriteBytes(B);
end;
procedure TBinaryStreamWriter.WriteSizedBytes32(const B: TBytes);
begin
WriteInt32(Length(B));
WriteBytes(B);
end;
procedure TBinaryStreamWriter.WriteSizedString16(const Str: UnicodeString);
begin
WriteSizedBytes16(Encoding.GetBytes(Str));
end;
procedure TBinaryStreamWriter.WriteSizedString32(const Str: UnicodeString);
begin
WriteSizedBytes32(Encoding.GetBytes(Str));
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.