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: /**
030: * Abstract class for reading character streams. The only methods that a
031: * subclass must implement are read(char[], int, int) and close(). Most
032: * subclasses, however, will override some of the methods defined here in order
033: * to provide higher efficiency, additional functionality, or both.
034: *
035: * @version 12/17/01 (CLDC 1.1)
036: * @see java.io.InputStreamReader
037: * @see java.io.Writer
038: * @since JDK1.1, CLDC 1.0
039: */
040:
041: public abstract class Reader {
042:
043: /**
044: * The object used to synchronize operations on this stream. For
045: * efficiency, a character-stream object may use an object other than
046: * itself to protect critical sections. A subclass should therefore use
047: * the object in this field rather than <tt>this</tt> or a synchronized
048: * method.
049: */
050: protected Object lock;
051:
052: /**
053: * Create a new character-stream reader whose critical sections will
054: * synchronize on the reader itself.
055: */
056: protected Reader() {
057: this .lock = this ;
058: }
059:
060: /**
061: * Create a new character-stream reader whose critical sections will
062: * synchronize on the given object.
063: *
064: * @param lock The Object to synchronize on.
065: */
066: protected Reader(Object lock) {
067: if (lock == null) {
068: throw new NullPointerException();
069: }
070: this .lock = lock;
071: }
072:
073: /**
074: * Read a single character. This method will block until a character is
075: * available, an I/O error occurs, or the end of the stream is reached.
076: *
077: * <p> Subclasses that intend to support efficient single-character input
078: * should override this method.
079: *
080: * @return The character read, as an integer in the range 0 to 65535
081: * (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
082: * been reached
083: *
084: * @exception IOException If an I/O error occurs
085: */
086: public int read() throws IOException {
087: char cb[] = new char[1];
088: if (read(cb, 0, 1) == -1)
089: return -1;
090: else
091: return cb[0];
092: }
093:
094: /**
095: * Read characters into an array. This method will block until some input
096: * is available, an I/O error occurs, or the end of the stream is reached.
097: *
098: * @param cbuf Destination buffer
099: *
100: * @return The number of bytes read, or -1 if the end of the stream
101: * has been reached
102: *
103: * @exception IOException If an I/O error occurs
104: */
105: public int read(char cbuf[]) throws IOException {
106: return read(cbuf, 0, cbuf.length);
107: }
108:
109: /**
110: * Read characters into a portion of an array. This method will block
111: * until some input is available, an I/O error occurs, or the end of the
112: * stream is reached.
113: *
114: * @param cbuf Destination buffer
115: * @param off Offset at which to start storing characters
116: * @param len Maximum number of characters to read
117: *
118: * @return The number of characters read, or -1 if the end of the
119: * stream has been reached
120: *
121: * @exception IOException If an I/O error occurs
122: */
123: abstract public int read(char cbuf[], int off, int len)
124: throws IOException;
125:
126: /**
127: * Maximum skip-buffer size
128: */
129: private static final int maxSkipBufferSize = 8192;
130:
131: /**
132: * Skip buffer, null until allocated
133: */
134: private char skipBuffer[] = null;
135:
136: /**
137: * Skip characters. This method will block until some characters are
138: * available, an I/O error occurs, or the end of the stream is reached.
139: *
140: * @param n The number of characters to skip
141: *
142: * @return The number of characters actually skipped
143: *
144: * @exception IllegalArgumentException If <code>n</code> is negative.
145: * @exception IOException If an I/O error occurs
146: */
147: public long skip(long n) throws IOException {
148: if (n < 0L)
149: throw new IllegalArgumentException(
150: /* #ifdef VERBOSE_EXCEPTIONS */
151: /// skipped "skip value is negative"
152: /* #endif */
153: );
154: int nn = (int) Math.min(n, maxSkipBufferSize);
155: synchronized (lock) {
156: if ((skipBuffer == null) || (skipBuffer.length < nn))
157: skipBuffer = new char[nn];
158: long r = n;
159: while (r > 0) {
160: int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
161: if (nc == -1)
162: break;
163: r -= nc;
164: }
165: return n - r;
166: }
167: }
168:
169: /**
170: * Tell whether this stream is ready to be read.
171: *
172: * @return True if the next read() is guaranteed not to block for input,
173: * false otherwise. Note that returning false does not guarantee that the
174: * next read will block.
175: *
176: * @exception IOException If an I/O error occurs
177: */
178: public boolean ready() throws IOException {
179: return false;
180: }
181:
182: /**
183: * Tell whether this stream supports the mark() operation. The default
184: * implementation always returns false. Subclasses should override this
185: * method.
186: *
187: * @return true if and only if this stream supports the mark operation.
188: */
189: public boolean markSupported() {
190: return false;
191: }
192:
193: /**
194: * Mark the present position in the stream. Subsequent calls to reset()
195: * will attempt to reposition the stream to this point. Not all
196: * character-input streams support the mark() operation.
197: *
198: * @param readAheadLimit Limit on the number of characters that may be
199: * read while still preserving the mark. After
200: * reading this many characters, attempting to
201: * reset the stream may fail.
202: *
203: * @exception IOException If the stream does not support mark(),
204: * or if some other I/O error occurs
205: */
206: public void mark(int readAheadLimit) throws IOException {
207: throw new IOException(
208: /* #ifdef VERBOSE_EXCEPTIONS */
209: /// skipped "mark() not supported"
210: /* #endif */
211: );
212: }
213:
214: /**
215: * Reset the stream. If the stream has been marked, then attempt to
216: * reposition it at the mark. If the stream has not been marked, then
217: * attempt to reset it in some way appropriate to the particular stream,
218: * for example by repositioning it to its starting point. Not all
219: * character-input streams support the reset() operation, and some support
220: * reset() without supporting mark().
221: *
222: * @exception IOException If the stream has not been marked,
223: * or if the mark has been invalidated,
224: * or if the stream does not support reset(),
225: * or if some other I/O error occurs
226: */
227: public void reset() throws IOException {
228: throw new IOException(
229: /* #ifdef VERBOSE_EXCEPTIONS */
230: /// skipped "reset() not supported"
231: /* #endif */
232: );
233: }
234:
235: /**
236: * Close the stream. Once a stream has been closed, further read(),
237: * ready(), mark(), or reset() invocations will throw an IOException.
238: * Closing a previously-closed stream, however, has no effect.
239: *
240: * @exception IOException If an I/O error occurs
241: */
242: abstract public void close() throws IOException;
243:
244: }
|