1
2
3
4
5
6 using System;
7
8 namespace System.IO
9 {
10 public class StreamReader
11 {
12 public StreamReader(const SharedPtr<ByteStream>& stream_) : stream(stream_), buffered(false), eos(false)
13 {
14 }
15 public int Read()
16 {
17 int x = Get(false);
18 eos = x == -1;
19 return x;
20 }
21 public int Peek()
22 {
23 int x = Get(true);
24 eos = x == -1;
25 return x;
26 }
27 public string ReadLine()
28 {
29 string result;
30 int x = Read();
31 bool prevWasCR = false;
32 while (x != -1)
33 {
34 if (cast<char>(x) == '\r')
35 {
36 if (prevWasCR)
37 {
38 result.Append('\r');
39 }
40 prevWasCR = true;
41 }
42 else if (cast<char>(x) == '\n')
43 {
44 return result;
45 }
46 else
47 {
48 if (prevWasCR)
49 {
50 result.Append('\r');
51 prevWasCR = false;
52 }
53 result.Append(cast<char>(x));
54 }
55 x = Read();
56 }
57 eos = true;
58 if (prevWasCR)
59 {
60 result.Append('\r');
61 }
62 return result;
63 }
64 public string ReadToEnd()
65 {
66 string result;
67 int x = Read();
68 while (x != -1)
69 {
70 result.Append(cast<char>(x));
71 x = Read();
72 }
73 eos = true;
74 return result;
75 }
76 public void PutBack(byte b)
77 {
78 buffered = true;
79 buffer = b;
80 }
81 private int Get(bool peek)
82 {
83 if (buffered)
84 {
85 if (!peek)
86 {
87 buffered = false;
88 }
89 return buffer;
90 }
91 else
92 {
93 int x = stream->ReadByte();
94 if (peek)
95 {
96 buffer = x;
97 buffered = true;
98 }
99 return x;
100 }
101 }
102 public nothrow const SharedPtr<ByteStream>& ContainedStream()
103 {
104 return stream;
105 }
106 public inline nothrow bool EndOfStream() const
107 {
108 return eos;
109 }
110 private SharedPtr<ByteStream> stream;
111 private bool buffered;
112 private int buffer;
113 private bool eos;
114 }
115 }