42 |
import gnu.java.io.decode.Decoder; |
import gnu.java.io.decode.Decoder; |
43 |
|
|
44 |
/** |
/** |
45 |
* This class reads characters from a byte input stream. The characters |
* This class reads characters from a byte input stream. The characters |
46 |
* read are converted from bytes in the underlying stream by a |
* read are converted from bytes in the underlying stream by a |
47 |
* decoding layer. The decoding layer transforms bytes to chars according |
* decoding layer. The decoding layer transforms bytes to chars according |
48 |
* to an encoding standard. There are many available encodings to choose |
* to an encoding standard. There are many available encodings to choose |
49 |
* from. The desired encoding can either be specified by name, or if no |
* from. The desired encoding can either be specified by name, or if no |
50 |
* encoding is selected, the system default encoding will be used. The |
* encoding is selected, the system default encoding will be used. The |
51 |
* system default encoding name is determined from the system property |
* system default encoding name is determined from the system property |
52 |
* <code>file.encoding</code>. The only encodings that are guaranteed to |
* <code>file.encoding</code>. The only encodings that are guaranteed to |
53 |
* be availalbe are "8859_1" (the Latin-1 character set) and "UTF8". |
* be availalbe are "8859_1" (the Latin-1 character set) and "UTF8". |
54 |
* Unforunately, Java does not provide a mechanism for listing the |
* Unforunately, Java does not provide a mechanism for listing the |
55 |
* ecodings that are supported in a given implementation. |
* ecodings that are supported in a given implementation. |
56 |
* <p> |
* <p> |
57 |
* Here is a list of standard encoding names that may be available: |
* Here is a list of standard encoding names that may be available: |
58 |
* <p> |
* <p> |
59 |
* <ul> |
* <ul> |
60 |
* <li>8859_1 (ISO-8859-1/Latin-1) |
* <li>8859_1 (ISO-8859-1/Latin-1) |
61 |
* <li>8859_2 (ISO-8859-2/Latin-2) |
* <li>8859_2 (ISO-8859-2/Latin-2) |
62 |
* <li>8859_3 (ISO-8859-3/Latin-3) |
* <li>8859_3 (ISO-8859-3/Latin-3) |
63 |
* <li>8859_4 (ISO-8859-4/Latin-4) |
* <li>8859_4 (ISO-8859-4/Latin-4) |
64 |
* <li>8859_5 (ISO-8859-5/Latin-5) |
* <li>8859_5 (ISO-8859-5/Latin-5) |
65 |
* <li>8859_6 (ISO-8859-6/Latin-6) |
* <li>8859_6 (ISO-8859-6/Latin-6) |
66 |
* <li>8859_7 (ISO-8859-7/Latin-7) |
* <li>8859_7 (ISO-8859-7/Latin-7) |
67 |
* <li>8859_8 (ISO-8859-8/Latin-8) |
* <li>8859_8 (ISO-8859-8/Latin-8) |
68 |
* <li>8859_9 (ISO-8859-9/Latin-9) |
* <li>8859_9 (ISO-8859-9/Latin-9) |
69 |
* <li>ASCII (7-bit ASCII) |
* <li>ASCII (7-bit ASCII) |
70 |
* <li>UTF8 (UCS Transformation Format-8) |
* <li>UTF8 (UCS Transformation Format-8) |
71 |
* <li>More later |
* <li>More later |
72 |
* </ul> |
* </ul> |
73 |
* <p> |
* <p> |
74 |
* It is recommended that applications do not use |
* It is recommended that applications do not use |
75 |
* <code>InputStreamReader</code>'s |
* <code>InputStreamReader</code>'s |
76 |
* directly. Rather, for efficiency purposes, an object of this class |
* directly. Rather, for efficiency purposes, an object of this class |
77 |
* should be wrapped by a <code>BufferedReader</code>. |
* should be wrapped by a <code>BufferedReader</code>. |
78 |
* <p> |
* <p> |
79 |
* Due to a deficiency the Java class library design, there is no standard |
* Due to a deficiency the Java class library design, there is no standard |
80 |
* way for an application to install its own byte-character encoding. |
* way for an application to install its own byte-character encoding. |
81 |
* |
* |
82 |
* @see BufferedReader |
* @see BufferedReader |
83 |
* @see InputStream |
* @see InputStream |
84 |
* |
* |
85 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
86 |
*/ |
*/ |
87 |
public class InputStreamReader extends Reader |
public class InputStreamReader extends Reader |
88 |
{ |
{ |
89 |
/* |
/* |
90 |
* This is the byte-character decoder class that does the reading and |
* This is the byte-character decoder class that does the reading and |
91 |
* translation of bytes from the underlying stream. |
* translation of bytes from the underlying stream. |
92 |
*/ |
*/ |
93 |
private Decoder in; |
private Decoder in; |
94 |
|
|
95 |
/** |
/** |
96 |
* This method initializes a new instance of <code>InputStreamReader</code> |
* This method initializes a new instance of <code>InputStreamReader</code> |
97 |
* to read from the specified stream using the default encoding. |
* to read from the specified stream using the default encoding. |
98 |
* |
* |
99 |
* @param in The <code>InputStream</code> to read from |
* @param in The <code>InputStream</code> to read from |
100 |
*/ |
*/ |
101 |
public InputStreamReader(InputStream in) |
public InputStreamReader(InputStream in) |
102 |
{ |
{ |
103 |
if (in == null) |
if (in == null) |
106 |
} |
} |
107 |
|
|
108 |
/** |
/** |
109 |
* This method initializes a new instance of <code>InputStreamReader</code> |
* This method initializes a new instance of <code>InputStreamReader</code> |
110 |
* to read from the specified stream using a caller supplied character |
* to read from the specified stream using a caller supplied character |
111 |
* encoding scheme. Note that due to a deficiency in the Java language |
* encoding scheme. Note that due to a deficiency in the Java language |
112 |
* design, there is no way to determine which encodings are supported. |
* design, there is no way to determine which encodings are supported. |
113 |
* |
* |
114 |
* @param in The <code>InputStream</code> to read from |
* @param in The <code>InputStream</code> to read from |
115 |
* @param encoding_name The name of the encoding scheme to use |
* @param encoding_name The name of the encoding scheme to use |
116 |
* |
* |
117 |
* @exception UnsupportedEncodingException If the encoding scheme |
* @exception UnsupportedEncodingException If the encoding scheme |
118 |
* requested is not available. |
* requested is not available. |
119 |
*/ |
*/ |
120 |
public InputStreamReader(InputStream in, String encoding_name) |
public InputStreamReader(InputStream in, String encoding_name) |
121 |
throws UnsupportedEncodingException |
throws UnsupportedEncodingException |
122 |
{ |
{ |
123 |
if (in == null || encoding_name == null) |
if (in == null |
124 |
|
|| encoding_name == null) |
125 |
throw new NullPointerException(); |
throw new NullPointerException(); |
126 |
|
|
127 |
this.in = EncodingManager.getDecoder(in, encoding_name); |
this.in = EncodingManager.getDecoder(in, encoding_name); |
128 |
} |
} |
129 |
|
|
130 |
/** |
/** |
131 |
* This method returns the name of the encoding that is currently in use |
* This method closes this stream, as well as the underlying |
132 |
* by this object. If the stream has been closed, this method is allowed |
* <code>InputStream</code>. |
133 |
* to return <code>null</code>. |
* |
134 |
* |
* @exception IOException If an error occurs |
135 |
* @param The current encoding name |
*/ |
136 |
*/ |
public void close() throws IOException |
|
public String getEncoding() |
|
137 |
{ |
{ |
138 |
return(in.getSchemeName()); |
in.close(); |
139 |
} |
} |
140 |
|
|
141 |
/** |
/** |
142 |
* This method closes this stream, as well as the underlying |
* This method returns the name of the encoding that is currently in use |
143 |
* <code>InputStream</code>. |
* by this object. If the stream has been closed, this method is allowed |
144 |
* |
* to return <code>null</code>. |
145 |
* @exception IOException If an error occurs |
* |
146 |
*/ |
* @param The current encoding name |
147 |
public void close() throws IOException |
*/ |
148 |
|
public String getEncoding() |
149 |
{ |
{ |
150 |
in.close(); |
return(in.getSchemeName()); |
151 |
} |
} |
152 |
|
|
153 |
/** |
/** |
154 |
* This method checks to see if the stream is read to be read. It |
* This method checks to see if the stream is read to be read. It |
155 |
* will return <code>true</code> if is, or <code>false</code> if it is not. |
* will return <code>true</code> if is, or <code>false</code> if it is not. |
156 |
* If the stream is not ready to be read, it could (although is not required |
* If the stream is not ready to be read, it could (although is not required |
157 |
* to) block on the next read attempt. |
* to) block on the next read attempt. |
158 |
* |
* |
159 |
* @return <code>true</code> if the stream is ready to be read, |
* @return <code>true</code> if the stream is ready to be read, |
160 |
* <code>false</code> otherwise |
* <code>false</code> otherwise |
161 |
* |
* |
162 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
163 |
*/ |
*/ |
164 |
public boolean ready() throws IOException |
public boolean ready() throws IOException |
165 |
{ |
{ |
166 |
return(in.ready()); |
return(in.ready()); |
167 |
} |
} |
168 |
|
|
169 |
/** |
/** |
170 |
* This method reads a single character of data from the stream. |
* This method reads up to <code>len</code> characters from the stream into |
171 |
* |
* the specified array starting at index <code>offset</code> into the |
172 |
* @return The char read, as an int, or -1 if end of stream. |
* array. |
173 |
* |
* |
174 |
* @exception IOException If an error occurs |
* @param buf The character array to recieve the data read |
175 |
*/ |
* @param offset The offset into the array to start storing characters |
176 |
public int read() throws IOException |
* @param len The requested number of characters to read. |
177 |
|
* |
178 |
|
* @return The actual number of characters read, or -1 if end of stream. |
179 |
|
* |
180 |
|
* @exception IOException If an error occurs |
181 |
|
*/ |
182 |
|
public int read(char[] buf, int offset, int len) throws IOException |
183 |
{ |
{ |
184 |
return(in.read()); |
return(in.read(buf, offset, len)); |
185 |
} |
} |
186 |
|
|
187 |
/** |
/** |
188 |
* This method reads up to <code>len</code> characters from the stream into |
* This method reads a single character of data from the stream. |
189 |
* the specified array starting at index <code>offset</code> into the |
* |
190 |
* array. |
* @return The char read, as an int, or -1 if end of stream. |
191 |
* |
* |
192 |
* @param buf The character array to recieve the data read |
* @exception IOException If an error occurs |
193 |
* @param offset The offset into the array to start storing characters |
*/ |
194 |
* @param len The requested number of characters to read. |
public int read() throws IOException |
|
* |
|
|
* @return The actual number of characters read, or -1 if end of stream. |
|
|
* |
|
|
* @exception IOException If an error occurs |
|
|
*/ |
|
|
public int read(char[] buf, int offset, int len) throws IOException |
|
195 |
{ |
{ |
196 |
return(in.read(buf, offset, len)); |
return(in.read()); |
197 |
} |
} |
198 |
|
|
199 |
} // class InputStreamReader |
} // class InputStreamReader |