jaroslav@557
|
1 |
/*
|
jaroslav@557
|
2 |
* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@557
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@557
|
4 |
*
|
jaroslav@557
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@557
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@557
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@557
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@557
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@557
|
10 |
*
|
jaroslav@557
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@557
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@557
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@557
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@557
|
15 |
* accompanied this code).
|
jaroslav@557
|
16 |
*
|
jaroslav@557
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@557
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@557
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@557
|
20 |
*
|
jaroslav@557
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@557
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@557
|
23 |
* questions.
|
jaroslav@557
|
24 |
*/
|
jaroslav@557
|
25 |
|
jaroslav@557
|
26 |
package java.io;
|
jaroslav@557
|
27 |
|
jaroslav@557
|
28 |
|
jaroslav@557
|
29 |
/**
|
jaroslav@557
|
30 |
* An InputStreamReader is a bridge from byte streams to character streams: It
|
jaroslav@557
|
31 |
* reads bytes and decodes them into characters using a specified {@link
|
jaroslav@557
|
32 |
* java.nio.charset.Charset <code>charset</code>}. The charset that it uses
|
jaroslav@557
|
33 |
* may be specified by name or may be given explicitly, or the platform's
|
jaroslav@557
|
34 |
* default charset may be accepted.
|
jaroslav@557
|
35 |
*
|
jaroslav@557
|
36 |
* <p> Each invocation of one of an InputStreamReader's read() methods may
|
jaroslav@557
|
37 |
* cause one or more bytes to be read from the underlying byte-input stream.
|
jaroslav@557
|
38 |
* To enable the efficient conversion of bytes to characters, more bytes may
|
jaroslav@557
|
39 |
* be read ahead from the underlying stream than are necessary to satisfy the
|
jaroslav@557
|
40 |
* current read operation.
|
jaroslav@557
|
41 |
*
|
jaroslav@557
|
42 |
* <p> For top efficiency, consider wrapping an InputStreamReader within a
|
jaroslav@557
|
43 |
* BufferedReader. For example:
|
jaroslav@557
|
44 |
*
|
jaroslav@557
|
45 |
* <pre>
|
jaroslav@557
|
46 |
* BufferedReader in
|
jaroslav@557
|
47 |
* = new BufferedReader(new InputStreamReader(System.in));
|
jaroslav@557
|
48 |
* </pre>
|
jaroslav@557
|
49 |
*
|
jaroslav@557
|
50 |
* @see BufferedReader
|
jaroslav@557
|
51 |
* @see InputStream
|
jaroslav@557
|
52 |
* @see java.nio.charset.Charset
|
jaroslav@557
|
53 |
*
|
jaroslav@557
|
54 |
* @author Mark Reinhold
|
jaroslav@557
|
55 |
* @since JDK1.1
|
jaroslav@557
|
56 |
*/
|
jaroslav@557
|
57 |
|
jaroslav@557
|
58 |
public class InputStreamReader extends Reader {
|
jaroslav@557
|
59 |
|
jaroslav@557
|
60 |
/**
|
jaroslav@557
|
61 |
* Creates an InputStreamReader that uses the default charset.
|
jaroslav@557
|
62 |
*
|
jaroslav@557
|
63 |
* @param in An InputStream
|
jaroslav@557
|
64 |
*/
|
jaroslav@557
|
65 |
public InputStreamReader(InputStream in) {
|
jaroslav@557
|
66 |
super(in);
|
jaroslav@557
|
67 |
}
|
jaroslav@557
|
68 |
|
jaroslav@557
|
69 |
/**
|
jaroslav@557
|
70 |
* Creates an InputStreamReader that uses the named charset.
|
jaroslav@557
|
71 |
*
|
jaroslav@557
|
72 |
* @param in
|
jaroslav@557
|
73 |
* An InputStream
|
jaroslav@557
|
74 |
*
|
jaroslav@557
|
75 |
* @param charsetName
|
jaroslav@557
|
76 |
* The name of a supported
|
jaroslav@557
|
77 |
* {@link java.nio.charset.Charset </code>charset<code>}
|
jaroslav@557
|
78 |
*
|
jaroslav@557
|
79 |
* @exception UnsupportedEncodingException
|
jaroslav@557
|
80 |
* If the named charset is not supported
|
jaroslav@557
|
81 |
*/
|
jaroslav@557
|
82 |
public InputStreamReader(InputStream in, String charsetName)
|
jaroslav@557
|
83 |
throws UnsupportedEncodingException
|
jaroslav@557
|
84 |
{
|
jaroslav@557
|
85 |
super(in);
|
jaroslav@561
|
86 |
if (!charsetName.toUpperCase().equals("UTF-8")) {
|
jaroslav@561
|
87 |
throw new UnsupportedEncodingException(charsetName);
|
jaroslav@561
|
88 |
}
|
jaroslav@557
|
89 |
}
|
jaroslav@557
|
90 |
|
jaroslav@557
|
91 |
/**
|
jaroslav@557
|
92 |
* Creates an InputStreamReader that uses the given charset. </p>
|
jaroslav@557
|
93 |
*
|
jaroslav@557
|
94 |
* @param in An InputStream
|
jaroslav@557
|
95 |
* @param cs A charset
|
jaroslav@557
|
96 |
*
|
jaroslav@557
|
97 |
* @since 1.4
|
jaroslav@557
|
98 |
* @spec JSR-51
|
jaroslav@557
|
99 |
*/
|
jaroslav@560
|
100 |
/* XXX:
|
jaroslav@557
|
101 |
public InputStreamReader(InputStream in, Charset cs) {
|
jaroslav@557
|
102 |
super(in);
|
jaroslav@557
|
103 |
if (cs == null)
|
jaroslav@557
|
104 |
throw new NullPointerException("charset");
|
jaroslav@557
|
105 |
sd = StreamDecoder.forInputStreamReader(in, this, cs);
|
jaroslav@557
|
106 |
}
|
jaroslav@560
|
107 |
*/
|
jaroslav@557
|
108 |
/**
|
jaroslav@557
|
109 |
* Creates an InputStreamReader that uses the given charset decoder. </p>
|
jaroslav@557
|
110 |
*
|
jaroslav@557
|
111 |
* @param in An InputStream
|
jaroslav@557
|
112 |
* @param dec A charset decoder
|
jaroslav@557
|
113 |
*
|
jaroslav@557
|
114 |
* @since 1.4
|
jaroslav@557
|
115 |
* @spec JSR-51
|
jaroslav@557
|
116 |
*/
|
jaroslav@560
|
117 |
/* XXX:
|
jaroslav@557
|
118 |
public InputStreamReader(InputStream in, CharsetDecoder dec) {
|
jaroslav@557
|
119 |
super(in);
|
jaroslav@557
|
120 |
if (dec == null)
|
jaroslav@557
|
121 |
throw new NullPointerException("charset decoder");
|
jaroslav@557
|
122 |
sd = StreamDecoder.forInputStreamReader(in, this, dec);
|
jaroslav@557
|
123 |
}
|
jaroslav@560
|
124 |
*/
|
jaroslav@560
|
125 |
|
jaroslav@557
|
126 |
/**
|
jaroslav@557
|
127 |
* Returns the name of the character encoding being used by this stream.
|
jaroslav@557
|
128 |
*
|
jaroslav@557
|
129 |
* <p> If the encoding has an historical name then that name is returned;
|
jaroslav@557
|
130 |
* otherwise the encoding's canonical name is returned.
|
jaroslav@557
|
131 |
*
|
jaroslav@557
|
132 |
* <p> If this instance was created with the {@link
|
jaroslav@557
|
133 |
* #InputStreamReader(InputStream, String)} constructor then the returned
|
jaroslav@557
|
134 |
* name, being unique for the encoding, may differ from the name passed to
|
jaroslav@557
|
135 |
* the constructor. This method will return <code>null</code> if the
|
jaroslav@557
|
136 |
* stream has been closed.
|
jaroslav@557
|
137 |
* </p>
|
jaroslav@557
|
138 |
* @return The historical name of this encoding, or
|
jaroslav@557
|
139 |
* <code>null</code> if the stream has been closed
|
jaroslav@557
|
140 |
*
|
jaroslav@557
|
141 |
* @see java.nio.charset.Charset
|
jaroslav@557
|
142 |
*
|
jaroslav@557
|
143 |
* @revised 1.4
|
jaroslav@557
|
144 |
* @spec JSR-51
|
jaroslav@557
|
145 |
*/
|
jaroslav@557
|
146 |
public String getEncoding() {
|
jaroslav@561
|
147 |
return "UTF-8";
|
jaroslav@557
|
148 |
}
|
jaroslav@557
|
149 |
|
jaroslav@557
|
150 |
/**
|
jaroslav@557
|
151 |
* Reads a single character.
|
jaroslav@557
|
152 |
*
|
jaroslav@557
|
153 |
* @return The character read, or -1 if the end of the stream has been
|
jaroslav@557
|
154 |
* reached
|
jaroslav@557
|
155 |
*
|
jaroslav@557
|
156 |
* @exception IOException If an I/O error occurs
|
jaroslav@557
|
157 |
*/
|
jaroslav@557
|
158 |
public int read() throws IOException {
|
jaroslav@595
|
159 |
final InputStream is = (InputStream)lock;
|
jaroslav@595
|
160 |
int c = is.read();
|
jaroslav@595
|
161 |
if (c == -1) {
|
jaroslav@595
|
162 |
return -1;
|
jaroslav@595
|
163 |
}
|
jaroslav@595
|
164 |
c = (int) c & 0xff;
|
jaroslav@595
|
165 |
switch (c >> 4) {
|
jaroslav@595
|
166 |
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
|
jaroslav@595
|
167 |
/* 0xxxxxxx*/
|
jaroslav@595
|
168 |
return c;
|
jaroslav@595
|
169 |
case 12: case 13: {
|
jaroslav@595
|
170 |
/* 110x xxxx 10xx xxxx*/
|
jaroslav@595
|
171 |
int char2 = (int) is.read();
|
jaroslav@595
|
172 |
if ((char2 & 0xC0) != 0x80)
|
jaroslav@595
|
173 |
throw new UTFDataFormatException("malformed input");
|
jaroslav@595
|
174 |
return (((c & 0x1F) << 6) | (char2 & 0x3F));
|
jaroslav@595
|
175 |
}
|
jaroslav@595
|
176 |
case 14: {
|
jaroslav@595
|
177 |
/* 1110 xxxx 10xx xxxx 10xx xxxx */
|
jaroslav@595
|
178 |
int char2 = is.read();
|
jaroslav@595
|
179 |
int char3 = is.read();
|
jaroslav@595
|
180 |
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
|
jaroslav@595
|
181 |
throw new UTFDataFormatException("malformed input");
|
jaroslav@595
|
182 |
return (((c & 0x0F) << 12) |
|
jaroslav@595
|
183 |
((char2 & 0x3F) << 6) |
|
jaroslav@595
|
184 |
((char3 & 0x3F) << 0));
|
jaroslav@595
|
185 |
}
|
jaroslav@595
|
186 |
default:
|
jaroslav@595
|
187 |
/* 10xx xxxx, 1111 xxxx */
|
jaroslav@595
|
188 |
throw new UTFDataFormatException("malformed input");
|
jaroslav@595
|
189 |
}
|
jaroslav@557
|
190 |
}
|
jaroslav@595
|
191 |
|
jaroslav@557
|
192 |
/**
|
jaroslav@557
|
193 |
* Reads characters into a portion of an array.
|
jaroslav@557
|
194 |
*
|
jaroslav@557
|
195 |
* @param cbuf Destination buffer
|
jaroslav@557
|
196 |
* @param offset Offset at which to start storing characters
|
jaroslav@557
|
197 |
* @param length Maximum number of characters to read
|
jaroslav@557
|
198 |
*
|
jaroslav@557
|
199 |
* @return The number of characters read, or -1 if the end of the
|
jaroslav@557
|
200 |
* stream has been reached
|
jaroslav@557
|
201 |
*
|
jaroslav@557
|
202 |
* @exception IOException If an I/O error occurs
|
jaroslav@557
|
203 |
*/
|
jaroslav@557
|
204 |
public int read(char cbuf[], int offset, int length) throws IOException {
|
jaroslav@561
|
205 |
for (int i = 0; i < length; i++) {
|
jaroslav@561
|
206 |
int ch = read();
|
jaroslav@561
|
207 |
if (ch == -1) {
|
jaroslav@561
|
208 |
if (i == 0) return -1;
|
jaroslav@561
|
209 |
return i;
|
jaroslav@561
|
210 |
}
|
jaroslav@561
|
211 |
cbuf[offset++] = (char) ch;
|
jaroslav@561
|
212 |
}
|
jaroslav@561
|
213 |
return length;
|
jaroslav@557
|
214 |
}
|
jaroslav@557
|
215 |
|
jaroslav@557
|
216 |
/**
|
jaroslav@557
|
217 |
* Tells whether this stream is ready to be read. An InputStreamReader is
|
jaroslav@557
|
218 |
* ready if its input buffer is not empty, or if bytes are available to be
|
jaroslav@557
|
219 |
* read from the underlying byte stream.
|
jaroslav@557
|
220 |
*
|
jaroslav@557
|
221 |
* @exception IOException If an I/O error occurs
|
jaroslav@557
|
222 |
*/
|
jaroslav@557
|
223 |
public boolean ready() throws IOException {
|
jaroslav@561
|
224 |
return ((InputStream)lock).available() > 0;
|
jaroslav@557
|
225 |
}
|
jaroslav@557
|
226 |
|
jaroslav@557
|
227 |
public void close() throws IOException {
|
jaroslav@561
|
228 |
((InputStream)lock).close();
|
jaroslav@557
|
229 |
}
|
jaroslav@557
|
230 |
}
|