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., 59 Temple Place, Suite 330, 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
.*;
44 * This class reads characters from a byte input stream. The characters
45 * read are converted from bytes in the underlying stream by a
46 * decoding layer. The decoding layer transforms bytes to chars according
47 * to an encoding standard. There are many available encodings to choose
48 * from. The desired encoding can either be specified by name, or if no
49 * encoding is selected, the system default encoding will be used. The
50 * system default encoding name is determined from the system property
51 * <code>file.encoding</code>. The only encodings that are guaranteed to
52 * be availalbe are "8859_1" (the Latin-1 character set) and "UTF8".
53 * Unforunately, Java does not provide a mechanism for listing the
54 * ecodings that are supported in a given implementation.
56 * Here is a list of standard encoding names that may be available:
59 * <li>8859_1 (ISO-8859-1/Latin-1)</li>
60 * <li>8859_2 (ISO-8859-2/Latin-2)</li>
61 * <li>8859_3 (ISO-8859-3/Latin-3)</li>
62 * <li>8859_4 (ISO-8859-4/Latin-4)</li>
63 * <li>8859_5 (ISO-8859-5/Latin-5)</li>
64 * <li>8859_6 (ISO-8859-6/Latin-6)</li>
65 * <li>8859_7 (ISO-8859-7/Latin-7)</li>
66 * <li>8859_8 (ISO-8859-8/Latin-8)</li>
67 * <li>8859_9 (ISO-8859-9/Latin-9)</li>
68 * <li>ASCII (7-bit ASCII)</li>
69 * <li>UTF8 (UCS Transformation Format-8)</li>
73 * It is recommended that applications do not use
74 * <code>InputStreamReader</code>'s
75 * directly. Rather, for efficiency purposes, an object of this class
76 * should be wrapped by a <code>BufferedReader</code>.
78 * Due to a deficiency the Java class library design, there is no standard
79 * way for an application to install its own byte-character encoding.
84 * @author Aaron M. Renn (arenn@urbanophile.com)
85 * @author Per Bothner (bothner@cygnus.com)
86 * @date April 22, 1998.
88 public class InputStreamReader
extends Reader
90 BufferedInputStream in
;
92 // Buffer of chars read from in and converted but not consumed.
94 // Next available character (in work buffer) to read.
96 // Last available character (in work buffer) to read.
100 * This is the byte-character decoder class that does the reading and
101 * translation of bytes from the underlying stream.
103 BytesToUnicode converter
;
106 * This method initializes a new instance of <code>InputStreamReader</code>
107 * to read from the specified stream using the default encoding.
109 * @param in The <code>InputStream</code> to read from
111 public InputStreamReader(InputStream in
)
113 this(in
, BytesToUnicode
.getDefaultDecoder());
117 * This method initializes a new instance of <code>InputStreamReader</code>
118 * to read from the specified stream using a caller supplied character
119 * encoding scheme. Note that due to a deficiency in the Java language
120 * design, there is no way to determine which encodings are supported.
122 * @param in The <code>InputStream</code> to read from
123 * @param encoding_name The name of the encoding scheme to use
125 * @exception UnsupportedEncodingException If the encoding scheme
126 * requested is not available.
128 public InputStreamReader(InputStream in
, String encoding_name
)
129 throws UnsupportedEncodingException
131 this(in
, BytesToUnicode
.getDecoder(encoding_name
));
134 private InputStreamReader(InputStream in
, BytesToUnicode decoder
)
136 // FIXME: someone could pass in a BufferedInputStream whose buffer
137 // is smaller than the longest encoded character for this
138 // encoding. We will probably go into an infinite loop in this
139 // case. We probably ought to just have our own byte buffering
141 this.in
= in
instanceof BufferedInputStream
142 ?
(BufferedInputStream
) in
143 : new BufferedInputStream(in
);
144 /* Don't need to call super(in) here as long as the lock gets set. */
147 converter
.setInput(this.in
.buf
, 0, 0);
151 * This method closes this stream, as well as the underlying
152 * <code>InputStream</code>.
154 * @exception IOException If an error occurs
156 public void close() throws IOException
169 * This method returns the name of the encoding that is currently in use
170 * by this object. If the stream has been closed, this method is allowed
171 * to return <code>null</code>.
173 * @return The current encoding name
175 public String
getEncoding()
177 return in
!= null ? converter
.getName() : null;
181 * This method checks to see if the stream is read to be read. It
182 * will return <code>true</code> if is, or <code>false</code> if it is not.
183 * If the stream is not ready to be read, it could (although is not required
184 * to) block on the next read attempt.
186 * @return <code>true</code> if the stream is ready to be read,
187 * <code>false</code> otherwise
189 * @exception IOException If an error occurs
191 public boolean ready() throws IOException
196 throw new IOException("Stream closed");
201 // According to the spec, an InputStreamReader is ready if its
202 // input buffer is not empty (above), or if bytes are
203 // available on the underlying byte stream.
204 return in
.available () > 0;
209 * This method reads up to <code>length</code> characters from the stream into
210 * the specified array starting at index <code>offset</code> into the
213 * @param buf The character array to recieve the data read
214 * @param offset The offset into the array to start storing characters
215 * @param length The requested number of characters to read.
217 * @return The actual number of characters read, or -1 if end of stream.
219 * @exception IOException If an error occurs
221 public int read (char[] buf
, int offset
, int length
) throws IOException
226 throw new IOException("Stream closed");
231 int wavail
= wcount
- wpos
;
234 // Nothing waiting, so refill their buffer.
235 return refill(buf
, offset
, length
);
240 System
.arraycopy(work
, wpos
, buf
, offset
, length
);
247 * This method reads a single character of data from the stream.
249 * @return The char read, as an int, or -1 if end of stream.
251 * @exception IOException If an error occurs
253 public int read() throws IOException
258 throw new IOException("Stream closed");
260 int wavail
= wcount
- wpos
;
263 // Nothing waiting, so refill our internal buffer.
266 work
= new char[100];
267 int count
= refill(work
, 0, work
.length
);
277 // Read more bytes and convert them into the specified buffer.
278 // Returns the number of converted characters or -1 on EOF.
279 private int refill(char[] buf
, int offset
, int length
) throws IOException
283 // We have knowledge of the internals of BufferedInputStream
286 // BufferedInputStream.refill() can only be called when
288 boolean r
= in
.pos
< in
.count
|| in
.refill ();
292 converter
.setInput(in
.buf
, in
.pos
, in
.count
);
293 int count
= converter
.read(buf
, offset
, length
);
294 in
.skip(converter
.inpos
- in
.pos
);