< prev index next >

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

Print this page




  50  * </pre>
  51  *
  52  * will buffer the PrintWriter's output to the file.  Without buffering, each
  53  * invocation of a print() method would cause characters to be converted into
  54  * bytes that would then be written immediately to the file, which can be very
  55  * inefficient.
  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


 149      * Writes a portion of an array of characters.
 150      *
 151      * <p> Ordinarily this method stores characters from the given array into
 152      * this stream's buffer, flushing the buffer to the underlying stream as
 153      * needed.  If the requested length is at least as large as the buffer,
 154      * however, then this method will flush the buffer and write the characters
 155      * directly to the underlying stream.  Thus redundant
 156      * {@code BufferedWriter}s will not copy data unnecessarily.
 157      *
 158      * @param  cbuf  A character array
 159      * @param  off   Offset from which to start reading characters
 160      * @param  len   Number of characters to write
 161      *
 162      * @throws  IndexOutOfBoundsException
 163      *          If {@code off} is negative, or {@code len} is negative,
 164      *          or {@code off + len} is negative or greater than the length
 165      *          of the given array
 166      *
 167      * @throws  IOException  If an I/O error occurs
 168      */
 169     public void write(char cbuf[], int off, int len) throws IOException {
 170         synchronized (lock) {
 171             ensureOpen();
 172             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
 173                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
 174                 throw new IndexOutOfBoundsException();
 175             } else if (len == 0) {
 176                 return;
 177             }
 178 
 179             if (len >= nChars) {
 180                 /* If the request length exceeds the size of the output buffer,
 181                    flush the buffer and then write the data directly.  In this
 182                    way buffered streams will cascade harmlessly. */
 183                 flushBuffer();
 184                 out.write(cbuf, off, len);
 185                 return;
 186             }
 187 
 188             int b = off, t = off + len;
 189             while (b < t) {




  50  * </pre>
  51  *
  52  * will buffer the PrintWriter's output to the file.  Without buffering, each
  53  * invocation of a print() method would cause characters to be converted into
  54  * bytes that would then be written immediately to the file, which can be very
  55  * inefficient.
  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


 149      * Writes a portion of an array of characters.
 150      *
 151      * <p> Ordinarily this method stores characters from the given array into
 152      * this stream's buffer, flushing the buffer to the underlying stream as
 153      * needed.  If the requested length is at least as large as the buffer,
 154      * however, then this method will flush the buffer and write the characters
 155      * directly to the underlying stream.  Thus redundant
 156      * {@code BufferedWriter}s will not copy data unnecessarily.
 157      *
 158      * @param  cbuf  A character array
 159      * @param  off   Offset from which to start reading characters
 160      * @param  len   Number of characters to write
 161      *
 162      * @throws  IndexOutOfBoundsException
 163      *          If {@code off} is negative, or {@code len} is negative,
 164      *          or {@code off + len} is negative or greater than the length
 165      *          of the given array
 166      *
 167      * @throws  IOException  If an I/O error occurs
 168      */
 169     public void write(char[] cbuf, int off, int len) throws IOException {
 170         synchronized (lock) {
 171             ensureOpen();
 172             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
 173                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
 174                 throw new IndexOutOfBoundsException();
 175             } else if (len == 0) {
 176                 return;
 177             }
 178 
 179             if (len >= nChars) {
 180                 /* If the request length exceeds the size of the output buffer,
 181                    flush the buffer and then write the data directly.  In this
 182                    way buffered streams will cascade harmlessly. */
 183                 flushBuffer();
 184                 out.write(cbuf, off, len);
 185                 return;
 186             }
 187 
 188             int b = off, t = off + len;
 189             while (b < t) {


< prev index next >