1 | /*
|
---|
2 | * Copyright 2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.util;
|
---|
18 |
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.InputStream;
|
---|
21 | import java.io.Reader;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Adapts a <code>Reader</code> as an <code>InputStream</code>.
|
---|
25 | * Adapted from <CODE>StringInputStream</CODE>.
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class ReaderInputStream extends InputStream {
|
---|
29 |
|
---|
30 | /** Source Reader */
|
---|
31 | private Reader in;
|
---|
32 |
|
---|
33 | private String encoding = System.getProperty("file.encoding");
|
---|
34 |
|
---|
35 | private byte[] slack;
|
---|
36 |
|
---|
37 | private int begin;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Construct a <CODE>ReaderInputStream</CODE>
|
---|
41 | * for the specified <CODE>Reader</CODE>.
|
---|
42 | *
|
---|
43 | * @param reader <CODE>Reader</CODE>. Must not be <code>null</code>.
|
---|
44 | */
|
---|
45 | public ReaderInputStream(Reader reader) {
|
---|
46 | in = reader;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Construct a <CODE>ReaderInputStream</CODE>
|
---|
51 | * for the specified <CODE>Reader</CODE>,
|
---|
52 | * with the specified encoding.
|
---|
53 | *
|
---|
54 | * @param reader non-null <CODE>Reader</CODE>.
|
---|
55 | * @param encoding non-null <CODE>String</CODE> encoding.
|
---|
56 | */
|
---|
57 | public ReaderInputStream(Reader reader, String encoding) {
|
---|
58 | this(reader);
|
---|
59 | if (encoding == null) {
|
---|
60 | throw new IllegalArgumentException("encoding must not be null");
|
---|
61 | } else {
|
---|
62 | this.encoding = encoding;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Reads from the <CODE>Reader</CODE>, returning the same value.
|
---|
68 | *
|
---|
69 | * @return the value of the next character in the <CODE>Reader</CODE>.
|
---|
70 | *
|
---|
71 | * @exception IOException if the original <code>Reader</code> fails to be read
|
---|
72 | */
|
---|
73 | public synchronized int read() throws IOException {
|
---|
74 | if (in == null) {
|
---|
75 | throw new IOException("Stream Closed");
|
---|
76 | }
|
---|
77 |
|
---|
78 | byte result;
|
---|
79 | if (slack != null && begin < slack.length) {
|
---|
80 | result = slack[begin];
|
---|
81 | if (++begin == slack.length) {
|
---|
82 | slack = null;
|
---|
83 | }
|
---|
84 | } else {
|
---|
85 | byte[] buf = new byte[1];
|
---|
86 | if (read(buf, 0, 1) <= 0) {
|
---|
87 | result = -1;
|
---|
88 | }
|
---|
89 | result = buf[0];
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (result < -1) {
|
---|
93 | result+= 256;
|
---|
94 | }
|
---|
95 |
|
---|
96 | return result;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Reads from the <code>Reader</code> into a byte array
|
---|
101 | *
|
---|
102 | * @param b the byte array to read into
|
---|
103 | * @param off the offset in the byte array
|
---|
104 | * @param len the length in the byte array to fill
|
---|
105 | * @return the actual number read into the byte array, -1 at
|
---|
106 | * the end of the stream
|
---|
107 | * @exception IOException if an error occurs
|
---|
108 | */
|
---|
109 | public synchronized int read(byte[] b, int off, int len)
|
---|
110 | throws IOException {
|
---|
111 | if (in == null) {
|
---|
112 | throw new IOException("Stream Closed");
|
---|
113 | }
|
---|
114 |
|
---|
115 | while (slack == null) {
|
---|
116 | char[] buf = new char[len]; // might read too much
|
---|
117 | int n = in.read(buf);
|
---|
118 | if (n == -1) {
|
---|
119 | return -1;
|
---|
120 | }
|
---|
121 | if (n > 0) {
|
---|
122 | slack = new String(buf, 0, n).getBytes(encoding);
|
---|
123 | begin = 0;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (len > slack.length - begin) {
|
---|
128 | len = slack.length - begin;
|
---|
129 | }
|
---|
130 |
|
---|
131 | System.arraycopy(slack, begin, b, off, len);
|
---|
132 |
|
---|
133 | if ((begin += len) >= slack.length) {
|
---|
134 | slack = null;
|
---|
135 | }
|
---|
136 |
|
---|
137 | return len;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Marks the read limit of the StringReader.
|
---|
142 | *
|
---|
143 | * @param limit the maximum limit of bytes that can be read before the
|
---|
144 | * mark position becomes invalid
|
---|
145 | */
|
---|
146 | public synchronized void mark(final int limit) {
|
---|
147 | try {
|
---|
148 | in.mark(limit);
|
---|
149 | } catch (IOException ioe) {
|
---|
150 | throw new RuntimeException(ioe.getMessage());
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @return the current number of bytes ready for reading
|
---|
157 | * @exception IOException if an error occurs
|
---|
158 | */
|
---|
159 | public synchronized int available() throws IOException {
|
---|
160 | if (in == null) {
|
---|
161 | throw new IOException("Stream Closed");
|
---|
162 | }
|
---|
163 | if (slack != null) {
|
---|
164 | return slack.length - begin;
|
---|
165 | }
|
---|
166 | if (in.ready()) {
|
---|
167 | return 1;
|
---|
168 | } else {
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * @return false - mark is not supported
|
---|
175 | */
|
---|
176 | public boolean markSupported () {
|
---|
177 | return false; // would be imprecise
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Resets the StringReader.
|
---|
182 | *
|
---|
183 | * @exception IOException if the StringReader fails to be reset
|
---|
184 | */
|
---|
185 | public synchronized void reset() throws IOException {
|
---|
186 | if (in == null) {
|
---|
187 | throw new IOException("Stream Closed");
|
---|
188 | }
|
---|
189 | slack = null;
|
---|
190 | in.reset();
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Closes the Stringreader.
|
---|
195 | *
|
---|
196 | * @exception IOException if the original StringReader fails to be closed
|
---|
197 | */
|
---|
198 | public synchronized void close() throws IOException {
|
---|
199 | in.close();
|
---|
200 | slack = null;
|
---|
201 | in = null;
|
---|
202 | }
|
---|
203 | }
|
---|