< prev index next >

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

Print this page
rev 11212 : 8068498: Remove constructor dependency on line.separator from PrintWriter and BufferedWriter


  56  *
  57  * @see PrintWriter
  58  * @see FileWriter
  59  * @see OutputStreamWriter
  60  * @see java.nio.file.Files#newBufferedWriter
  61  *
  62  * @author      Mark Reinhold
  63  * @since       1.1
  64  */
  65 
  66 public class BufferedWriter extends Writer {
  67 
  68     private Writer out;
  69 
  70     private char cb[];
  71     private int nChars, nextChar;
  72 
  73     private static int defaultCharBufferSize = 8192;
  74 
  75     /**
  76      * Line separator string.  This is the value of the line.separator
  77      * property at the moment that the stream was created.
  78      */
  79     private String lineSeparator;
  80 
  81     /**
  82      * Creates a buffered character-output stream that uses a default-sized
  83      * output buffer.
  84      *
  85      * @param  out  A Writer
  86      */
  87     public BufferedWriter(Writer out) {
  88         this(out, defaultCharBufferSize);
  89     }
  90 
  91     /**
  92      * Creates a new buffered character-output stream that uses an output
  93      * buffer of the given size.
  94      *
  95      * @param  out  A Writer
  96      * @param  sz   Output-buffer size, a positive integer
  97      *
  98      * @exception  IllegalArgumentException  If {@code sz <= 0}
  99      */
 100     public BufferedWriter(Writer out, int sz) {
 101         super(out);
 102         if (sz <= 0)
 103             throw new IllegalArgumentException("Buffer size <= 0");
 104         this.out = out;
 105         cb = new char[sz];
 106         nChars = sz;
 107         nextChar = 0;
 108 
 109         lineSeparator = java.security.AccessController.doPrivileged(
 110             new sun.security.action.GetPropertyAction("line.separator"));
 111     }
 112 
 113     /** Checks to make sure that the stream has not been closed */
 114     private void ensureOpen() throws IOException {
 115         if (out == null)
 116             throw new IOException("Stream closed");
 117     }
 118 
 119     /**
 120      * Flushes the output buffer to the underlying character stream, without
 121      * flushing the stream itself.  This method is non-private only so that it
 122      * may be invoked by PrintStream.
 123      */
 124     void flushBuffer() throws IOException {
 125         synchronized (lock) {
 126             ensureOpen();
 127             if (nextChar == 0)
 128                 return;
 129             out.write(cb, 0, nextChar);
 130             nextChar = 0;


 223             int b = off, t = off + len;
 224             while (b < t) {
 225                 int d = min(nChars - nextChar, t - b);
 226                 s.getChars(b, b + d, cb, nextChar);
 227                 b += d;
 228                 nextChar += d;
 229                 if (nextChar >= nChars)
 230                     flushBuffer();
 231             }
 232         }
 233     }
 234 
 235     /**
 236      * Writes a line separator.  The line separator string is defined by the
 237      * system property <tt>line.separator</tt>, and is not necessarily a single
 238      * newline ('\n') character.
 239      *
 240      * @exception  IOException  If an I/O error occurs
 241      */
 242     public void newLine() throws IOException {
 243         write(lineSeparator);
 244     }
 245 
 246     /**
 247      * Flushes the stream.
 248      *
 249      * @exception  IOException  If an I/O error occurs
 250      */
 251     public void flush() throws IOException {
 252         synchronized (lock) {
 253             flushBuffer();
 254             out.flush();
 255         }
 256     }
 257 
 258     @SuppressWarnings("try")
 259     public void close() throws IOException {
 260         synchronized (lock) {
 261             if (out == null) {
 262                 return;
 263             }


  56  *
  57  * @see PrintWriter
  58  * @see FileWriter
  59  * @see OutputStreamWriter
  60  * @see java.nio.file.Files#newBufferedWriter
  61  *
  62  * @author      Mark Reinhold
  63  * @since       1.1
  64  */
  65 
  66 public class BufferedWriter extends Writer {
  67 
  68     private Writer out;
  69 
  70     private char cb[];
  71     private int nChars, nextChar;
  72 
  73     private static int defaultCharBufferSize = 8192;
  74 
  75     /**






  76      * Creates a buffered character-output stream that uses a default-sized
  77      * output buffer.
  78      *
  79      * @param  out  A Writer
  80      */
  81     public BufferedWriter(Writer out) {
  82         this(out, defaultCharBufferSize);
  83     }
  84 
  85     /**
  86      * Creates a new buffered character-output stream that uses an output
  87      * buffer of the given size.
  88      *
  89      * @param  out  A Writer
  90      * @param  sz   Output-buffer size, a positive integer
  91      *
  92      * @exception  IllegalArgumentException  If {@code sz <= 0}
  93      */
  94     public BufferedWriter(Writer out, int sz) {
  95         super(out);
  96         if (sz <= 0)
  97             throw new IllegalArgumentException("Buffer size <= 0");
  98         this.out = out;
  99         cb = new char[sz];
 100         nChars = sz;
 101         nextChar = 0;



 102     }
 103 
 104     /** Checks to make sure that the stream has not been closed */
 105     private void ensureOpen() throws IOException {
 106         if (out == null)
 107             throw new IOException("Stream closed");
 108     }
 109 
 110     /**
 111      * Flushes the output buffer to the underlying character stream, without
 112      * flushing the stream itself.  This method is non-private only so that it
 113      * may be invoked by PrintStream.
 114      */
 115     void flushBuffer() throws IOException {
 116         synchronized (lock) {
 117             ensureOpen();
 118             if (nextChar == 0)
 119                 return;
 120             out.write(cb, 0, nextChar);
 121             nextChar = 0;


 214             int b = off, t = off + len;
 215             while (b < t) {
 216                 int d = min(nChars - nextChar, t - b);
 217                 s.getChars(b, b + d, cb, nextChar);
 218                 b += d;
 219                 nextChar += d;
 220                 if (nextChar >= nChars)
 221                     flushBuffer();
 222             }
 223         }
 224     }
 225 
 226     /**
 227      * Writes a line separator.  The line separator string is defined by the
 228      * system property <tt>line.separator</tt>, and is not necessarily a single
 229      * newline ('\n') character.
 230      *
 231      * @exception  IOException  If an I/O error occurs
 232      */
 233     public void newLine() throws IOException {
 234         write(System.lineSeparator());
 235     }
 236 
 237     /**
 238      * Flushes the stream.
 239      *
 240      * @exception  IOException  If an I/O error occurs
 241      */
 242     public void flush() throws IOException {
 243         synchronized (lock) {
 244             flushBuffer();
 245             out.flush();
 246         }
 247     }
 248 
 249     @SuppressWarnings("try")
 250     public void close() throws IOException {
 251         synchronized (lock) {
 252             if (out == null) {
 253                 return;
 254             }
< prev index next >