1 | unit UStreamHelper;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, DateUtils, SysUtils;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TEndianness = (enBig, enLittle);
|
---|
12 |
|
---|
13 | { TStreamHelper }
|
---|
14 |
|
---|
15 | TStreamHelper = class(TStream)
|
---|
16 | private
|
---|
17 | FOwnStream: Boolean;
|
---|
18 | FStream: TStream;
|
---|
19 | FEndianness: TEndianness;
|
---|
20 | SwapData: Boolean;
|
---|
21 | function GetItem(Index: Integer): Byte;
|
---|
22 | procedure SetEndianness(const AValue: TEndianness);
|
---|
23 | procedure SetItem(Index: Integer; const AValue: Byte);
|
---|
24 | procedure SetStream(AValue: TStream);
|
---|
25 | public
|
---|
26 | procedure Assign(Source: TStreamHelper);
|
---|
27 | procedure WriteByte(Data: Byte);
|
---|
28 | procedure WriteWord(Data: Word);
|
---|
29 | procedure WriteCardinal(Data: Cardinal);
|
---|
30 | procedure WriteInt64(Data: Int64);
|
---|
31 | procedure WriteString(Data: string);
|
---|
32 | procedure WriteShortString(Data: ShortString);
|
---|
33 | procedure WriteAnsiString(Data: string);
|
---|
34 | procedure WriteUnixTime(Data: TDateTime);
|
---|
35 | procedure WriteDouble(Value: Double);
|
---|
36 | procedure WriteSingle(Value: Single);
|
---|
37 | procedure WriteStream(AStream: TStream; Count: Integer);
|
---|
38 | procedure WriteStreamPart(AStream: TStream; Count: Integer);
|
---|
39 | function ReadByte: Byte;
|
---|
40 | function ReadWord: Word;
|
---|
41 | function ReadCardinal: Cardinal;
|
---|
42 | function ReadInt64: Int64;
|
---|
43 | function ReadString(Length: Integer): string;
|
---|
44 | function ReadShortString: string;
|
---|
45 | function ReadAnsiString: string;
|
---|
46 | function ReadStringTerminated(Terminator: string = #0): string;
|
---|
47 | function ReadUnixTime: TDateTime;
|
---|
48 | function ReadDouble: Double;
|
---|
49 | function ReadSingle: Single;
|
---|
50 | procedure ReadStream(AStream: TStream; Count: Integer);
|
---|
51 | procedure ReadStreamPart(AStream: TStream; Count: Integer);
|
---|
52 | function EqualTo(Source: TStream): Boolean;
|
---|
53 | function Sum: Byte;
|
---|
54 | procedure FillByte(Data: Byte; Count: Integer);
|
---|
55 | constructor Create; overload;
|
---|
56 | constructor Create(AStream: TStream); overload;
|
---|
57 | procedure Clear;
|
---|
58 | destructor Destroy; override;
|
---|
59 | function GetSize: Int64; override;
|
---|
60 | procedure SetSize(NewSize: Longint); override;
|
---|
61 | function Seek(Offset: Longint; Origin: Word): Longint; override;
|
---|
62 | function Read(var Buffer; Count: Longint): Longint; override;
|
---|
63 | function Write(const Buffer; Count: Longint): Longint; override;
|
---|
64 | property Endianness: TEndianness read FEndianness write SetEndianness;
|
---|
65 | property Stream: TStream read FStream write SetStream;
|
---|
66 | property Items[Index: Integer]: Byte read GetItem write SetItem; default;
|
---|
67 | property OwnStream: Boolean read FOwnStream write FOwnStream;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | implementation
|
---|
71 |
|
---|
72 | { TStreamHelper }
|
---|
73 |
|
---|
74 | function TStreamHelper.ReadAnsiString: string;
|
---|
75 | var
|
---|
76 | StringLength: Longint;
|
---|
77 | begin
|
---|
78 | StringLength := 0;
|
---|
79 | FStream.ReadBuffer(StringLength, SizeOf(StringLength));
|
---|
80 | Result := ReadString(StringLength);
|
---|
81 | end;
|
---|
82 |
|
---|
83 | function TStreamHelper.ReadStringTerminated(Terminator: string = #0): string;
|
---|
84 | var
|
---|
85 | Data: Char;
|
---|
86 | I: Integer;
|
---|
87 | OldPosition: Integer;
|
---|
88 | begin
|
---|
89 | OldPosition := FStream.Position;
|
---|
90 | Result := '';
|
---|
91 | I := 1;
|
---|
92 | repeat
|
---|
93 | if FStream.Position >= FStream.Size then Break;
|
---|
94 | Data := Chr(ReadByte);
|
---|
95 | if Data <> Terminator[I] then begin
|
---|
96 | Result := Result + Data;
|
---|
97 | I := 1;
|
---|
98 | end else Inc(I);
|
---|
99 | until I > Length(Terminator);
|
---|
100 | if not (I > Length(Terminator)) then begin
|
---|
101 | Result := '';
|
---|
102 | FStream.Position := OldPosition;
|
---|
103 | end;
|
---|
104 | end;
|
---|
105 |
|
---|
106 | function TStreamHelper.ReadByte: Byte;
|
---|
107 | begin
|
---|
108 | Result := 0;
|
---|
109 | FStream.ReadBuffer(Result, SizeOf(Byte));
|
---|
110 | end;
|
---|
111 |
|
---|
112 | function TStreamHelper.ReadCardinal: Cardinal;
|
---|
113 | begin
|
---|
114 | Result := 0;
|
---|
115 | FStream.ReadBuffer(Result, SizeOf(Cardinal));
|
---|
116 | if SwapData then Result := SwapEndian(Result);
|
---|
117 | end;
|
---|
118 |
|
---|
119 | function TStreamHelper.ReadInt64: Int64;
|
---|
120 | begin
|
---|
121 | Result := 0;
|
---|
122 | FStream.ReadBuffer(Result, SizeOf(Int64));
|
---|
123 | if SwapData then Result := SwapEndian(Result);
|
---|
124 | end;
|
---|
125 |
|
---|
126 | function TStreamHelper.ReadString(Length: Integer): string;
|
---|
127 | begin
|
---|
128 | if Length > 0 then begin
|
---|
129 | SetLength(Result, Length);
|
---|
130 | FStream.Read(Result[1], Length);
|
---|
131 | end else Result := '';
|
---|
132 | end;
|
---|
133 |
|
---|
134 | function TStreamHelper.ReadShortString: string;
|
---|
135 | var
|
---|
136 | Count: Byte;
|
---|
137 | begin
|
---|
138 | Count := 0;
|
---|
139 | FStream.ReadBuffer(Count, 1);
|
---|
140 | Result := ReadString(Count);
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure TStreamHelper.ReadStream(AStream: TStream; Count: Integer);
|
---|
144 | //var
|
---|
145 | // Buffer: array of Byte;
|
---|
146 | begin
|
---|
147 | AStream.Position := 0;
|
---|
148 | AStream.CopyFrom(Self, Count);
|
---|
149 | (*if Count > 0 then begin
|
---|
150 | SetLength(Buffer, Count);
|
---|
151 | FStream.ReadBuffer(Buffer[0], Count);
|
---|
152 | AStream.Size := Count;
|
---|
153 | AStream.Position := 0;
|
---|
154 | AStream.Write(Buffer[0], Count);
|
---|
155 | end;*)
|
---|
156 | end;
|
---|
157 |
|
---|
158 | procedure TStreamHelper.ReadStreamPart(AStream: TStream; Count: Integer);
|
---|
159 | var
|
---|
160 | Buffer: array of Byte;
|
---|
161 | begin
|
---|
162 | if Count > 0 then begin
|
---|
163 | SetLength(Buffer, Count);
|
---|
164 | FStream.ReadBuffer(Buffer[0], Count);
|
---|
165 | if AStream.Size < (AStream.Position + Count) then
|
---|
166 | AStream.Size := AStream.Position + Count;
|
---|
167 | AStream.Write(Buffer[0], Count);
|
---|
168 | end;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | function TStreamHelper.EqualTo(Source: TStream): Boolean;
|
---|
172 | const
|
---|
173 | BlockSize = 4096;
|
---|
174 | var
|
---|
175 | Buffer1: array[0..BlockSize - 1] of Byte;
|
---|
176 | Buffer2: array[0..BlockSize - 1] of Byte;
|
---|
177 | BufferLength: Integer;
|
---|
178 | OldPos1, OldPos2: Integer;
|
---|
179 | begin
|
---|
180 | OldPos1 := Source.Position;
|
---|
181 | Source.Position := 0;
|
---|
182 | OldPos2 := Position;
|
---|
183 | Position := 0;
|
---|
184 | Result := True;
|
---|
185 | if Source.Size = Size then begin
|
---|
186 | while Source.Position < Source.Size do begin
|
---|
187 | BufferLength := Source.Read(Buffer1, BlockSize);
|
---|
188 | Read(Buffer2, BlockSize);
|
---|
189 | if not CompareMem(@Buffer1, @Buffer2, BufferLength) then begin
|
---|
190 | Result := False;
|
---|
191 | Break;
|
---|
192 | end;
|
---|
193 | end;
|
---|
194 | end else Result := False;
|
---|
195 | Source.Position := OldPos1;
|
---|
196 | Position := OldPos2;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TStreamHelper.WriteStreamPart(AStream: TStream; Count: Integer);
|
---|
200 | var
|
---|
201 | Buffer: array of Byte;
|
---|
202 | begin
|
---|
203 | if Count > AStream.Size then Count := AStream.Size; // Limit max. stream size
|
---|
204 | if Count > 0 then begin
|
---|
205 | SetLength(Buffer, Count);
|
---|
206 | AStream.Read(Buffer[0], Count);
|
---|
207 | FStream.Write(Buffer[0], Count);
|
---|
208 | end;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | constructor TStreamHelper.Create;
|
---|
212 | begin
|
---|
213 | inherited;
|
---|
214 | Endianness := enLittle;
|
---|
215 | FStream := TMemoryStream.Create;
|
---|
216 | FOwnStream := True;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | constructor TStreamHelper.Create(AStream: TStream);
|
---|
220 | begin
|
---|
221 | inherited Create;
|
---|
222 | Endianness := enLittle;
|
---|
223 | FStream := AStream;
|
---|
224 | FOwnStream := False;
|
---|
225 | end;
|
---|
226 |
|
---|
227 | procedure TStreamHelper.Clear;
|
---|
228 | begin
|
---|
229 | Stream.Size := 0;
|
---|
230 | end;
|
---|
231 |
|
---|
232 | destructor TStreamHelper.Destroy;
|
---|
233 | begin
|
---|
234 | if FOwnStream then FStream.Free;
|
---|
235 | inherited Destroy;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | function TStreamHelper.GetSize: Int64;
|
---|
239 | begin
|
---|
240 | Result := Stream.Size;
|
---|
241 | end;
|
---|
242 |
|
---|
243 | procedure TStreamHelper.SetSize(NewSize: Longint);
|
---|
244 | begin
|
---|
245 | Stream.Size := NewSize;
|
---|
246 | end;
|
---|
247 |
|
---|
248 | function TStreamHelper.Seek(Offset: Longint; Origin: Word): Longint;
|
---|
249 | begin
|
---|
250 | Result := Stream.Seek(Offset, Origin);
|
---|
251 | end;
|
---|
252 |
|
---|
253 | function TStreamHelper.Read(var Buffer; Count: Longint): Longint;
|
---|
254 | begin
|
---|
255 | Result := Stream.Read(Buffer, Count);
|
---|
256 | end;
|
---|
257 |
|
---|
258 | function TStreamHelper.Write(const Buffer; Count: Longint): Longint;
|
---|
259 | begin
|
---|
260 | Result := Stream.Write(Buffer, Count);
|
---|
261 | end;
|
---|
262 |
|
---|
263 | function TStreamHelper.ReadUnixTime: TDateTime;
|
---|
264 | begin
|
---|
265 | Result := UnixToDateTime(ReadCardinal);
|
---|
266 | end;
|
---|
267 |
|
---|
268 | function TStreamHelper.ReadDouble: Double;
|
---|
269 | begin
|
---|
270 | Result := 0;
|
---|
271 | FStream.ReadBuffer(Result, SizeOf(Double));
|
---|
272 | end;
|
---|
273 |
|
---|
274 | function TStreamHelper.ReadSingle: Single;
|
---|
275 | begin
|
---|
276 | Result := 0;
|
---|
277 | FStream.ReadBuffer(Result, SizeOf(Single));
|
---|
278 | end;
|
---|
279 |
|
---|
280 | function TStreamHelper.Sum: Byte;
|
---|
281 | begin
|
---|
282 | Result := 0;
|
---|
283 | FStream.Position := 0;
|
---|
284 | while FStream.Position < FStream.Size do
|
---|
285 | Result := (Result + FStream.ReadByte) and $ff;
|
---|
286 | end;
|
---|
287 |
|
---|
288 | procedure TStreamHelper.FillByte(Data:Byte;Count:Integer);
|
---|
289 | var
|
---|
290 | I: Integer;
|
---|
291 | begin
|
---|
292 | for I := 0 to Count - 1 do
|
---|
293 | WriteByte(Data);
|
---|
294 | end;
|
---|
295 |
|
---|
296 | function TStreamHelper.ReadWord: Word;
|
---|
297 | begin
|
---|
298 | Result := 0;
|
---|
299 | FStream.ReadBuffer(Result, SizeOf(Word));
|
---|
300 | if SwapData then Result := SwapEndian(Result);
|
---|
301 | end;
|
---|
302 |
|
---|
303 | procedure TStreamHelper.SetEndianness(const AValue: TEndianness);
|
---|
304 | begin
|
---|
305 | FEndianness := AValue;
|
---|
306 | {$if defined(FPC_LITTLE_ENDIAN)}
|
---|
307 | SwapData := FEndianness = enBig;
|
---|
308 | {$elseif defined(FPC_BIG_ENDIAN)}
|
---|
309 | SwapData := FEndianness = enLittle;
|
---|
310 | {$endif}
|
---|
311 | end;
|
---|
312 |
|
---|
313 | function TStreamHelper.GetItem(Index: Integer): Byte;
|
---|
314 | begin
|
---|
315 | Position := Index;
|
---|
316 | Result := ReadByte;
|
---|
317 | end;
|
---|
318 |
|
---|
319 | procedure TStreamHelper.SetItem(Index: Integer; const AValue: Byte);
|
---|
320 | begin
|
---|
321 | Position := Index;
|
---|
322 | WriteByte(AValue);
|
---|
323 | end;
|
---|
324 |
|
---|
325 | procedure TStreamHelper.SetStream(AValue: TStream);
|
---|
326 | begin
|
---|
327 | if FStream = AValue then Exit;
|
---|
328 | if FOwnStream and Assigned(FStream) then FStream.Free;
|
---|
329 | FStream := AValue;
|
---|
330 | FOwnStream := False;
|
---|
331 | end;
|
---|
332 |
|
---|
333 | procedure TStreamHelper.Assign(Source: TStreamHelper);
|
---|
334 | var
|
---|
335 | OldPosition: Integer;
|
---|
336 | begin
|
---|
337 | OldPosition := Source.Position;
|
---|
338 | Clear;
|
---|
339 | WriteStream(Source, Source.Size);
|
---|
340 | Source.Position := OldPosition;
|
---|
341 | Position := OldPosition;
|
---|
342 | end;
|
---|
343 |
|
---|
344 | procedure TStreamHelper.WriteAnsiString(Data: string);
|
---|
345 | var
|
---|
346 | StringLength: Longint;
|
---|
347 | begin
|
---|
348 | StringLength := Length(Data);
|
---|
349 | Write(StringLength, SizeOf(StringLength));
|
---|
350 | Write(Data[1], StringLength);
|
---|
351 | end;
|
---|
352 |
|
---|
353 | procedure TStreamHelper.WriteByte(Data: Byte);
|
---|
354 | begin
|
---|
355 | FStream.Write(Data, SizeOf(Byte));
|
---|
356 | end;
|
---|
357 |
|
---|
358 | procedure TStreamHelper.WriteCardinal(Data: Cardinal);
|
---|
359 | begin
|
---|
360 | if SwapData then Data := SwapEndian(Data);
|
---|
361 | Write(Data, SizeOf(Cardinal));
|
---|
362 | end;
|
---|
363 |
|
---|
364 | procedure TStreamHelper.WriteInt64(Data: Int64);
|
---|
365 | begin
|
---|
366 | if SwapData then Data := SwapEndian(Data);
|
---|
367 | Write(Data, SizeOf(Int64));
|
---|
368 | end;
|
---|
369 |
|
---|
370 | procedure TStreamHelper.WriteString(Data:string);
|
---|
371 | begin
|
---|
372 | if Length(Data) > 0 then
|
---|
373 | Write(Data[1], Length(Data));
|
---|
374 | end;
|
---|
375 |
|
---|
376 | procedure TStreamHelper.WriteShortString(Data: ShortString);
|
---|
377 | begin
|
---|
378 | WriteByte(Length(Data));
|
---|
379 | Write(Data[1], Length(Data));
|
---|
380 | end;
|
---|
381 |
|
---|
382 | procedure TStreamHelper.WriteStream(AStream: TStream; Count: Integer);
|
---|
383 | //var
|
---|
384 | // Buffer: array of Byte;
|
---|
385 | begin
|
---|
386 | AStream.Position := 0;
|
---|
387 | CopyFrom(AStream, Count);
|
---|
388 | (*if Count > AStream.Size then Count := AStream.Size; // Limit max. stream size
|
---|
389 | AStream.Position := 0;
|
---|
390 | if Count > 0 then begin
|
---|
391 | SetLength(Buffer, Count);
|
---|
392 | AStream.Read(Buffer[0], Count);
|
---|
393 | FStream.Write(Buffer[0], Count);
|
---|
394 | end;*)
|
---|
395 | end;
|
---|
396 |
|
---|
397 | procedure TStreamHelper.WriteDouble(Value: Double);
|
---|
398 | begin
|
---|
399 | Write(Value, SizeOf(Double));
|
---|
400 | end;
|
---|
401 |
|
---|
402 | procedure TStreamHelper.WriteSingle(Value: Single);
|
---|
403 | begin
|
---|
404 | Write(Value, SizeOf(Single));
|
---|
405 | end;
|
---|
406 |
|
---|
407 | procedure TStreamHelper.WriteUnixTime(Data: TDateTime);
|
---|
408 | var
|
---|
409 | DataUnix: Int64;
|
---|
410 | begin
|
---|
411 | DataUnix := DateTimeToUnix(Data);
|
---|
412 | WriteCardinal(DataUnix);
|
---|
413 | end;
|
---|
414 |
|
---|
415 | procedure TStreamHelper.WriteWord(Data: Word);
|
---|
416 | begin
|
---|
417 | if SwapData then Data := SwapEndian(Data);
|
---|
418 | Write(Data, SizeOf(Word));
|
---|
419 | end;
|
---|
420 |
|
---|
421 |
|
---|
422 | end.
|
---|