-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnbufferedStreamReader.cs
More file actions
175 lines (102 loc) · 3.92 KB
/
UnbufferedStreamReader.cs
File metadata and controls
175 lines (102 loc) · 3.92 KB
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
using System;
using System.IO;
using System.Text;
namespace Gsemac.IO {
public class UnbufferedStreamReader :
TextReader {
// Public members
public static new UnbufferedStreamReader Null => new UnbufferedStreamReader();
public Stream BaseStream => stream;
public Encoding CurrentEncoding => encoding;
public bool EndOfStream => endOfStream;
public UnbufferedStreamReader(Stream stream) :
this(stream, DefaultEncoding) {
}
public UnbufferedStreamReader(Stream stream, Encoding encoding) {
if (stream is null)
throw new ArgumentNullException(nameof(stream));
if (encoding is null)
throw new ArgumentNullException(nameof(encoding));
this.stream = stream;
this.encoding = encoding;
}
public override void Close() {
base.Close();
stream.Close();
}
public override int Peek() {
return PeekNextCharacter();
}
public override int Read() {
return ReadNextCharacter();
}
public override int Read(char[] buffer, int index, int count) {
int bytesRead = 0;
for (int i = index; i < index + count; ++i) {
int nextChar = Read();
if (nextChar < 0)
break;
buffer[index] = (char)nextChar;
++bytesRead;
}
return bytesRead;
}
public override string ReadLine() {
// Read until we hit "\n", "\r", or "\r\n".
// The newline sequence we encounter is consumed, but not returned.
if (EndOfStream)
return string.Empty;
StringBuilder sb = new StringBuilder();
while (!EndOfStream) {
int nextChar = ReadNextCharacter();
if (nextChar == '\n')
break;
if (nextChar == '\r') {
// Consume the following newline, if present.
nextChar = PeekNextCharacter();
if (nextChar == '\n')
ReadNextCharacter();
break;
}
if (nextChar >= 0)
sb.Append((char)nextChar);
}
return sb.ToString();
}
public override string ReadToEnd() {
// Since we're reading the entire stream, using a buffer is the same as not.
using (StreamReader sr = new StreamReader(stream, encoding))
return sr.ReadToEnd();
}
// Private members
private readonly Stream stream;
private readonly Encoding encoding = DefaultEncoding;
private readonly char[] buffer = new char[1];
private bool endOfStream = false;
private bool isBufferEmpty = true;
private static readonly Encoding DefaultEncoding = Encoding.UTF8;
private UnbufferedStreamReader() { }
private int PeekNextCharacter() {
// The stream will be read one character at a time with the current encoding.
if (!isBufferEmpty)
return buffer[0];
Decoder decoder = encoding.GetDecoder();
int nextByte;
while ((nextByte = stream.ReadByte()) != -1) {
int decodedCharCount = decoder.GetChars(new[] { (byte)nextByte }, 0, 1, buffer, 0);
if (decodedCharCount > 0) {
isBufferEmpty = false;
return buffer[0];
}
}
// If we reach this point, no chars were read.
endOfStream = true;
return -1;
}
private int ReadNextCharacter() {
int nextChar = PeekNextCharacter();
isBufferEmpty = true;
return nextChar;
}
}
}