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