天天看點

Reader和Writer

Reader:

Reader比InputStream對流的操作加了鎖,見源碼的方法abstract public int read(char cbuf[], int off, int len),其中鎖是protected Object lock。

package java.io;
public abstract class Reader implements Readable, Closeable {

    /** The object used to synchronize operations on this stream.  For efficiency, a character-stream object may use an object other than
     * itself to protect critical sections.  A subclass should therefore use the object in this field rather than this or a synchronized method.*/
    protected Object lock;

    /*** Creates a new character-stream reader whose critical sections will synchronize on the reader itself.*/
    protected Reader() {
	this.lock = this;
    }

    /** Creates a new character-stream reader whose critical sections will synchronize on the given object. */
    protected Reader(Object lock) {
	if (lock == null) {
	    throw new NullPointerException();
	}
	this.lock = lock;
    }

    /** Attempts to read characters into the specified character buffer.
     * The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or 
     rewinding of the buffer is performed. @return The number of characters added to the buffer, or     -1 if this source of characters is at its end
     * @since 1.5
     */
    public int read(java.nio.CharBuffer target) throws IOException {
        //...
    }

    /** Reads a single character.  This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.
     * @return     The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has   been reached
     */
    public int read() throws IOException {
	//...
    }

    /** Reads characters into an array.  This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
     * @return      The number of characters read, or -1  if the end of the stream   has been reached */
    public int read(char cbuf[]) throws IOException {
	return read(cbuf, 0, cbuf.length);
    }

    /** Reads characters into a portion of an array.  This method will block until some input is available, an I/O error occurs, or the end of the
     * stream is reached.
     * @return     The number of characters read, or -1 if the end of the  stream has been reached */
    abstract public int read(char cbuf[], int off, int len) throws IOException;

    /** Maximum skip-buffer size */
    private static final int maxSkipBufferSize = 8192;

    /** Skip buffer, null until allocated */
    private char skipBuffer[] = null;

    /** Skips characters.  This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.
     * @return    The number of characters actually skipped
     */
    public long skip(long n) throws IOException {
	//...
    }

    /**Tells whether this stream is ready to be read.
     * @return True if the next read() is guaranteed not to block for input, false otherwise.  Note that returning false does not 
guarantee that the next read will block.
     */
    public boolean ready() throws IOException {
	return false;
    }

    /**Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override 
     * this method.
     * @return true if and only if this stream supports the mark operation.
     */
    public boolean markSupported() {
	return false;
    }

    /** Marks the present position in the stream.  Subsequent calls to reset() will attempt to reposition the stream to this point.  Not all
     * character-input streams support the mark() operation. */
    public void mark(int readAheadLimit) throws IOException {
	throw new IOException("mark() not supported");
    }

    /** Resets the stream.  If the stream has been marked, then attempt to reposition it at the mark.  If the stream has not been marked, then
     * attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point.  Not all
     * character-input streams support the reset() operation, and some support reset() without supporting mark().
     */
    public void reset() throws IOException {
	throw new IOException("reset() not supported");
    }

    /** Closes the stream and releases any system resources associated with it.  Once the stream has been closed, further read(), ready(),
     * mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
     */
     abstract public void close() throws IOException;

}
           

Writer:

Writer比OutputStream對流的操作加了鎖,見源碼的方法write(String str, int off, int len),其中鎖是protected Object lock。

package java.io;
public abstract class Writer implements Appendable, Closeable, Flushable {

    /**Temporary buffer used to hold writes of strings and single characters */
    private char[] writeBuffer;

    /**Size of writeBuffer, must be >= 1*/
    private final int writeBufferSize = 1024;

    /** The object used to synchronize operations on this stream.  For efficiency, a character-stream object may use an object other than
     * itself to protect critical sections.  A subclass should therefore use the object in this field rather than <tt>this</tt> or a synchronized method.
     */
    protected Object lock;

    /** Creates a new character-stream writer whose critical sections will synchronize on the writer itself.*/
    protected Writer() {
    this.lock = this;
    }

    /** Creates a new character-stream writer whose critical sections will synchronize on the given object.*/
    protected Writer(Object lock) {
    if (lock == null) {
        throw new NullPointerException();
    }
    this.lock = lock;
    }

    /** Writes a single character.  The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits
     * are ignored.*/
    public void write(int c) throws IOException {
    synchronized (lock) {
       //...
    }
    }

    /** Writes an array of characters.*/
    public void write(char cbuf[]) throws IOException {
    write(cbuf, 0, cbuf.length);
    }

    /**Writes a portion of an array of characters. */
    abstract public void write(char cbuf[], int off, int len) throws IOException;

    /** Writes a string.*/
    public void write(String str) throws IOException {
    write(str, 0, str.length());
    }

    /** Writes a portion of a string.*/
    public void write(String str, int off, int len) throws IOException {
     synchronized (lock) {
       ///...
      }
    }

    /** Appends the specified character sequence to this writer.
     * @return  This writer
     * @since  1.5
     */
    public Writer append(CharSequence csq) throws IOException {
        //...
    }

    /** Appends a subsequence of the specified character sequence to this writer.
     * @return  This writer
     * @since  1.5
     */
    public Writer append(CharSequence csq, int start, int end) throws IOException {
    CharSequence cs = (csq == null ? "null" : csq);
    write(cs.subSequence(start, end).toString());
     return this; 
    }

    /** Appends the specified character to this writer.
     * @return  This writer
     * @since 1.5
     */
    public Writer append(char c) throws IOException {
    write(c);
    return this;
    }

    /** Flushes the stream.  If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their
     * intended destination.  Then, if that destination is another character or byte stream, flush it.  Thus one flush() invocation will flush all the
     * buffers in a chain of Writers and OutputStreams.  */
    abstract public void flush() throws IOException;

    /** Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be
     * thrown. Closing a previously closed stream has no effect. */
    abstract public void close() throws IOException;

}


           

額外篇:Reader、Writer的UML和使用例子,點選打開連結