forked from lordcrc/BufferedStreamReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferedStreamDev.dpr
181 lines (157 loc) · 3.81 KB
/
BufferedStreamDev.dpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
program BufferedStreamDev;
{$APPTYPE CONSOLE}
{$R *.res}
uses
WinAPI.Windows,
System.SysUtils,
System.Classes,
BufStream in 'BufStream.pas',
BufStreamReader in 'BufStreamReader.pas',
EncodingHelper in 'EncodingHelper.pas';
function GetData: TBytes;
begin
result := TEncoding.UTF8.GetBytes(
'This is a test' + #13#10 +
'Это тест' + #13#10 +
'0123456789');
end;
procedure RunTest1;
var
ss: TBytesStream;
bs: BufferedStream;
data: array[0..9] of UInt8;
begin
ss := nil;
bs := nil;
try
ss := TBytesStream.Create(GetData());
bs := BufferedStream.Create(ss, [], 3);
bs.FillBuffer;
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', bs.Position);
WriteLn;
bs.ConsumeBuffer(1);
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', bs.Position);
WriteLn;
bs.Read(data, SizeOf(data));
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', bs.Position);
WriteLn;
WriteLn;
finally
bs.Free;
ss.Free;
end;
end;
procedure RunTest2;
var
ss: TBytesStream;
sr: BufferedStreamReader;
s: string;
begin
ss := nil;
sr := nil;
try
ss := TBytesStream.Create(GetData());
sr := BufferedStreamReader.Create(ss, TEncoding.UTF8);
s := sr.ReadLine;
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
s := sr.ReadLine;
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
s := sr.ReadLine;
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
WriteLn;
finally
sr.Free;
ss.Free;
end;
end;
procedure RunTest3;
var
ss: TBytesStream;
sr: BufferedStreamReader;
s: string;
begin
sr := nil;
try
ss := TBytesStream.Create(GetData());
sr := BufferedStreamReader.Create(ss, TEncoding.UTF8, [BufferedStreamReaderOwnsSource]);
s := sr.ReadUntil(' т');
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
s := sr.ReadUntil(13);
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
s := sr.ReadLine;
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
s := sr.ReadToEnd;
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
WriteLn;
finally
sr.Free;
end;
end;
procedure RunTest4;
var
ss: TBytesStream;
sr: BufferedStreamReader;
c: TCharArray;
s: string;
begin
sr := nil;
try
ss := TBytesStream.Create(GetData());
sr := BufferedStreamReader.Create(ss, TEncoding.UTF8, [BufferedStreamReaderOwnsSource]);
sr.ReadLine;
c := sr.ReadChars(1);
SetString(s, PChar(@c[0]), Length(c));
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
c := sr.ReadChars(2);
SetString(s, PChar(@c[0]), Length(c));
WriteLn('Line data: "' + s + '"');
WriteLn('Source position: ', ss.Position);
WriteLn('Buffer position: ', sr.Stream.Position);
WriteLn;
WriteLn;
finally
sr.Free;
end;
end;
begin
try
SetConsoleOutputCP(CP_UTF8);
// RunTest1;
//
// RunTest2;
//
// RunTest3;
RunTest4;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.