< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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 /**
  30  * Abstract class for writing to character streams.  The only methods that a
  31  * subclass must implement are write(char[], int, int), flush(), and close().
  32  * Most subclasses, however, will override some of the methods defined here in
  33  * order to provide higher efficiency, additional functionality, or both.
  34  *
  35  * @see   BufferedWriter
  36  * @see   CharArrayWriter
  37  * @see   FilterWriter
  38  * @see   OutputStreamWriter
  39  * @see   FileWriter
  40  * @see   PipedWriter
  41  * @see   PrintWriter
  42  * @see   StringWriter
  43  * @see Reader
  44  *
  45  * @author      Mark Reinhold
  46  * @since       1.1
  47  */
  48 
  49 public abstract class Writer implements Appendable, Closeable, Flushable {
  50 
  51     /**
  52      * Temporary buffer used to hold writes of strings and single characters
  53      */
  54     private char[] writeBuffer;
  55 
  56     /**
  57      * Size of writeBuffer, must be >= 1
  58      */
  59     private static final int WRITE_BUFFER_SIZE = 1024;
  60 
  61     /**




















































































  62      * The object used to synchronize operations on this stream.  For
  63      * efficiency, a character-stream object may use an object other than
  64      * itself to protect critical sections.  A subclass should therefore use
  65      * the object in this field rather than {@code this} or a synchronized
  66      * method.
  67      */
  68     protected Object lock;
  69 
  70     /**
  71      * Creates a new character-stream writer whose critical sections will
  72      * synchronize on the writer itself.
  73      */
  74     protected Writer() {
  75         this.lock = this;
  76     }
  77 
  78     /**
  79      * Creates a new character-stream writer whose critical sections will
  80      * synchronize on the given object.
  81      *


   1 /*
   2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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.util.Objects;
  30 
  31 /**
  32  * Abstract class for writing to character streams.  The only methods that a
  33  * subclass must implement are write(char[], int, int), flush(), and close().
  34  * Most subclasses, however, will override some of the methods defined here in
  35  * order to provide higher efficiency, additional functionality, or both.
  36  *
  37  * @see   BufferedWriter
  38  * @see   CharArrayWriter
  39  * @see   FilterWriter
  40  * @see   OutputStreamWriter
  41  * @see   FileWriter
  42  * @see   PipedWriter
  43  * @see   PrintWriter
  44  * @see   StringWriter
  45  * @see Reader
  46  *
  47  * @author      Mark Reinhold
  48  * @since       1.1
  49  */
  50 
  51 public abstract class Writer implements Appendable, Closeable, Flushable {
  52 
  53     /**
  54      * Temporary buffer used to hold writes of strings and single characters
  55      */
  56     private char[] writeBuffer;
  57 
  58     /**
  59      * Size of writeBuffer, must be >= 1
  60      */
  61     private static final int WRITE_BUFFER_SIZE = 1024;
  62 
  63     /**
  64      * Returns a new {@code Writer} which discards all characters.  The
  65      * returned stream is initially open.  The stream is closed by calling
  66      * the {@code close()} method.  Subsequent calls to {@code close()} have
  67      * no effect.
  68      *
  69      * <p> While the stream is open, the {@code append(char)}, {@code
  70      * append(CharSequence)}, {@code append(CharSequence, int, int)},
  71      * {@code flush()}, {@code write(int)}, {@code write(char[])}, and
  72      * {@code write(char[], int, int)} methods do nothing. After the stream
  73      * has been closed, these methods all throw {@code IOException}.
  74      *
  75      * <p> The {@link #lock object} used to synchronize operations on the
  76      * returned {@code Writer} is not specified.
  77      *
  78      * @return a {@code Writer} which discards all characters
  79      *
  80      * @since 11
  81      */
  82     public static Writer nullWriter() {
  83         return new Writer() {
  84             private volatile boolean closed;
  85 
  86             private void ensureOpen() throws IOException {
  87                 if (closed) {
  88                     throw new IOException("Stream closed");
  89                 }
  90             }
  91 
  92             @Override
  93             public Writer append(char c) throws IOException {
  94                 ensureOpen();
  95                 return this;
  96             }
  97 
  98             @Override
  99             public Writer append(CharSequence csq) throws IOException {
 100                 Objects.requireNonNull(csq);
 101                 ensureOpen();
 102                 return this;
 103             }
 104 
 105             @Override
 106             public Writer append(CharSequence csq, int start, int end) throws IOException {
 107                 Objects.requireNonNull(csq);
 108                 ensureOpen();
 109                 return this;
 110             }
 111 
 112             @Override
 113             public void write(int c) throws IOException {
 114                 ensureOpen();
 115             }
 116 
 117             @Override
 118             public void write(char[] cbuf, int off, int len) throws IOException {
 119                 Objects.checkFromIndexSize(off, len, cbuf.length);
 120                 ensureOpen();
 121             }
 122 
 123             @Override
 124             public void write(String str) throws IOException {
 125                 Objects.requireNonNull(str);
 126                 ensureOpen();
 127             }
 128 
 129             @Override
 130             public void write(String str, int off, int len) throws IOException {
 131                 Objects.checkFromIndexSize(off, len, str.length());
 132                 ensureOpen();
 133             }
 134 
 135             @Override
 136             public void flush() throws IOException {
 137                 ensureOpen();
 138             }
 139 
 140             @Override
 141             public void close() throws IOException {
 142                 closed = true;
 143             }
 144         };
 145     }
 146 
 147     /**
 148      * The object used to synchronize operations on this stream.  For
 149      * efficiency, a character-stream object may use an object other than
 150      * itself to protect critical sections.  A subclass should therefore use
 151      * the object in this field rather than {@code this} or a synchronized
 152      * method.
 153      */
 154     protected Object lock;
 155 
 156     /**
 157      * Creates a new character-stream writer whose critical sections will
 158      * synchronize on the writer itself.
 159      */
 160     protected Writer() {
 161         this.lock = this;
 162     }
 163 
 164     /**
 165      * Creates a new character-stream writer whose critical sections will
 166      * synchronize on the given object.
 167      *


< prev index next >