KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > Reader


1 /*
2  * @(#)Reader.java 1.27 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10
11 /**
12  * Abstract class for reading character streams. The only methods that a
13  * subclass must implement are read(char[], int, int) and close(). Most
14  * subclasses, however, will override some of the methods defined here in order
15  * to provide higher efficiency, additional functionality, or both.
16  *
17  *
18  * @see BufferedReader
19  * @see LineNumberReader
20  * @see CharArrayReader
21  * @see InputStreamReader
22  * @see FileReader
23  * @see FilterReader
24  * @see PushbackReader
25  * @see PipedReader
26  * @see StringReader
27  * @see Writer
28  *
29  * @version 1.27, 03/12/19
30  * @author Mark Reinhold
31  * @since JDK1.1
32  */

33
34 public abstract class Reader implements Readable JavaDoc, Closeable JavaDoc {
35
36     /**
37      * The object used to synchronize operations on this stream. For
38      * efficiency, a character-stream object may use an object other than
39      * itself to protect critical sections. A subclass should therefore use
40      * the object in this field rather than <tt>this</tt> or a synchronized
41      * method.
42      */

43     protected Object JavaDoc lock;
44
45     /**
46      * Create a new character-stream reader whose critical sections will
47      * synchronize on the reader itself.
48      */

49     protected Reader() {
50     this.lock = this;
51     }
52
53     /**
54      * Create a new character-stream reader whose critical sections will
55      * synchronize on the given object.
56      *
57      * @param lock The Object to synchronize on.
58      */

59     protected Reader(Object JavaDoc lock) {
60     if (lock == null) {
61         throw new NullPointerException JavaDoc();
62     }
63     this.lock = lock;
64     }
65
66     /**
67      * Attempts to read characters into the specified character buffer.
68      * The buffer is used as a repository of characters as-is: the only
69      * changes made are the results of a put operation. No flipping or
70      * rewinding of the buffer is performed.
71      *
72      * @param target the buffer to read characters into
73      * @return The number of characters added to the buffer, or
74      * -1 if this source of characters is at its end
75      * @throws IOException if an I/O error occurs
76      * @throws NullPointerException if target is null
77      * @throws ReadOnlyBufferException if target is a read only buffer
78      */

79     public int read(java.nio.CharBuffer JavaDoc target) throws IOException JavaDoc {
80         int len = target.remaining();
81         char[] cbuf = new char[len];
82         int n = read(cbuf, 0, len);
83         if (n > 0)
84             target.put(cbuf, 0, n);
85         return n;
86     }
87
88     /**
89      * Read a single character. This method will block until a character is
90      * available, an I/O error occurs, or the end of the stream is reached.
91      *
92      * <p> Subclasses that intend to support efficient single-character input
93      * should override this method.
94      *
95      * @return The character read, as an integer in the range 0 to 65535
96      * (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
97      * been reached
98      *
99      * @exception IOException If an I/O error occurs
100      */

101     public int read() throws IOException JavaDoc {
102     char cb[] = new char[1];
103     if (read(cb, 0, 1) == -1)
104         return -1;
105     else
106         return cb[0];
107     }
108
109     /**
110      * Read characters into an array. This method will block until some input
111      * is available, an I/O error occurs, or the end of the stream is reached.
112      *
113      * @param cbuf Destination buffer
114      *
115      * @return The number of characters read, or -1
116      * if the end of the stream
117      * has been reached
118      *
119      * @exception IOException If an I/O error occurs
120      */

121     public int read(char cbuf[]) throws IOException JavaDoc {
122     return read(cbuf, 0, cbuf.length);
123     }
124
125     /**
126      * Read characters into a portion of an array. This method will block
127      * until some input is available, an I/O error occurs, or the end of the
128      * stream is reached.
129      *
130      * @param cbuf Destination buffer
131      * @param off Offset at which to start storing characters
132      * @param len Maximum number of characters to read
133      *
134      * @return The number of characters read, or -1 if the end of the
135      * stream has been reached
136      *
137      * @exception IOException If an I/O error occurs
138      */

139     abstract public int read(char cbuf[], int off, int len) throws IOException JavaDoc;
140
141     /** Maximum skip-buffer size */
142     private static final int maxSkipBufferSize = 8192;
143
144     /** Skip buffer, null until allocated */
145     private char skipBuffer[] = null;
146
147     /**
148      * Skip characters. This method will block until some characters are
149      * available, an I/O error occurs, or the end of the stream is reached.
150      *
151      * @param n The number of characters to skip
152      *
153      * @return The number of characters actually skipped
154      *
155      * @exception IllegalArgumentException If <code>n</code> is negative.
156      * @exception IOException If an I/O error occurs
157      */

158     public long skip(long n) throws IOException JavaDoc {
159     if (n < 0L)
160         throw new IllegalArgumentException JavaDoc("skip value is negative");
161     int nn = (int) Math.min(n, maxSkipBufferSize);
162     synchronized (lock) {
163         if ((skipBuffer == null) || (skipBuffer.length < nn))
164         skipBuffer = new char[nn];
165         long r = n;
166         while (r > 0) {
167         int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
168         if (nc == -1)
169             break;
170         r -= nc;
171         }
172         return n - r;
173     }
174     }
175
176     /**
177      * Tell whether this stream is ready to be read.
178      *
179      * @return True if the next read() is guaranteed not to block for input,
180      * false otherwise. Note that returning false does not guarantee that the
181      * next read will block.
182      *
183      * @exception IOException If an I/O error occurs
184      */

185     public boolean ready() throws IOException JavaDoc {
186     return false;
187     }
188
189     /**
190      * Tell whether this stream supports the mark() operation. The default
191      * implementation always returns false. Subclasses should override this
192      * method.
193      *
194      * @return true if and only if this stream supports the mark operation.
195      */

196     public boolean markSupported() {
197     return false;
198     }
199
200     /**
201      * Mark the present position in the stream. Subsequent calls to reset()
202      * will attempt to reposition the stream to this point. Not all
203      * character-input streams support the mark() operation.
204      *
205      * @param readAheadLimit Limit on the number of characters that may be
206      * read while still preserving the mark. After
207      * reading this many characters, attempting to
208      * reset the stream may fail.
209      *
210      * @exception IOException If the stream does not support mark(),
211      * or if some other I/O error occurs
212      */

213     public void mark(int readAheadLimit) throws IOException JavaDoc {
214     throw new IOException JavaDoc("mark() not supported");
215     }
216
217     /**
218      * Reset the stream. If the stream has been marked, then attempt to
219      * reposition it at the mark. If the stream has not been marked, then
220      * attempt to reset it in some way appropriate to the particular stream,
221      * for example by repositioning it to its starting point. Not all
222      * character-input streams support the reset() operation, and some support
223      * reset() without supporting mark().
224      *
225      * @exception IOException If the stream has not been marked,
226      * or if the mark has been invalidated,
227      * or if the stream does not support reset(),
228      * or if some other I/O error occurs
229      */

230     public void reset() throws IOException JavaDoc {
231     throw new IOException JavaDoc("reset() not supported");
232     }
233
234     /**
235      * Close the stream. Once a stream has been closed, further read(),
236      * ready(), mark(), or reset() invocations will throw an IOException.
237      * Closing a previously-closed stream, however, has no effect.
238      *
239      * @exception IOException If an I/O error occurs
240      */

241      abstract public void close() throws IOException JavaDoc;
242
243 }
244
Popular Tags