1 | unit UMemoryStreamEx;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, DateUtils;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TMemoryStreamEx }
|
---|
13 |
|
---|
14 | TMemoryStreamEx = class(TMemoryStream)
|
---|
15 | procedure WriteByte(Data: Byte);
|
---|
16 | procedure WriteWord(Data: Word);
|
---|
17 | procedure WriteCardinal(Data: Cardinal);
|
---|
18 | procedure WriteInt64(Data: Int64);
|
---|
19 | procedure WriteAnsiString(Data: string);
|
---|
20 | procedure WriteShortString(Data: ShortString);
|
---|
21 | procedure WriteUnixTime(Data: TDateTime);
|
---|
22 | procedure WriteStream(Stream: TStream);
|
---|
23 | procedure WriteString(Text: string);
|
---|
24 | function ReadByte: Byte;
|
---|
25 | function ReadWord: Word;
|
---|
26 | function ReadCardinal: Cardinal;
|
---|
27 | function ReadInt64: Int64;
|
---|
28 | function ReadAnsiString: string;
|
---|
29 | function ReadShortString: string;
|
---|
30 | function ReadUnixTime: TDateTime;
|
---|
31 | procedure ReadStream(var Stream: TStream; Count: Integer);
|
---|
32 | end;
|
---|
33 |
|
---|
34 | implementation
|
---|
35 |
|
---|
36 | { TMemoryStreamEx }
|
---|
37 |
|
---|
38 | function TMemoryStreamEx.ReadByte: Byte;
|
---|
39 | begin
|
---|
40 | Read(Result, 1);
|
---|
41 | end;
|
---|
42 |
|
---|
43 | function TMemoryStreamEx.ReadCardinal: Cardinal;
|
---|
44 | begin
|
---|
45 | Read(Result, 4);
|
---|
46 | end;
|
---|
47 |
|
---|
48 | function TMemoryStreamEx.ReadInt64: Int64;
|
---|
49 | begin
|
---|
50 | Read(Result, 8);
|
---|
51 | end;
|
---|
52 |
|
---|
53 | function TMemoryStreamEx.ReadAnsiString: string;
|
---|
54 | var
|
---|
55 | StringLength: Longint;
|
---|
56 | begin
|
---|
57 | Read(StringLength, SizeOf(StringLength));
|
---|
58 | SetLength(Result, StringLength);
|
---|
59 | if StringLength > 0 then begin
|
---|
60 | Read(Result[1], StringLength);
|
---|
61 | end;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | function TMemoryStreamEx.ReadShortString: string;
|
---|
65 | var
|
---|
66 | Count: Byte;
|
---|
67 | begin
|
---|
68 | Read(Count, 1);
|
---|
69 | SetLength(Result, Count);
|
---|
70 | Read(Result[1], Count);
|
---|
71 | end;
|
---|
72 |
|
---|
73 | procedure TMemoryStreamEx.ReadStream(var Stream: TStream; Count: Integer);
|
---|
74 | var
|
---|
75 | Buffer: array of Byte;
|
---|
76 | begin
|
---|
77 | if Count > 0 then begin
|
---|
78 | SetLength(Buffer, Count);
|
---|
79 | Read(Buffer[0], Count);
|
---|
80 | Stream.Size := Count;
|
---|
81 | Stream.Position := 0;
|
---|
82 | Stream.Write(Buffer[0], Count);
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | function TMemoryStreamEx.ReadUnixTime: TDateTime;
|
---|
87 | begin
|
---|
88 | Result := UnixToDateTime(ReadCardinal);
|
---|
89 | end;
|
---|
90 |
|
---|
91 | function TMemoryStreamEx.ReadWord: Word;
|
---|
92 | begin
|
---|
93 | Read(Result, 2);
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TMemoryStreamEx.WriteByte(Data: Byte);
|
---|
97 | begin
|
---|
98 | Write(Data, 1);
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TMemoryStreamEx.WriteCardinal(Data: Cardinal);
|
---|
102 | begin
|
---|
103 | Write(Data, 4);
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TMemoryStreamEx.WriteInt64(Data: Int64);
|
---|
107 | begin
|
---|
108 | Write(Data, 8);
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TMemoryStreamEx.WriteAnsiString(Data: string);
|
---|
112 | var
|
---|
113 | StringLength: Longint;
|
---|
114 | begin
|
---|
115 | StringLength := Length(Data);
|
---|
116 | Write(StringLength, SizeOf(StringLength));
|
---|
117 | Write(Data[1], StringLength);
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TMemoryStreamEx.WriteShortString(Data: ShortString);
|
---|
121 | begin
|
---|
122 | WriteByte(Length(Data));
|
---|
123 | Write(Data[1], Length(Data))
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure TMemoryStreamEx.WriteStream(Stream: TStream);
|
---|
127 | var
|
---|
128 | Buffer: array of Byte;
|
---|
129 | begin
|
---|
130 | Stream.Position := 0;
|
---|
131 | SetLength(Buffer, Stream.Size);
|
---|
132 | Stream.Read(Buffer[0], Stream.Size);
|
---|
133 | Write(Buffer[0], Stream.Size);
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TMemoryStreamEx.WriteString(Text: string);
|
---|
137 | begin
|
---|
138 | Write(Text[1], Length(Text));
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TMemoryStreamEx.WriteUnixTime(Data: TDateTime);
|
---|
142 | var
|
---|
143 | DataUnix: Int64;
|
---|
144 | begin
|
---|
145 | DataUnix := DateTimeToUnix(Data);
|
---|
146 | Write(DataUnix, 4);
|
---|
147 | end;
|
---|
148 |
|
---|
149 | procedure TMemoryStreamEx.WriteWord(Data: Word);
|
---|
150 | begin
|
---|
151 | Write(Data, 2);
|
---|
152 | end;
|
---|
153 |
|
---|
154 | end.
|
---|