001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package java.io;
028:
029: import com.sun.cldc.i18n.*;
030:
031: /**
032: * An InputStreamReader is a bridge from byte streams to character streams:
033: * It reads bytes and translates them into characters.
034: * The encoding that it uses may be specified by name, or the platform's
035: * default encoding may be accepted.
036: *
037: * <p> Each invocation of one of an InputStreamReader's read() methods may
038: * cause one or more bytes to be read from the underlying byte input stream.
039: * To enable the efficient conversion of bytes to characters, more bytes may
040: * be read ahead from the underlying stream than are necessary to satisfy the
041: * current read operation.
042: *
043: * @version 12/17/01 (CLDC 1.1)
044: * @see java.io.Reader
045: * @see java.io.UnsupportedEncodingException
046: * @since CLDC 1.0
047: */
048:
049: public class InputStreamReader extends Reader {
050:
051: /**
052: * The underlying character input stream.
053: */
054: private Reader in;
055:
056: /**
057: * Create an InputStreamReader that uses the default character encoding.
058: *
059: * @param is An InputStream
060: */
061: public InputStreamReader(InputStream is) {
062: in = Helper.getStreamReader(is);
063: }
064:
065: /**
066: * Create an InputStreamReader that uses the named character encoding.
067: *
068: * @param is An InputStream
069: * @param enc The name of a supported character encoding
070: *
071: * @exception UnsupportedEncodingException
072: * If the named encoding is not supported
073: */
074: public InputStreamReader(InputStream is, String enc)
075: throws UnsupportedEncodingException {
076: in = Helper.getStreamReader(is, enc);
077: }
078:
079: /**
080: * Check to make sure that the stream has not been closed
081: */
082: private void ensureOpen() throws IOException {
083: if (in == null) {
084: throw new IOException(
085: /* #ifdef VERBOSE_EXCEPTIONS */
086: /// skipped "Stream closed"
087: /* #endif */
088: );
089: }
090: }
091:
092: /**
093: * Read a single character.
094: *
095: * @return The character read, or -1 if the end of the stream has
096: * been reached
097: *
098: * @exception IOException If an I/O error occurs
099: */
100: public int read() throws IOException {
101: ensureOpen();
102: return in.read();
103: }
104:
105: /**
106: * Read characters into a portion of an array.
107: *
108: * @param cbuf Destination buffer
109: * @param off Offset at which to start storing characters
110: * @param len Maximum number of characters to read
111: *
112: * @return The number of characters read, or -1 if the end of
113: * the stream has been reached
114: *
115: * @exception IOException If an I/O error occurs
116: */
117: public int read(char cbuf[], int off, int len) throws IOException {
118: ensureOpen();
119: if ((off < 0) || (off > cbuf.length) || (len < 0)
120: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
121: throw new IndexOutOfBoundsException();
122: } else if (len == 0) {
123: return 0;
124: }
125: return in.read(cbuf, off, len);
126: }
127:
128: /**
129: * Skip characters.
130: *
131: * @param n The number of characters to skip
132: *
133: * @return The number of characters actually skipped
134: *
135: * @exception IllegalArgumentException If <code>n</code> is negative.
136: * @exception IOException If an I/O error occurs
137: */
138: public long skip(long n) throws IOException {
139: ensureOpen();
140: return in.skip(n);
141: }
142:
143: /**
144: * Tell whether this stream is ready to be read.
145: *
146: * @return True if the next read() is guaranteed not to block for input,
147: * false otherwise. Note that returning false does not guarantee that the
148: * next read will block.
149: *
150: * @exception IOException If an I/O error occurs
151: */
152: public boolean ready() throws IOException {
153: ensureOpen();
154: return in.ready();
155: }
156:
157: /**
158: * Tell whether this stream supports the mark() operation.
159: *
160: * @return true if and only if this stream supports the mark operation.
161: */
162: public boolean markSupported() {
163: if (in == null) {
164: return false;
165: }
166: return in.markSupported();
167: }
168:
169: /**
170: * Mark the present position in the stream.
171: *
172: * @param readAheadLimit Limit on the number of characters that may be
173: * read while still preserving the mark. After
174: * reading this many characters, attempting to
175: * reset the stream may fail.
176: *
177: * @exception IOException If the stream does not support mark(),
178: * or if some other I/O error occurs
179: */
180: public void mark(int readAheadLimit) throws IOException {
181: ensureOpen();
182: if (in.markSupported()) {
183: in.mark(readAheadLimit);
184: } else {
185: throw new IOException(
186: /* #ifdef VERBOSE_EXCEPTIONS */
187: /// skipped "mark() not supported"
188: /* #endif */
189: );
190: }
191: }
192:
193: /**
194: * Reset the stream.
195: *
196: * @exception IOException If an I/O error occurs
197: */
198: public void reset() throws IOException {
199: ensureOpen();
200: in.reset();
201: }
202:
203: /**
204: * Close the stream. Closing a previously closed stream
205: * has no effect.
206: *
207: * @exception IOException If an I/O error occurs
208: */
209: public void close() throws IOException {
210: if (in != null) {
211: in.close();
212: in = null;
213: }
214: }
215:
216: }
|