Abstract class for reading character streams. The only methods that a
subclass must implement are read(char[], int, int) and close(). Most
subclasses, however, will override some of the methods defined here in order
to provide higher efficiency, additional functionality, or both.
/**
* The object used to synchronize operations on this stream.
*/protected Object lock;/** Maximum skip-buffer size */privatestaticfinalint maxSkipBufferSize =8192;/** Skip buffer, null until allocated */privatechar skipBuffer[]= null;
构造函数
/**
* Creates a new character-stream reader whose critical sections will
* synchronize on the reader itself.
*/protectedReader(){this.lock =this;}/**
* Creates a new character-stream reader whose critical sections will
* synchronize on the given object.
* @param lock The Object to synchronize on.
*/protectedReader(Object lock){if(lock == null){thrownewNullPointerException();}this.lock = lock;}
成员方法
/**
* Attempts to read characters into the specified character buffer.
*/publicintread(java.nio.CharBuffer target)throws IOException {int len = target.remaining();char[] cbuf =newchar[len];int n =read(cbuf,0, len);if(n >0)
target.put(cbuf,0, n);return n;}/**
* Reads a single character.
*/publicintread()throws IOException {char cb[]=newchar[1];if(read(cb,0,1)==-1)return-1;elsereturn cb[0];}/**
* Reads characters into an array.
*/publicintread(char cbuf[])throws IOException {returnread(cbuf,0, cbuf.length);}/**
* Reads characters into a portion of an array.
*/abstractpublicintread(char cbuf[],int off,int len)throws IOException;publiclongskip(long n)throws IOException {if(n <0L)thrownewIllegalArgumentException("skip value is negative");int nn =(int) Math.min(n, maxSkipBufferSize);synchronized(lock){if((skipBuffer == null)||(skipBuffer.length < nn))
skipBuffer =newchar[nn];long r = n;while(r >0){int nc =read(skipBuffer,0,(int)Math.min(r, nn));if(nc ==-1)break;
r -= nc;}return n - r;}}/**
* Tells whether this stream is ready to be read.
*/publicbooleanready()throws IOException {returnfalse;}/**
* Tells whether this stream supports the mark() operation.
*/publicbooleanmarkSupported(){returnfalse;}/**
* Marks the present position in the stream.
*/publicvoidmark(int readAheadLimit)throws IOException {thrownewIOException("mark() not supported");}/**
* Resets the stream.
*/publicvoidreset()throws IOException {thrownewIOException("reset() not supported");}/**
* Closes the stream and releases any system resources associated with
* it.
*/abstractpublicvoidclose()throws IOException;