1 /* InputStreamReader.java -- Reader than transforms bytes to chars
2 Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import gnu.gcj.convert.*;
42 import java.nio.charset.Charset;
43 import java.nio.charset.CharsetDecoder;
46 * This class reads characters from a byte input stream. The characters
47 * read are converted from bytes in the underlying stream by a
48 * decoding layer. The decoding layer transforms bytes to chars according
49 * to an encoding standard. There are many available encodings to choose
50 * from. The desired encoding can either be specified by name, or if no
51 * encoding is selected, the system default encoding will be used. The
52 * system default encoding name is determined from the system property
53 * <code>file.encoding</code>. The only encodings that are guaranteed to
54 * be availalbe are "8859_1" (the Latin-1 character set) and "UTF8".
55 * Unforunately, Java does not provide a mechanism for listing the
56 * ecodings that are supported in a given implementation.
58 * Here is a list of standard encoding names that may be available:
61 * <li>8859_1 (ISO-8859-1/Latin-1)</li>
62 * <li>8859_2 (ISO-8859-2/Latin-2)</li>
63 * <li>8859_3 (ISO-8859-3/Latin-3)</li>
64 * <li>8859_4 (ISO-8859-4/Latin-4)</li>
65 * <li>8859_5 (ISO-8859-5/Latin-5)</li>
66 * <li>8859_6 (ISO-8859-6/Latin-6)</li>
67 * <li>8859_7 (ISO-8859-7/Latin-7)</li>
68 * <li>8859_8 (ISO-8859-8/Latin-8)</li>
69 * <li>8859_9 (ISO-8859-9/Latin-9)</li>
70 * <li>ASCII (7-bit ASCII)</li>
71 * <li>UTF8 (UCS Transformation Format-8)</li>
75 * It is recommended that applications do not use
76 * <code>InputStreamReader</code>'s
77 * directly. Rather, for efficiency purposes, an object of this class
78 * should be wrapped by a <code>BufferedReader</code>.
80 * Due to a deficiency the Java class library design, there is no standard
81 * way for an application to install its own byte-character encoding.
86 * @author Aaron M. Renn (arenn@urbanophile.com)
87 * @author Per Bothner (bothner@cygnus.com)
88 * @date April 22, 1998.
90 public class InputStreamReader extends Reader
92 BufferedInputStream in;
94 // Buffer of chars read from in and converted but not consumed.
96 // Next available character (in work buffer) to read.
98 // Last available character (in work buffer) to read.
102 * This is the byte-character decoder class that does the reading and
103 * translation of bytes from the underlying stream.
105 BytesToUnicode converter;
108 * This method initializes a new instance of <code>InputStreamReader</code>
109 * to read from the specified stream using the default encoding.
111 * @param in The <code>InputStream</code> to read from
113 public InputStreamReader(InputStream in)
115 this(in, BytesToUnicode.getDefaultDecoder());
119 * This method initializes a new instance of <code>InputStreamReader</code>
120 * to read from the specified stream using a caller supplied character
121 * encoding scheme. Note that due to a deficiency in the Java language
122 * design, there is no way to determine which encodings are supported.
124 * @param in The <code>InputStream</code> to read from
125 * @param encoding_name The name of the encoding scheme to use
127 * @exception UnsupportedEncodingException If the encoding scheme
128 * requested is not available.
130 public InputStreamReader(InputStream in, String encoding_name)
131 throws UnsupportedEncodingException
133 this(in, BytesToUnicode.getDecoder(encoding_name));
137 * Creates an InputStreamReader that uses a decoder of the given
138 * charset to decode the bytes in the InputStream into
141 public InputStreamReader(InputStream in, Charset charset)
143 this(in, new BytesToCharsetAdaptor(charset));
147 * Creates an InputStreamReader that uses the given charset decoder
148 * to decode the bytes in the InputStream into characters.
150 public InputStreamReader(InputStream in, CharsetDecoder decoder)
152 this(in, new BytesToCharsetAdaptor(decoder));
155 private InputStreamReader(InputStream in, BytesToUnicode decoder)
157 // FIXME: someone could pass in a BufferedInputStream whose buffer
158 // is smaller than the longest encoded character for this
159 // encoding. We will probably go into an infinite loop in this
160 // case. We probably ought to just have our own byte buffering
162 this.in = in instanceof BufferedInputStream
163 ? (BufferedInputStream) in
164 : new BufferedInputStream(in);
165 /* Don't need to call super(in) here as long as the lock gets set. */
168 converter.setInput(this.in.buf, 0, 0);
172 * This method closes this stream, as well as the underlying
173 * <code>InputStream</code>.
175 * @exception IOException If an error occurs
177 public void close() throws IOException
190 * This method returns the name of the encoding that is currently in use
191 * by this object. If the stream has been closed, this method is allowed
192 * to return <code>null</code>.
194 * @return The current encoding name
196 public String getEncoding()
198 return in != null ? converter.getName() : null;
202 * This method checks to see if the stream is read to be read. It
203 * will return <code>true</code> if is, or <code>false</code> if it is not.
204 * If the stream is not ready to be read, it could (although is not required
205 * to) block on the next read attempt.
207 * @return <code>true</code> if the stream is ready to be read,
208 * <code>false</code> otherwise
210 * @exception IOException If an error occurs
212 public boolean ready() throws IOException
217 throw new IOException("Stream closed");
222 // According to the spec, an InputStreamReader is ready if its
223 // input buffer is not empty (above), or if bytes are
224 // available on the underlying byte stream.
225 return in.available () > 0;
230 * This method reads up to <code>length</code> characters from the stream into
231 * the specified array starting at index <code>offset</code> into the
234 * @param buf The character array to recieve the data read
235 * @param offset The offset into the array to start storing characters
236 * @param length The requested number of characters to read.
238 * @return The actual number of characters read, or -1 if end of stream.
240 * @exception IOException If an error occurs
242 public int read (char[] buf, int offset, int length) throws IOException
247 throw new IOException("Stream closed");
252 int wavail = wcount - wpos;
255 // Nothing waiting, so refill their buffer.
256 return refill(buf, offset, length);
261 System.arraycopy(work, wpos, buf, offset, length);
268 * This method reads a single character of data from the stream.
270 * @return The char read, as an int, or -1 if end of stream.
272 * @exception IOException If an error occurs
274 public int read() throws IOException
279 throw new IOException("Stream closed");
281 int wavail = wcount - wpos;
284 // Nothing waiting, so refill our internal buffer.
287 work = new char[100];
288 int count = refill(work, 0, work.length);
298 // Read more bytes and convert them into the specified buffer.
299 // Returns the number of converted characters or -1 on EOF.
300 private int refill(char[] buf, int offset, int length) throws IOException
304 // We have knowledge of the internals of BufferedInputStream
306 // BufferedInputStream.refill() can only be called when
308 boolean r = in.pos < in.count || in.refill ();
311 converter.setInput(in.buf, in.pos, in.count);
312 int count = converter.read(buf, offset, length);
314 // We might have bytes but not have made any progress. In
315 // this case we try to refill. If refilling fails, we assume
316 // we have a malformed character at the end of the stream.
317 if (count == 0 && converter.inpos == in.pos)
321 throw new CharConversionException ();
326 in.skip(converter.inpos - in.pos);