1 /* Copyright (C) 1998, 1999, 2001 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 import gnu.gcj.convert.*;
13 * @author Per Bothner <bothner@cygnus.com>
14 * @date April 22, 1998.
16 /* Written using "Java Class Libraries", 2nd edition, plus online
17 * API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct, but only supports 8859_1.
21 public class InputStreamReader extends Reader
23 BufferedInputStream in;
25 // Buffer of chars read from in and converted but not consumed.
27 // Next available character (in work buffer) to read.
29 // Last available character (in work buffer) to read.
32 BytesToUnicode converter;
34 public InputStreamReader(InputStream in)
36 this(in, BytesToUnicode.getDefaultDecoder());
39 public InputStreamReader(InputStream in, String enc)
40 throws UnsupportedEncodingException
42 this(in, BytesToUnicode.getDecoder(enc));
45 private InputStreamReader(InputStream in, BytesToUnicode decoder)
47 // FIXME: someone could pass in a BufferedInputStream whose buffer
48 // is smaller than the longest encoded character for this
49 // encoding. We will probably go into an infinite loop in this
50 // case. We probably ought to just have our own byte buffering
52 this.in = in instanceof BufferedInputStream
53 ? (BufferedInputStream) in
54 : new BufferedInputStream(in);
55 /* Don't need to call super(in) here as long as the lock gets set. */
58 converter.setInput(this.in.buf, 0, 0);
61 public void close() throws IOException
73 public String getEncoding() { return converter.getName(); }
75 public boolean ready() throws IOException
80 throw new IOException("Stream closed");
85 // According to the spec, an InputStreamReader is ready if its
86 // input buffer is not empty (above), or if bytes are
87 // available on the underlying byte stream.
88 return in.available () > 0;
92 public int read(char buf[], int offset, int length) throws IOException
97 throw new IOException("Stream closed");
102 int wavail = wcount - wpos;
105 // Nothing waiting, so refill our buffer.
108 wavail = wcount - wpos;
113 System.arraycopy(work, wpos, buf, offset, length);
119 public int read() throws IOException
124 throw new IOException("Stream closed");
126 int wavail = wcount - wpos;
129 // Nothing waiting, so refill our buffer.
138 // Read more bytes and convert them into the WORK buffer.
139 // Return false on EOF.
140 private boolean refill () throws IOException
145 work = new char[100];
149 // We have knowledge of the internals of BufferedInputStream
152 boolean r = in.refill ();
156 converter.setInput(in.buf, in.pos, in.count);
157 int count = converter.read (work, wpos, work.length - wpos);
158 in.skip(converter.inpos - in.pos);