1 | unit UMemoryStreamEx;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, DateUtils, syncobjs;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TEndianness = (enBig, enLittle);
|
---|
12 |
|
---|
13 | { TMemoryStreamEx }
|
---|
14 |
|
---|
15 | TMemoryStreamEx = class(TMemoryStream)
|
---|
16 | private
|
---|
17 | FEndianness: TEndianness;
|
---|
18 | SwapData: Boolean;
|
---|
19 | procedure SetEndianness(const AValue: TEndianness);
|
---|
20 | public
|
---|
21 | procedure WriteByte(Data: Byte);
|
---|
22 | procedure WriteWord(Data: Word);
|
---|
23 | procedure WriteCardinal(Data: Cardinal);
|
---|
24 | procedure WriteInt64(Data: Int64);
|
---|
25 | procedure WriteString(Data: string);
|
---|
26 | procedure WriteShortString(Data: ShortString);
|
---|
27 | procedure WriteAnsiString(Data: string);
|
---|
28 | procedure WriteUnixTime(Data: TDateTime);
|
---|
29 | procedure WriteDouble(Value: Double);
|
---|
30 | procedure WriteSingle(Value: Single);
|
---|
31 | procedure WriteStream(Stream: TStream; Count: Integer);
|
---|
32 | procedure WriteStreamPart(Stream: TStream; Count: Integer);
|
---|
33 | function ReadByte: Byte;
|
---|
34 | function ReadWord: Word;
|
---|
35 | function ReadCardinal: Cardinal;
|
---|
36 | function ReadInt64: Int64;
|
---|
37 | function ReadString: string;
|
---|
38 | function ReadShortString: string;
|
---|
39 | function ReadAnsiString: string;
|
---|
40 | function ReadStringTerminated(Terminator: string = #0): string;
|
---|
41 | function ReadUnixTime: TDateTime;
|
---|
42 | function ReadDouble: Double;
|
---|
43 | function ReadSingle: Single;
|
---|
44 | procedure ReadStream(Stream: TStream; Count: Integer);
|
---|
45 | procedure ReadStreamPart(Stream: TStream; Count: Integer);
|
---|
46 | function Sum: Byte;
|
---|
47 | procedure FillByte(Data: Byte; Count: Integer);
|
---|
48 | procedure Assign(Source: TMemoryStreamEx);
|
---|
49 | constructor Create;
|
---|
50 | property Endianness: TEndianness read FEndianness write SetEndianness;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | { TThreadMemoryStreamEx }
|
---|
54 |
|
---|
55 | TThreadMemoryStreamEx = class(TMemoryStreamEx)
|
---|
56 | Lock: TCriticalSection;
|
---|
57 | constructor Create;
|
---|
58 | destructor Destroy; override;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | implementation
|
---|
62 |
|
---|
63 | { TMemoryStreamEx }
|
---|
64 |
|
---|
65 | function TMemoryStreamEx.ReadAnsiString: string;
|
---|
66 | var
|
---|
67 | StringLength: Longint;
|
---|
68 | begin
|
---|
69 | ReadBuffer(StringLength, SizeOf(StringLength));
|
---|
70 | SetLength(Result, StringLength);
|
---|
71 | if StringLength > 0 then begin
|
---|
72 | ReadBuffer(Result[1], StringLength);
|
---|
73 | end;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | function TMemoryStreamEx.ReadStringTerminated(Terminator: string = #0): string;
|
---|
77 | var
|
---|
78 | Data: Char;
|
---|
79 | I: Integer;
|
---|
80 | OldPosition: Integer;
|
---|
81 | begin
|
---|
82 | OldPosition := Position;
|
---|
83 | Result := '';
|
---|
84 | I := 1;
|
---|
85 | repeat
|
---|
86 | if Position >= Size then Break;
|
---|
87 | Data := Chr(ReadByte);
|
---|
88 | if Data <> Terminator[I] then begin
|
---|
89 | Result := Result + Data;
|
---|
90 | I := 1;
|
---|
91 | end else Inc(I);
|
---|
92 | until I > Length(Terminator);
|
---|
93 | if not (I > Length(Terminator)) then begin
|
---|
94 | Result := '';
|
---|
95 | Position := OldPosition;
|
---|
96 | end;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function TMemoryStreamEx.ReadByte: Byte;
|
---|
100 | begin
|
---|
101 | ReadBuffer(Result, SizeOf(Byte));
|
---|
102 | end;
|
---|
103 |
|
---|
104 | function TMemoryStreamEx.ReadCardinal: Cardinal;
|
---|
105 | begin
|
---|
106 | ReadBuffer(Result, SizeOf(Cardinal));
|
---|
107 | if SwapData then Result := Swap(Result);
|
---|
108 | end;
|
---|
109 |
|
---|
110 | function TMemoryStreamEx.ReadInt64: Int64;
|
---|
111 | begin
|
---|
112 | ReadBuffer(Result, SizeOf(Int64));
|
---|
113 | if SwapData then Result := Swap(Result);
|
---|
114 | end;
|
---|
115 |
|
---|
116 | function TMemoryStreamEx.ReadString:string;
|
---|
117 | begin
|
---|
118 | SetLength(Result, Size - Position);
|
---|
119 | if (Size - Position) > 0 then
|
---|
120 | Read(Result[1], Size - Position)
|
---|
121 | else Result := '';
|
---|
122 | end;
|
---|
123 |
|
---|
124 | function TMemoryStreamEx.ReadShortString: string;
|
---|
125 | var
|
---|
126 | Count: Byte;
|
---|
127 | begin
|
---|
128 | ReadBuffer(Count, 1);
|
---|
129 | SetLength(Result, Count);
|
---|
130 | ReadBuffer(Result[1], Count);
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TMemoryStreamEx.ReadStream(Stream: TStream; Count: Integer);
|
---|
134 | var
|
---|
135 | Buffer: array of Byte;
|
---|
136 | begin
|
---|
137 | if Count > 0 then begin
|
---|
138 | SetLength(Buffer, Count);
|
---|
139 | ReadBuffer(Buffer[0], Count);
|
---|
140 | Stream.Size := Count;
|
---|
141 | Stream.Position := 0;
|
---|
142 | Stream.Write(Buffer[0], Count);
|
---|
143 | end;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TMemoryStreamEx.ReadStreamPart(Stream:TStream;Count:Integer);
|
---|
147 | var
|
---|
148 | Buffer: array of Byte;
|
---|
149 | begin
|
---|
150 | if Count > 0 then begin
|
---|
151 | SetLength(Buffer, Count);
|
---|
152 | ReadBuffer(Buffer[0], Count);
|
---|
153 | if Stream.Size < (Stream.Position + Count) then
|
---|
154 | Stream.Size := Stream.Position + Count;
|
---|
155 | Stream.Write(Buffer[0], Count);
|
---|
156 | end;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TMemoryStreamEx.WriteStreamPart(Stream:TStream;Count:Integer);
|
---|
160 | var
|
---|
161 | Buffer: array of Byte;
|
---|
162 | begin
|
---|
163 | if Count > Stream.Size then Count := Stream.Size; // Limit max. stream size
|
---|
164 | if Count > 0 then begin
|
---|
165 | SetLength(Buffer, Count);
|
---|
166 | Stream.Read(Buffer[0], Count);
|
---|
167 | Write(Buffer[0], Count);
|
---|
168 | end;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | constructor TMemoryStreamEx.Create;
|
---|
172 | begin
|
---|
173 | Endianness := enLittle;
|
---|
174 | end;
|
---|
175 |
|
---|
176 | function TMemoryStreamEx.ReadUnixTime: TDateTime;
|
---|
177 | begin
|
---|
178 | Result := UnixToDateTime(ReadCardinal);
|
---|
179 | end;
|
---|
180 |
|
---|
181 | function TMemoryStreamEx.ReadDouble: Double;
|
---|
182 | begin
|
---|
183 | ReadBuffer(Result, SizeOf(Double));
|
---|
184 | end;
|
---|
185 |
|
---|
186 | function TMemoryStreamEx.ReadSingle: Single;
|
---|
187 | begin
|
---|
188 | ReadBuffer(Result, SizeOf(Single));
|
---|
189 | end;
|
---|
190 |
|
---|
191 | function TMemoryStreamEx.Sum: Byte;
|
---|
192 | begin
|
---|
193 | Result := 0;
|
---|
194 | Position := 0;
|
---|
195 | while Position < Size do
|
---|
196 | Result := (Result + ReadByte) and $ff;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TMemoryStreamEx.FillByte(Data:Byte;Count:Integer);
|
---|
200 | var
|
---|
201 | I: Integer;
|
---|
202 | begin
|
---|
203 | for I := 0 to Count - 1 do
|
---|
204 | WriteByte(Data);
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TMemoryStreamEx.Assign(Source: TMemoryStreamEx);
|
---|
208 | var
|
---|
209 | OldPosition: Int64;
|
---|
210 | begin
|
---|
211 | FEndianness := Source.FEndianness;
|
---|
212 | SwapData := Source.SwapData;
|
---|
213 | OldPosition := Source.Position;
|
---|
214 | Clear;
|
---|
215 | Source.Position := 0;
|
---|
216 | CopyFrom(Source, Source.Size);
|
---|
217 | Position := OldPosition;
|
---|
218 | Source.Position := OldPosition;
|
---|
219 | end;
|
---|
220 |
|
---|
221 | function TMemoryStreamEx.ReadWord: Word;
|
---|
222 | begin
|
---|
223 | ReadBuffer(Result, SizeOf(Word));
|
---|
224 | if SwapData then Result := Swap(Result);
|
---|
225 | end;
|
---|
226 |
|
---|
227 | procedure TMemoryStreamEx.SetEndianness(const AValue: TEndianness);
|
---|
228 | begin
|
---|
229 | FEndianness := AValue;
|
---|
230 | {$if defined(FPC_LITTLE_ENDIAN)}
|
---|
231 | SwapData := FEndianness = enBig;
|
---|
232 | {$elseif defined(FPC_BIG_ENDIAN)}
|
---|
233 | SwapData := FEndianness = enLittle;
|
---|
234 | {$endif}
|
---|
235 | end;
|
---|
236 |
|
---|
237 | procedure TMemoryStreamEx.WriteAnsiString(Data: string);
|
---|
238 | var
|
---|
239 | StringLength: Longint;
|
---|
240 | begin
|
---|
241 | StringLength := Length(Data);
|
---|
242 | Write(StringLength, SizeOf(StringLength));
|
---|
243 | Write(Data[1], StringLength);
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TMemoryStreamEx.WriteByte(Data: Byte);
|
---|
247 | begin
|
---|
248 | Write(Data, SizeOf(Byte));
|
---|
249 | end;
|
---|
250 |
|
---|
251 | procedure TMemoryStreamEx.WriteCardinal(Data: Cardinal);
|
---|
252 | begin
|
---|
253 | if SwapData then Data := Swap(Data);
|
---|
254 | Write(Data, SizeOf(Cardinal));
|
---|
255 | end;
|
---|
256 |
|
---|
257 | procedure TMemoryStreamEx.WriteInt64(Data: Int64);
|
---|
258 | begin
|
---|
259 | if SwapData then Data := Swap(Data);
|
---|
260 | Write(Data, SizeOf(Int64));
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TMemoryStreamEx.WriteString(Data:string);
|
---|
264 | begin
|
---|
265 | if Length(Data) > 0 then
|
---|
266 | Write(Data[1], Length(Data));
|
---|
267 | end;
|
---|
268 |
|
---|
269 | procedure TMemoryStreamEx.WriteShortString(Data: ShortString);
|
---|
270 | begin
|
---|
271 | WriteByte(Length(Data));
|
---|
272 | Write(Data[1], Length(Data));
|
---|
273 | end;
|
---|
274 |
|
---|
275 | procedure TMemoryStreamEx.WriteStream(Stream: TStream; Count: Integer);
|
---|
276 | var
|
---|
277 | Buffer: array of Byte;
|
---|
278 | begin
|
---|
279 | if Count > Stream.Size then Count := Stream.Size; // Limit max. stream size
|
---|
280 | Stream.Position := 0;
|
---|
281 | if Count > 0 then begin
|
---|
282 | SetLength(Buffer, Count);
|
---|
283 | Stream.Read(Buffer[0], Count);
|
---|
284 | Write(Buffer[0], Count);
|
---|
285 | end;
|
---|
286 | end;
|
---|
287 |
|
---|
288 | procedure TMemoryStreamEx.WriteDouble(Value: Double);
|
---|
289 | begin
|
---|
290 | Write(Value, SizeOf(Double));
|
---|
291 | end;
|
---|
292 |
|
---|
293 | procedure TMemoryStreamEx.WriteSingle(Value: Single);
|
---|
294 | begin
|
---|
295 | Write(Value, SizeOf(Single));
|
---|
296 | end;
|
---|
297 |
|
---|
298 | procedure TMemoryStreamEx.WriteUnixTime(Data: TDateTime);
|
---|
299 | var
|
---|
300 | DataUnix: Int64;
|
---|
301 | begin
|
---|
302 | DataUnix := DateTimeToUnix(Data);
|
---|
303 | WriteCardinal(DataUnix);
|
---|
304 | end;
|
---|
305 |
|
---|
306 | procedure TMemoryStreamEx.WriteWord(Data: Word);
|
---|
307 | begin
|
---|
308 | if SwapData then Data := Swap(Data);
|
---|
309 | Write(Data, SizeOf(Word));
|
---|
310 | end;
|
---|
311 |
|
---|
312 | { TThreadMemoryStreamEx }
|
---|
313 |
|
---|
314 | constructor TThreadMemoryStreamEx.Create;
|
---|
315 | begin
|
---|
316 | inherited Create;
|
---|
317 | Lock := TCriticalSection.Create;
|
---|
318 | end;
|
---|
319 |
|
---|
320 | destructor TThreadMemoryStreamEx.Destroy;
|
---|
321 | begin
|
---|
322 | Lock.Free;
|
---|
323 | inherited Destroy;
|
---|
324 | end;
|
---|
325 |
|
---|
326 | end.
|
---|
327 |
|
---|