1
2
3
4
5
6
7 package org.mule.util;
8
9 import java.io.IOException;
10 import java.io.Reader;
11
12
13
14
15
16 public class ChainedReader extends Reader
17 {
18 private final Reader first;
19 private final Reader second;
20 private boolean firstRead = false;
21
22 public ChainedReader(Reader first, Reader second)
23 {
24 this.first = first;
25 this.second = second;
26 }
27
28 public void close() throws IOException
29 {
30 first.close();
31 second.close();
32 }
33
34 public int read(char[] cbuf, int off, int len) throws IOException
35 {
36 if (!firstRead)
37 {
38 int i = first.read(cbuf, off, len);
39 if (i < len)
40 {
41 firstRead = true;
42 int x = second.read(cbuf, i, len - i);
43 return x + i;
44 }
45 else
46 {
47 return i;
48 }
49 }
50 else
51 {
52 return second.read(cbuf, off, len);
53 }
54 }
55
56 public int read() throws IOException
57 {
58 if (!firstRead)
59 {
60 int i = first.read();
61 if (i == -1)
62 {
63 firstRead = true;
64 return second.read();
65 }
66 else
67 {
68 return i;
69 }
70 }
71 else
72 {
73 return second.read();
74 }
75 }
76
77 public int read(char[] cbuf) throws IOException
78 {
79 if (!firstRead)
80 {
81 int i = first.read(cbuf);
82 if (i < cbuf.length)
83 {
84 firstRead = true;
85 int x = second.read(cbuf, i, cbuf.length - i);
86 return x + i;
87 }
88 else
89 {
90 return i;
91 }
92 }
93 else
94 {
95 return second.read(cbuf);
96 }
97 }
98
99 }