< prev index next >

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

Print this page
@@ -24,10 +24,12 @@
   */
  
  package java.io;
  
  
+ import jdk.internal.misc.InternalLock;
+ 
  import java.util.Objects;
  
  /**
   * Abstract class for writing to character streams.  The only methods that a
   * subclass must implement are write(char[], int, int), flush(), and close().

@@ -48,20 +50,10 @@
   * @since       1.1
   */
  
  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 static final int WRITE_BUFFER_SIZE = 1024;
- 
      /**
       * Returns a new {@code Writer} which discards all characters.  The
       * returned stream is initially open.  The stream is closed by calling
       * the {@code close()} method.  Subsequent calls to {@code close()} have
       * no effect.

@@ -157,25 +149,32 @@
      /**
       * Creates a new character-stream writer whose critical sections will
       * synchronize on the writer itself.
       */
      protected Writer() {
-         this.lock = this;
+         // use InternalLock for trusted classes
+         Class<?> clazz = getClass();
+         if (clazz == OutputStreamWriter.class
+                 || clazz == BufferedWriter.class
+                 || clazz == FileWriter.class
+                 || clazz == PrintWriter.class
+                 || clazz == sun.nio.cs.StreamEncoder.class) {
+             this.lock = new InternalLock();
+         } else {
+             this.lock = this;
+         }
      }
  
      /**
       * Creates a new character-stream writer whose critical sections will
       * synchronize on the given object.
       *
       * @param  lock
       *         Object to synchronize on
       */
      protected Writer(Object lock) {
-         if (lock == null) {
-             throw new NullPointerException();
-         }
-         this.lock = lock;
+         this.lock = Objects.requireNonNull(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

@@ -189,17 +188,13 @@
       *
       * @throws  IOException
       *          If an I/O error occurs
       */
      public void write(int c) throws IOException {
-         synchronized (lock) {
-             if (writeBuffer == null){
-                 writeBuffer = new char[WRITE_BUFFER_SIZE];
-             }
-             writeBuffer[0] = (char) c;
-             write(writeBuffer, 0, 1);
-         }
+         var writeBuffer = new char[1];
+         writeBuffer[0] = (char) c;
+         write(writeBuffer, 0, 1);
      }
  
      /**
       * Writes an array of characters.
       *

@@ -274,23 +269,14 @@
       *
       * @throws  IOException
       *          If an I/O error occurs
       */
      public void write(String str, int off, int len) throws IOException {
-         synchronized (lock) {
-             char cbuf[];
-             if (len <= WRITE_BUFFER_SIZE) {
-                 if (writeBuffer == null) {
-                     writeBuffer = new char[WRITE_BUFFER_SIZE];
-                 }
-                 cbuf = writeBuffer;
-             } else {    // Don't permanently allocate very large buffers.
-                 cbuf = new char[len];
-             }
-             str.getChars(off, (off + len), cbuf, 0);
-             write(cbuf, 0, len);
-         }
+         Objects.checkFromIndexSize(off, len, str.length());
+         char cbuf[] = new char[len];
+         str.getChars(off, (off + len), cbuf, 0);
+         write(cbuf, 0, len);
      }
  
      /**
       * Appends the specified character sequence to this writer.
       *
< prev index next >