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 | 0 | private boolean firstRead = false; |
21 | |
|
22 | |
public ChainedReader(Reader first, Reader second) |
23 | 0 | { |
24 | 0 | this.first = first; |
25 | 0 | this.second = second; |
26 | 0 | } |
27 | |
|
28 | |
public void close() throws IOException |
29 | |
{ |
30 | 0 | first.close(); |
31 | 0 | second.close(); |
32 | 0 | } |
33 | |
|
34 | |
public int read(char[] cbuf, int off, int len) throws IOException |
35 | |
{ |
36 | 0 | if (!firstRead) |
37 | |
{ |
38 | 0 | int i = first.read(cbuf, off, len); |
39 | 0 | if (i < len) |
40 | |
{ |
41 | 0 | firstRead = true; |
42 | 0 | int x = second.read(cbuf, i, len - i); |
43 | 0 | return x + i; |
44 | |
} |
45 | |
else |
46 | |
{ |
47 | 0 | return i; |
48 | |
} |
49 | |
} |
50 | |
else |
51 | |
{ |
52 | 0 | return second.read(cbuf, off, len); |
53 | |
} |
54 | |
} |
55 | |
|
56 | |
public int read() throws IOException |
57 | |
{ |
58 | 0 | if (!firstRead) |
59 | |
{ |
60 | 0 | int i = first.read(); |
61 | 0 | if (i == -1) |
62 | |
{ |
63 | 0 | firstRead = true; |
64 | 0 | return second.read(); |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 0 | return i; |
69 | |
} |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | return second.read(); |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
public int read(char[] cbuf) throws IOException |
78 | |
{ |
79 | 0 | if (!firstRead) |
80 | |
{ |
81 | 0 | int i = first.read(cbuf); |
82 | 0 | if (i < cbuf.length) |
83 | |
{ |
84 | 0 | firstRead = true; |
85 | 0 | int x = second.read(cbuf, i, cbuf.length - i); |
86 | 0 | return x + i; |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | return i; |
91 | |
} |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | return second.read(cbuf); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
} |