source: branches/DirectWeb/UMemoryStreamEx.pas

Last change on this file was 82, checked in by george, 15 years ago
  • Přidáno: Výchozí zdrojové kódy pro http server ve free pascalu.
File size: 3.2 KB
Line 
1unit UMemoryStreamEx;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, DateUtils;
9
10type
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
34implementation
35
36{ TMemoryStreamEx }
37
38function TMemoryStreamEx.ReadByte: Byte;
39begin
40 Read(Result, 1);
41end;
42
43function TMemoryStreamEx.ReadCardinal: Cardinal;
44begin
45 Read(Result, 4);
46end;
47
48function TMemoryStreamEx.ReadInt64: Int64;
49begin
50 Read(Result, 8);
51end;
52
53function TMemoryStreamEx.ReadAnsiString: string;
54var
55 StringLength: Longint;
56begin
57 Read(StringLength, SizeOf(StringLength));
58 SetLength(Result, StringLength);
59 if StringLength > 0 then begin
60 Read(Result[1], StringLength);
61 end;
62end;
63
64function TMemoryStreamEx.ReadShortString: string;
65var
66 Count: Byte;
67begin
68 Read(Count, 1);
69 SetLength(Result, Count);
70 Read(Result[1], Count);
71end;
72
73procedure TMemoryStreamEx.ReadStream(var Stream: TStream; Count: Integer);
74var
75 Buffer: array of Byte;
76begin
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;
84end;
85
86function TMemoryStreamEx.ReadUnixTime: TDateTime;
87begin
88 Result := UnixToDateTime(ReadCardinal);
89end;
90
91function TMemoryStreamEx.ReadWord: Word;
92begin
93 Read(Result, 2);
94end;
95
96procedure TMemoryStreamEx.WriteByte(Data: Byte);
97begin
98 Write(Data, 1);
99end;
100
101procedure TMemoryStreamEx.WriteCardinal(Data: Cardinal);
102begin
103 Write(Data, 4);
104end;
105
106procedure TMemoryStreamEx.WriteInt64(Data: Int64);
107begin
108 Write(Data, 8);
109end;
110
111procedure TMemoryStreamEx.WriteAnsiString(Data: string);
112var
113 StringLength: Longint;
114begin
115 StringLength := Length(Data);
116 Write(StringLength, SizeOf(StringLength));
117 Write(Data[1], StringLength);
118end;
119
120procedure TMemoryStreamEx.WriteShortString(Data: ShortString);
121begin
122 WriteByte(Length(Data));
123 Write(Data[1], Length(Data))
124end;
125
126procedure TMemoryStreamEx.WriteStream(Stream: TStream);
127var
128 Buffer: array of Byte;
129begin
130 Stream.Position := 0;
131 SetLength(Buffer, Stream.Size);
132 Stream.Read(Buffer[0], Stream.Size);
133 Write(Buffer[0], Stream.Size);
134end;
135
136procedure TMemoryStreamEx.WriteString(Text: string);
137begin
138 Write(Text[1], Length(Text));
139end;
140
141procedure TMemoryStreamEx.WriteUnixTime(Data: TDateTime);
142var
143 DataUnix: Int64;
144begin
145 DataUnix := DateTimeToUnix(Data);
146 Write(DataUnix, 4);
147end;
148
149procedure TMemoryStreamEx.WriteWord(Data: Word);
150begin
151 Write(Data, 2);
152end;
153
154end.
Note: See TracBrowser for help on using the repository browser.