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