KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > ReaderInputStream


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
18 //package org.apache.tools.ant.util;
19
package org.mmbase.util;
20
21 import java.io.*;
22
23 import org.mmbase.util.logging.*;
24
25
26 /**
27  * Oddly enough, Java does not provide this itself. Stolen from Ant code.
28  *
29  * Adapts a <code>Reader</code> as an <code>InputStream</code>.
30  * Adapted from <CODE>StringInputStream</CODE>.
31  *
32  * @since MMBase-1.8.2
33  * @version $Id: ReaderInputStream.java,v 1.1.2.1 2006/10/02 14:36:12 michiel Exp $
34  */

35
36
37 public class ReaderInputStream extends InputStream {
38     private static Logger log = Logging.getLoggerInstance(ReaderInputStream.class);
39     /** Source Reader */
40     private Reader in;
41
42     private String JavaDoc encoding = System.getProperty("file.encoding");
43
44     private byte[] slack;
45
46     private int begin;
47
48     /**
49      * Construct a <CODE>ReaderInputStream</CODE>
50      * for the specified <CODE>Reader</CODE>.
51      *
52      * @param reader <CODE>Reader</CODE>. Must not be <code>null</code>.
53      */

54     public ReaderInputStream(Reader reader) {
55         in = reader;
56     }
57
58     /**
59      * Construct a <CODE>ReaderInputStream</CODE>
60      * for the specified <CODE>Reader</CODE>,
61      * with the specified encoding.
62      *
63      * @param reader non-null <CODE>Reader</CODE>.
64      * @param encoding non-null <CODE>String</CODE> encoding.
65      */

66     public ReaderInputStream(Reader reader, String JavaDoc encoding) {
67         this(reader);
68         if (encoding == null) {
69             throw new IllegalArgumentException JavaDoc("encoding must not be null");
70         } else {
71             this.encoding = encoding;
72         }
73     }
74
75     /**
76      * Reads from the <CODE>Reader</CODE>, returning the same value.
77      *
78      * @return the value of the next character in the <CODE>Reader</CODE>.
79      *
80      * @exception IOException if the original <code>Reader</code> fails to be read
81      */

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

119     public synchronized int read(byte[] b, int off, int len)
120         throws IOException {
121         if (in == null) {
122             throw new IOException("Stream Closed");
123         }
124
125         while (slack == null) {
126             char[] buf = new char[len]; // might read too much
127
int n = in.read(buf);
128             if (n == -1) {
129                 return -1;
130             }
131             if (n > 0) {
132                 slack = new String JavaDoc(buf, 0, n).getBytes(encoding);
133                 begin = 0;
134             }
135         }
136
137         if (len > slack.length - begin) {
138             len = slack.length - begin;
139         }
140
141         System.arraycopy(slack, begin, b, off, len);
142
143         if ((begin += len) >= slack.length) {
144             slack = null;
145         }
146
147         return len;
148     }
149
150     /**
151      * Marks the read limit of the StringReader.
152      *
153      * @param limit the maximum limit of bytes that can be read before the
154      * mark position becomes invalid
155      */

156     public synchronized void mark(final int limit) {
157         try {
158             in.mark(limit);
159         } catch (IOException ioe) {
160             throw new RuntimeException JavaDoc(ioe.getMessage());
161         }
162     }
163
164
165     /**
166      * @return the current number of bytes ready for reading
167      * @exception IOException if an error occurs
168      */

169     public synchronized int available() throws IOException {
170         if (in == null) {
171             throw new IOException("Stream Closed");
172         }
173         if (slack != null) {
174             return slack.length - begin;
175         }
176         if (in.ready()) {
177             return 1;
178         } else {
179             return 0;
180         }
181     }
182
183     /**
184      * @return false - mark is not supported
185      */

186     public boolean markSupported () {
187         return false; // would be imprecise
188
}
189
190     /**
191      * Resets the StringReader.
192      *
193      * @exception IOException if the StringReader fails to be reset
194      */

195     public synchronized void reset() throws IOException {
196         if (in == null) {
197             throw new IOException("Stream Closed");
198         }
199         slack = null;
200         in.reset();
201     }
202
203     /**
204      * Closes the Stringreader.
205      *
206      * @exception IOException if the original StringReader fails to be closed
207      */

208     public synchronized void close() throws IOException {
209         in.close();
210         slack = null;
211         in = null;
212     }
213
214     public static void main(String JavaDoc[] args) throws IOException {
215         ReaderInputStream is = new ReaderInputStream(new StringReader(args[0]), "UTF-8");
216         while (true) {
217             int b = is.read();
218             if (b <= 0) break;
219             System.out.println("" + b);
220         }
221     }
222 }
223
Popular Tags