001 /* Reader.java -- base class of classes that read input as a stream of chars 002 Copyright (C) 1998, 1999, 2000, 2003, 2004, 2005 Free Software Foundation 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath 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 for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.io; 039 040 import java.nio.CharBuffer; 041 042 /* Written using "Java Class Libraries", 2nd edition, plus online 043 * API docs for JDK 1.2 beta from http://www.javasoft.com. 044 * Status: Believed complete and correct. 045 */ 046 047 /** 048 * This abstract class forms the base of the hierarchy of classes that read 049 * input as a stream of characters. It provides a common set of methods for 050 * reading characters from streams. Subclasses implement and extend these 051 * methods to read characters from a particular input source such as a file 052 * or network connection. 053 * 054 * @author Per Bothner (bothner@cygnus.com) 055 * @date April 21, 1998. 056 * @author Aaron M. Renn (arenn@urbanophile.com) 057 */ 058 public abstract class Reader implements Closeable, Readable 059 { 060 /** 061 * This is the <code>Object</code> used for synchronizing critical code 062 * sections. Subclasses should use this variable instead of a 063 * synchronized method or an explicit synchronization on <code>this</code> 064 */ 065 protected Object lock; 066 067 /** 068 * Unitializes a <code>Reader</code> that will use the object 069 * itself for synchronization of critical code sections. 070 */ 071 protected Reader() 072 { 073 this.lock = this; 074 } 075 076 /** 077 * Initializes a <code>Reader</code> that will use the specified 078 * <code>Object</code> for synchronization of critical code sections. 079 * 080 * @param lock The <code>Object</code> to use for synchronization 081 */ 082 protected Reader(Object lock) 083 { 084 this.lock = lock; 085 } 086 087 /** 088 * Read chars from a stream and stores them into a caller 089 * supplied buffer. It starts storing the data at index <code>offset</code> 090 * into the buffer and attempts to read <code>len</code> chars. This method 091 * can return before reading the number of chars requested. The actual 092 * number of chars read is returned as an int. A -1 is returned to indicate 093 * the end of the stream. 094 * <p> 095 * This method will block until some data can be read. 096 * <p> 097 * This method operates by calling the single char <code>read()</code> method 098 * in a loop until the desired number of chars are read. The read loop 099 * stops short if the end of the stream is encountered or if an IOException 100 * is encountered on any read operation except the first. If the first 101 * attempt to read a chars fails, the IOException is allowed to propagate 102 * upward. And subsequent IOException is caught and treated identically 103 * to an end of stream condition. Subclasses can (and should if possible) 104 * override this method to provide a more efficient implementation. 105 * 106 * @param buf The array into which the chars read should be stored 107 * @param offset The offset into the array to start storing chars 108 * @param count The requested number of chars to read 109 * 110 * @return The actual number of chars read, or -1 if end of stream. 111 * 112 * @exception IOException If an error occurs. 113 */ 114 public abstract int read(char buf[], int offset, int count) 115 throws IOException; 116 117 /** 118 * Reads chars from a stream and stores them into a caller 119 * supplied buffer. This method attempts to completely fill the buffer, 120 * but can return before doing so. The actual number of chars read is 121 * returned as an int. A -1 is returned to indicate the end of the stream. 122 * <p> 123 * This method will block until some data can be read. 124 * <p> 125 * This method operates by calling an overloaded read method like so: 126 * <code>read(buf, 0, buf.length)</code> 127 * 128 * @param buf The buffer into which the chars read will be stored. 129 * 130 * @return The number of chars read or -1 if end of stream. 131 * 132 * @exception IOException If an error occurs. 133 */ 134 public int read(char buf[]) throws IOException 135 { 136 return read(buf, 0, buf.length); 137 } 138 139 /** 140 * Reads an char from the input stream and returns it 141 * as an int in the range of 0-65535. This method also will return -1 if 142 * the end of the stream has been reached. 143 * <p> 144 * This method will block until the char can be read. 145 * 146 * @return The char read or -1 if end of stream 147 * 148 * @exception IOException If an error occurs 149 */ 150 public int read() throws IOException 151 { 152 char[] buf = new char[1]; 153 int count = read(buf, 0, 1); 154 return count > 0 ? buf[0] : -1; 155 } 156 157 /** @since 1.5 */ 158 public int read(CharBuffer buffer) throws IOException 159 { 160 // We want to call put(), so we don't manipulate the CharBuffer 161 // directly. 162 int rem = buffer.remaining(); 163 char[] buf = new char[rem]; 164 int result = read(buf, 0, rem); 165 if (result != -1) 166 buffer.put(buf, 0, result); 167 return result; 168 } 169 170 /** 171 * Closes the stream. Any futher attempts to read from the 172 * stream may generate an <code>IOException</code>. 173 * 174 * @exception IOException If an error occurs 175 */ 176 public abstract void close() throws IOException; 177 178 /** 179 * Returns a boolean that indicates whether the mark/reset 180 * methods are supported in this class. Those methods can be used to 181 * remember a specific point in the stream and reset the stream to that 182 * point. 183 * <p> 184 * This method always returns <code>false</code> in this class, but 185 * subclasses can override this method to return <code>true</code> if they 186 * support mark/reset functionality. 187 * 188 * @return <code>true</code> if mark/reset functionality is supported, 189 * <code>false</code> otherwise 190 * 191 */ 192 public boolean markSupported() 193 { 194 return false; 195 } 196 197 /** 198 * Marks a position in the input to which the stream can be 199 * "reset" by calling the <code>reset()</code> method. The parameter 200 * <code>readlimit</code> is the number of chars that can be read from the 201 * stream after setting the mark before the mark becomes invalid. For 202 * example, if <code>mark()</code> is called with a read limit of 10, then 203 * when 11 chars of data are read from the stream before the 204 * <code>reset()</code> method is called, then the mark is invalid and the 205 * stream object instance is not required to remember the mark. 206 * 207 * @param readLimit The number of chars that can be read before the mark 208 * becomes invalid 209 * 210 * @exception IOException If an error occurs such as mark not being 211 * supported for this class 212 */ 213 public void mark(int readLimit) throws IOException 214 { 215 throw new IOException("mark not supported"); 216 } 217 218 /** 219 * Resets a stream to the point where the <code>mark()</code> 220 * method was called. Any chars that were read after the mark point was 221 * set will be re-read during subsequent reads. 222 * <p> 223 * This method always throws an IOException in this class, but subclasses 224 * can override this method if they provide mark/reset functionality. 225 * 226 * @exception IOException Always thrown for this class 227 */ 228 public void reset() throws IOException 229 { 230 throw new IOException("reset not supported"); 231 } 232 233 /** 234 * Determines whether or not this stream is ready to be 235 * read. If it returns <code>false</code> the stream may block if a 236 * read is attempted, but it is not guaranteed to do so. 237 * <p> 238 * This method always returns <code>false</code> in this class 239 * 240 * @return <code>true</code> if the stream is ready to be read, 241 * <code>false</code> otherwise. 242 * 243 * @exception IOException If an error occurs 244 */ 245 public boolean ready() throws IOException 246 { 247 return false; 248 } 249 250 /** 251 * Skips the specified number of chars in the stream. It 252 * returns the actual number of chars skipped, which may be less than the 253 * requested amount. 254 * <p> 255 * This method reads and discards chars into a 256 char array until the 256 * specified number of chars were skipped or until either the end of stream 257 * is reached or a read attempt returns a short count. Subclasses can 258 * override this method to provide a more efficient implementation where 259 * one exists. 260 * 261 * @param count The requested number of chars to skip 262 * 263 * @return The actual number of chars skipped. 264 * 265 * @exception IOException If an error occurs 266 */ 267 public long skip(long count) throws IOException 268 { 269 if (count <= 0) 270 return 0; 271 int bsize = count > 1024 ? 1024 : (int) count; 272 char[] buffer = new char[bsize]; 273 long todo = count; 274 synchronized (lock) 275 { 276 while (todo > 0) 277 { 278 int skipped = read(buffer, 0, bsize > todo ? (int) todo : bsize); 279 if (skipped <= 0) 280 break; 281 todo -= skipped; 282 } 283 } 284 return count - todo; 285 } 286 }