Merge the recent changes from HEAD onto the branch
[beagle.git] / Util / MultiReader.cs
blob3691ae001440a08ba3ef01855ebe767d74764d5b
1 //
2 // MultiReader.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Text;
32 namespace Beagle.Util {
34 public class MultiReader : TextReader {
36 Queue readers = new Queue ();
38 public MultiReader ()
39 { }
41 public void Add (TextReader reader)
43 readers.Enqueue (reader);
46 public int Count {
47 get { return readers.Count; }
50 private TextReader Current {
51 get {
52 if (readers.Count == 0)
53 return null;
54 return (TextReader) readers.Peek ();
58 private void Next ()
60 if (readers.Count > 0)
61 readers.Dequeue ();
64 public override void Close ()
66 while (Current != null) {
67 Current.Close ();
68 Next ();
72 public override int Peek ()
74 while (Current != null) {
75 int peek = Current.Peek ();
76 if (peek != -1)
77 return peek;
78 Next ();
81 return -1;
84 public override int Read ()
86 while (Current != null) {
87 int read = Current.Read ();
88 if (read != -1)
89 return read;
90 Next ();
93 return -1;
96 public override int Read (char[] buffer, int index, int count)
98 int total = 0;
99 while (Current != null && count > 0) {
100 int actual = Current.Read (buffer, index + total, count);
101 if (actual < count)
102 Next ();
103 count -= actual;
104 total += actual;
106 return total;
109 public override int ReadBlock (char[] buffer, int index, int count)
111 return Read (buffer, index, count);
114 public override string ReadLine ()
116 while (Current != null) {
117 string str = Current.ReadLine ();
118 if (str != null)
119 return str;
120 Next ();
123 return null;
126 public override string ReadToEnd ()
128 StringBuilder all = new StringBuilder ("");
130 while (Current != null) {
131 string str = Current.ReadToEnd ();
132 if (str != null)
133 all.Append (str);
134 Next ();
137 return all.ToString ();