< prev index next >

src/java.base/share/classes/java/io/Reader.java

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.io;
 27 
 28 
 29 import java.nio.CharBuffer;
 30 import java.nio.ReadOnlyBufferException;
 31 import java.util.Objects;

 32 
 33 /**
 34  * Abstract class for reading character streams.  The only methods that a
 35  * subclass must implement are read(char[], int, int) and close().  Most
 36  * subclasses, however, will override some of the methods defined here in order
 37  * to provide higher efficiency, additional functionality, or both.
 38  *
 39  *
 40  * @see BufferedReader
 41  * @see   LineNumberReader
 42  * @see CharArrayReader
 43  * @see InputStreamReader
 44  * @see   FileReader
 45  * @see FilterReader
 46  * @see   PushbackReader
 47  * @see PipedReader
 48  * @see StringReader
 49  * @see Writer
 50  *
 51  * @author      Mark Reinhold

137             public void close() {
138                 closed = true;
139             }
140         };
141     }
142 
143     /**
144      * The object used to synchronize operations on this stream.  For
145      * efficiency, a character-stream object may use an object other than
146      * itself to protect critical sections.  A subclass should therefore use
147      * the object in this field rather than {@code this} or a synchronized
148      * method.
149      */
150     protected Object lock;
151 
152     /**
153      * Creates a new character-stream reader whose critical sections will
154      * synchronize on the reader itself.
155      */
156     protected Reader() {
157         this.lock = this;









158     }
159 
160     /**
161      * Creates a new character-stream reader whose critical sections will
162      * synchronize on the given object.
163      *
164      * @param lock  The Object to synchronize on.
165      */
166     protected Reader(Object lock) {
167         if (lock == null) {
168             throw new NullPointerException();
169         }
170         this.lock = lock;
171     }
172 
173     /**
174      * Attempts to read characters into the specified character buffer.
175      * The buffer is used as a repository of characters as-is: the only
176      * changes made are the results of a put operation. No flipping or
177      * rewinding of the buffer is performed.
178      *
179      * @param target the buffer to read characters into
180      * @return The number of characters added to the buffer, or
181      *         -1 if this source of characters is at its end
182      * @throws IOException if an I/O error occurs
183      * @throws NullPointerException if target is null
184      * @throws java.nio.ReadOnlyBufferException if target is a read only buffer
185      * @since 1.5
186      */
187     public int read(CharBuffer target) throws IOException {
188         if (target.isReadOnly())
189             throw new ReadOnlyBufferException();
190 

280 
281     /** Skip buffer, null until allocated */
282     private char[] skipBuffer = null;
283 
284     /**
285      * Skips characters.  This method will block until some characters are
286      * available, an I/O error occurs, or the end of the stream is reached.
287      * If the stream is already at its end before this method is invoked,
288      * then no characters are skipped and zero is returned.
289      *
290      * @param  n  The number of characters to skip
291      *
292      * @return    The number of characters actually skipped
293      *
294      * @throws     IllegalArgumentException  If {@code n} is negative.
295      * @throws     IOException  If an I/O error occurs
296      */
297     public long skip(long n) throws IOException {
298         if (n < 0L)
299             throw new IllegalArgumentException("skip value is negative");
300         int nn = (int) Math.min(n, maxSkipBufferSize);
301         synchronized (lock) {
302             if ((skipBuffer == null) || (skipBuffer.length < nn))
303                 skipBuffer = new char[nn];
304             long r = n;
305             while (r > 0) {
306                 int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
307                 if (nc == -1)
308                     break;
309                 r -= nc;
310             }
311             return n - r;
















312         }

313     }
314 
315     /**
316      * Tells whether this stream is ready to be read.
317      *
318      * @return True if the next read() is guaranteed not to block for input,
319      * false otherwise.  Note that returning false does not guarantee that the
320      * next read will block.
321      *
322      * @throws     IOException  If an I/O error occurs
323      */
324     public boolean ready() throws IOException {
325         return false;
326     }
327 
328     /**
329      * Tells whether this stream supports the mark() operation. The default
330      * implementation always returns false. Subclasses should override this
331      * method.
332      *

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.io;
 27 

 28 import java.nio.CharBuffer;
 29 import java.nio.ReadOnlyBufferException;
 30 import java.util.Objects;
 31 import jdk.internal.misc.InternalLock;
 32 
 33 /**
 34  * Abstract class for reading character streams.  The only methods that a
 35  * subclass must implement are read(char[], int, int) and close().  Most
 36  * subclasses, however, will override some of the methods defined here in order
 37  * to provide higher efficiency, additional functionality, or both.
 38  *
 39  *
 40  * @see BufferedReader
 41  * @see   LineNumberReader
 42  * @see CharArrayReader
 43  * @see InputStreamReader
 44  * @see   FileReader
 45  * @see FilterReader
 46  * @see   PushbackReader
 47  * @see PipedReader
 48  * @see StringReader
 49  * @see Writer
 50  *
 51  * @author      Mark Reinhold

137             public void close() {
138                 closed = true;
139             }
140         };
141     }
142 
143     /**
144      * The object used to synchronize operations on this stream.  For
145      * efficiency, a character-stream object may use an object other than
146      * itself to protect critical sections.  A subclass should therefore use
147      * the object in this field rather than {@code this} or a synchronized
148      * method.
149      */
150     protected Object lock;
151 
152     /**
153      * Creates a new character-stream reader whose critical sections will
154      * synchronize on the reader itself.
155      */
156     protected Reader() {
157         // use InternalLock for trusted classes
158         Class<?> clazz = getClass();
159         if (clazz == InputStreamReader.class
160             || clazz == BufferedReader.class
161             || clazz == FileReader.class
162             || clazz == sun.nio.cs.StreamDecoder.class) {
163             this.lock = new InternalLock();
164         } else {
165             this.lock = this;
166         }
167     }
168 
169     /**
170      * Creates a new character-stream reader whose critical sections will
171      * synchronize on the given object.
172      *
173      * @param lock  The Object to synchronize on.
174      */
175     protected Reader(Object lock) {
176         this.lock = Objects.requireNonNull(lock);



177     }
178 
179     /**
180      * Attempts to read characters into the specified character buffer.
181      * The buffer is used as a repository of characters as-is: the only
182      * changes made are the results of a put operation. No flipping or
183      * rewinding of the buffer is performed.
184      *
185      * @param target the buffer to read characters into
186      * @return The number of characters added to the buffer, or
187      *         -1 if this source of characters is at its end
188      * @throws IOException if an I/O error occurs
189      * @throws NullPointerException if target is null
190      * @throws java.nio.ReadOnlyBufferException if target is a read only buffer
191      * @since 1.5
192      */
193     public int read(CharBuffer target) throws IOException {
194         if (target.isReadOnly())
195             throw new ReadOnlyBufferException();
196 

286 
287     /** Skip buffer, null until allocated */
288     private char[] skipBuffer = null;
289 
290     /**
291      * Skips characters.  This method will block until some characters are
292      * available, an I/O error occurs, or the end of the stream is reached.
293      * If the stream is already at its end before this method is invoked,
294      * then no characters are skipped and zero is returned.
295      *
296      * @param  n  The number of characters to skip
297      *
298      * @return    The number of characters actually skipped
299      *
300      * @throws     IllegalArgumentException  If {@code n} is negative.
301      * @throws     IOException  If an I/O error occurs
302      */
303     public long skip(long n) throws IOException {
304         if (n < 0L)
305             throw new IllegalArgumentException("skip value is negative");
306         Object lock = this.lock;
307         if (lock instanceof InternalLock locker) {
308             locker.lock();
309             try {
310                 return lockedSkip(n);
311             } finally {
312                 locker.unlock();



313             }
314         } else {
315             synchronized (lock) {
316                 return lockedSkip(n);
317             }
318         }
319     }
320 
321     private long lockedSkip(long n) throws IOException {
322         int nn = (int) Math.min(n, maxSkipBufferSize);
323         if ((skipBuffer == null) || (skipBuffer.length < nn))
324             skipBuffer = new char[nn];
325         long r = n;
326         while (r > 0) {
327             int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
328             if (nc == -1)
329                 break;
330             r -= nc;
331         }
332         return n - r;
333     }
334 
335     /**
336      * Tells whether this stream is ready to be read.
337      *
338      * @return True if the next read() is guaranteed not to block for input,
339      * false otherwise.  Note that returning false does not guarantee that the
340      * next read will block.
341      *
342      * @throws     IOException  If an I/O error occurs
343      */
344     public boolean ready() throws IOException {
345         return false;
346     }
347 
348     /**
349      * Tells whether this stream supports the mark() operation. The default
350      * implementation always returns false. Subclasses should override this
351      * method.
352      *
< prev index next >