KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > ReaderInputStream


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23
24 /**
25  * Adapts a <code>Reader</code> as an <code>InputStream</code>.
26  * Adapted from <code>StringInputStream</code>.
27  *
28  */

29 public class ReaderInputStream extends InputStream JavaDoc {
30
31     /** Source Reader */
32     private Reader JavaDoc in;
33
34     private String JavaDoc encoding = System.getProperty("file.encoding");
35
36     private byte[] slack;
37
38     private int begin;
39
40     /**
41      * Construct a <code>ReaderInputStream</code>
42      * for the specified <code>Reader</code>.
43      *
44      * @param reader <code>Reader</code>. Must not be <code>null</code>.
45      */

46     public ReaderInputStream(Reader JavaDoc reader) {
47         in = reader;
48     }
49
50     /**
51      * Construct a <code>ReaderInputStream</code>
52      * for the specified <code>Reader</code>,
53      * with the specified encoding.
54      *
55      * @param reader non-null <code>Reader</code>.
56      * @param encoding non-null <code>String</code> encoding.
57      */

58     public ReaderInputStream(Reader JavaDoc reader, String JavaDoc encoding) {
59         this(reader);
60         if (encoding == null) {
61             throw new IllegalArgumentException JavaDoc("encoding must not be null");
62         } else {
63             this.encoding = encoding;
64         }
65     }
66
67     /**
68      * Reads from the <code>Reader</code>, returning the same value.
69      *
70      * @return the value of the next character in the <code>Reader</code>.
71      *
72      * @exception IOException if the original <code>Reader</code> fails to be read
73      */

74     public synchronized int read() throws IOException JavaDoc {
75         if (in == null) {
76             throw new IOException JavaDoc("Stream Closed");
77         }
78
79         byte result;
80         if (slack != null && begin < slack.length) {
81             result = slack[begin];
82             if (++begin == slack.length) {
83                 slack = null;
84             }
85         } else {
86             byte[] buf = new byte[1];
87             if (read(buf, 0, 1) <= 0) {
88                 return -1;
89             } else {
90                 result = buf[0];
91             }
92         }
93
94         return result & 0xFF;
95     }
96
97     /**
98      * Reads from the <code>Reader</code> into a byte array
99      *
100      * @param b the byte array to read into
101      * @param off the offset in the byte array
102      * @param len the length in the byte array to fill
103      * @return the actual number read into the byte array, -1 at
104      * the end of the stream
105      * @exception IOException if an error occurs
106      */

107     public synchronized int read(byte[] b, int off, int len)
108         throws IOException JavaDoc {
109         if (in == null) {
110             throw new IOException JavaDoc("Stream Closed");
111         }
112         if (len == 0) {
113             return 0;
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 JavaDoc(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 JavaDoc ioe) {
150             throw new RuntimeException JavaDoc(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 JavaDoc {
160         if (in == null) {
161             throw new IOException JavaDoc("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 JavaDoc {
186         if (in == null) {
187             throw new IOException JavaDoc("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 JavaDoc {
199         if (in != null) {
200             in.close();
201             slack = null;
202             in = null;
203         }
204     }
205 }
206
Popular Tags