]> gcc.gnu.org Git - gcc.git/blob - libjava/java/io/InputStreamReader.java
ae5e2c7b0eb016f13d6e2f7130ed1f6745997e67
[gcc.git] / libjava / java / io / InputStreamReader.java
1 /* Copyright (C) 1998, 1999 Cygnus Solutions
2
3 This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
8
9 package java.io;
10 import gnu.gcj.convert.*;
11
12 /**
13 * @author Per Bothner <bothner@cygnus.com>
14 * @date April 22, 1998.
15 */
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.
19 */
20
21 public class InputStreamReader extends Reader
22 {
23 BufferedInputStream in;
24
25 // Buffer of chars read from in and converted but not consumed.
26 char[] work;
27 // Next available character (in work buffer) to read.
28 int wpos;
29 // Last available character (in work buffer) to read.
30 int wcount;
31
32 BytesToUnicode converter;
33
34 public InputStreamReader(InputStream in)
35 {
36 this(in, BytesToUnicode.getDefaultDecoder());
37 }
38
39 public InputStreamReader(InputStream in, String enc)
40 throws UnsupportedEncodingException
41 {
42 this(in, BytesToUnicode.getDecoder(enc));
43 }
44
45 private InputStreamReader(InputStream in, BytesToUnicode decoder)
46 {
47 super((this.in = (in instanceof BufferedInputStream
48 ? (BufferedInputStream) in
49 : new BufferedInputStream(in, 250))));
50 converter = decoder;
51 converter.setInput(this.in.buf, 0, 0);
52 }
53
54 public void close() throws IOException
55 {
56 synchronized (lock)
57 {
58 if (in != null)
59 in.close();
60 in = null;
61 work = null;
62 wpos = wcount = 0;
63 }
64 }
65
66 public String getEncoding() { return converter.getName(); }
67
68 public boolean ready() throws IOException
69 {
70 synchronized (lock)
71 {
72 if (wpos < wcount)
73 return true;
74 if (work == null)
75 {
76 work = new char[100];
77 wpos = 0;
78 wcount = 0;
79 }
80 for (;;)
81 {
82 if (in.available() <= 0)
83 return false;
84 in.mark(1);
85 int b = in.read();
86 if (b < 0)
87 return true;
88 in.reset();
89 converter.setInput(in.buf, in.pos, in.count);
90 wpos = 0;
91 wcount = converter.read(work, 0, work.length);
92 in.skip(converter.inpos - in.pos);
93 if (wcount > 0)
94 return true;
95 }
96 }
97 }
98
99 public int read(char buf[], int offset, int length) throws IOException
100 {
101 synchronized (lock)
102 {
103 int wavail = wcount - wpos;
104 if (wavail > 0)
105 {
106 if (length > wavail)
107 length = wavail;
108 System.arraycopy(work, wpos, buf, offset, length);
109 wpos += length;
110 return length;
111 }
112 else
113 {
114 if (length == 0)
115 return 0;
116 for (;;)
117 {
118 in.mark(1);
119 int b = in.read();
120 if (b < 0)
121 return -1;
122 in.reset();
123 converter.setInput(in.buf, in.pos, in.count);
124 int count = converter.read (buf, offset, length);
125 in.skip(converter.inpos - in.pos);
126 if (count > 0)
127 return count;
128 }
129 }
130 }
131 }
132
133 public int read() throws IOException
134 {
135 synchronized (lock)
136 {
137 int wavail = wcount - wpos;
138 if (wavail > 0)
139 return work[wpos++];
140 if (work == null)
141 {
142 work = new char[100];
143 wpos = 0;
144 wcount = 0;
145 }
146 else if (wavail == 0)
147 {
148 wpos = 0;
149 wcount = 0;
150 }
151 int count = read(work, wpos, work.length-wpos);
152 if (count <= 0)
153 return -1;
154 wcount = wpos + count;
155 return work[wpos++];
156 }
157 }
158 }
This page took 0.043087 seconds and 4 git commands to generate.