< prev index next >

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

Print this page




  27 
  28 import java.util.Arrays;
  29 
  30 /**
  31  * This class implements a character buffer that can be used as an Writer.
  32  * The buffer automatically grows when data is written to the stream.  The data
  33  * can be retrieved using toCharArray() and toString().
  34  * <P>
  35  * Note: Invoking close() on this class has no effect, and methods
  36  * of this class can be called after the stream has closed
  37  * without generating an IOException.
  38  *
  39  * @author      Herb Jellinek
  40  * @since       1.1
  41  */
  42 public
  43 class CharArrayWriter extends Writer {
  44     /**
  45      * The buffer where data is stored.
  46      */
  47     protected char buf[];
  48 
  49     /**
  50      * The number of chars in the buffer.
  51      */
  52     protected int count;
  53 
  54     /**
  55      * Creates a new CharArrayWriter.
  56      */
  57     public CharArrayWriter() {
  58         this(32);
  59     }
  60 
  61     /**
  62      * Creates a new CharArrayWriter with the specified initial size.
  63      *
  64      * @param initialSize  an int specifying the initial buffer size.
  65      * @exception IllegalArgumentException if initialSize is negative
  66      */
  67     public CharArrayWriter(int initialSize) {


  80             int newcount = count + 1;
  81             if (newcount > buf.length) {
  82                 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
  83             }
  84             buf[count] = (char)c;
  85             count = newcount;
  86         }
  87     }
  88 
  89     /**
  90      * Writes characters to the buffer.
  91      * @param c the data to be written
  92      * @param off       the start offset in the data
  93      * @param len       the number of chars that are written
  94      *
  95      * @throws  IndexOutOfBoundsException
  96      *          If {@code off} is negative, or {@code len} is negative,
  97      *          or {@code off + len} is negative or greater than the length
  98      *          of the given array
  99      */
 100     public void write(char c[], int off, int len) {
 101         if ((off < 0) || (off > c.length) || (len < 0) ||
 102             ((off + len) > c.length) || ((off + len) < 0)) {
 103             throw new IndexOutOfBoundsException();
 104         } else if (len == 0) {
 105             return;
 106         }
 107         synchronized (lock) {
 108             int newcount = count + len;
 109             if (newcount > buf.length) {
 110                 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
 111             }
 112             System.arraycopy(c, off, buf, count, len);
 113             count = newcount;
 114         }
 115     }
 116 
 117     /**
 118      * Write a portion of a string to the buffer.
 119      * @param  str  String to be written from
 120      * @param  off  Offset from which to start reading characters




  27 
  28 import java.util.Arrays;
  29 
  30 /**
  31  * This class implements a character buffer that can be used as an Writer.
  32  * The buffer automatically grows when data is written to the stream.  The data
  33  * can be retrieved using toCharArray() and toString().
  34  * <P>
  35  * Note: Invoking close() on this class has no effect, and methods
  36  * of this class can be called after the stream has closed
  37  * without generating an IOException.
  38  *
  39  * @author      Herb Jellinek
  40  * @since       1.1
  41  */
  42 public
  43 class CharArrayWriter extends Writer {
  44     /**
  45      * The buffer where data is stored.
  46      */
  47     protected char[] buf;
  48 
  49     /**
  50      * The number of chars in the buffer.
  51      */
  52     protected int count;
  53 
  54     /**
  55      * Creates a new CharArrayWriter.
  56      */
  57     public CharArrayWriter() {
  58         this(32);
  59     }
  60 
  61     /**
  62      * Creates a new CharArrayWriter with the specified initial size.
  63      *
  64      * @param initialSize  an int specifying the initial buffer size.
  65      * @exception IllegalArgumentException if initialSize is negative
  66      */
  67     public CharArrayWriter(int initialSize) {


  80             int newcount = count + 1;
  81             if (newcount > buf.length) {
  82                 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
  83             }
  84             buf[count] = (char)c;
  85             count = newcount;
  86         }
  87     }
  88 
  89     /**
  90      * Writes characters to the buffer.
  91      * @param c the data to be written
  92      * @param off       the start offset in the data
  93      * @param len       the number of chars that are written
  94      *
  95      * @throws  IndexOutOfBoundsException
  96      *          If {@code off} is negative, or {@code len} is negative,
  97      *          or {@code off + len} is negative or greater than the length
  98      *          of the given array
  99      */
 100     public void write(char[] c, int off, int len) {
 101         if ((off < 0) || (off > c.length) || (len < 0) ||
 102             ((off + len) > c.length) || ((off + len) < 0)) {
 103             throw new IndexOutOfBoundsException();
 104         } else if (len == 0) {
 105             return;
 106         }
 107         synchronized (lock) {
 108             int newcount = count + len;
 109             if (newcount > buf.length) {
 110                 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
 111             }
 112             System.arraycopy(c, off, buf, count, len);
 113             count = newcount;
 114         }
 115     }
 116 
 117     /**
 118      * Write a portion of a string to the buffer.
 119      * @param  str  String to be written from
 120      * @param  off  Offset from which to start reading characters


< prev index next >