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