< prev index next >

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

Print this page




 192      */
 193     public void write(int c) throws IOException {
 194         synchronized (lock) {
 195             if (writeBuffer == null){
 196                 writeBuffer = new char[WRITE_BUFFER_SIZE];
 197             }
 198             writeBuffer[0] = (char) c;
 199             write(writeBuffer, 0, 1);
 200         }
 201     }
 202 
 203     /**
 204      * Writes an array of characters.
 205      *
 206      * @param  cbuf
 207      *         Array of characters to be written
 208      *
 209      * @throws  IOException
 210      *          If an I/O error occurs
 211      */
 212     public void write(char cbuf[]) throws IOException {
 213         write(cbuf, 0, cbuf.length);
 214     }
 215 
 216     /**
 217      * Writes a portion of an array of characters.
 218      *
 219      * @param  cbuf
 220      *         Array of characters
 221      *
 222      * @param  off
 223      *         Offset from which to start writing characters
 224      *
 225      * @param  len
 226      *         Number of characters to write
 227      *
 228      * @throws  IndexOutOfBoundsException
 229      *          Implementations should throw this exception
 230      *          if {@code off} is negative, or {@code len} is negative,
 231      *          or {@code off + len} is negative or greater than the length
 232      *          of the given array
 233      *
 234      * @throws  IOException
 235      *          If an I/O error occurs
 236      */
 237     public abstract void write(char cbuf[], int off, int len) throws IOException;
 238 
 239     /**
 240      * Writes a string.
 241      *
 242      * @param  str
 243      *         String to be written
 244      *
 245      * @throws  IOException
 246      *          If an I/O error occurs
 247      */
 248     public void write(String str) throws IOException {
 249         write(str, 0, str.length());
 250     }
 251 
 252     /**
 253      * Writes a portion of a string.
 254      *
 255      * @implSpec
 256      * The implementation in this class throws an
 257      * {@code IndexOutOfBoundsException} for the indicated conditions;


 260      * @param  str
 261      *         A String
 262      *
 263      * @param  off
 264      *         Offset from which to start writing characters
 265      *
 266      * @param  len
 267      *         Number of characters to write
 268      *
 269      * @throws  IndexOutOfBoundsException
 270      *          Implementations should throw this exception
 271      *          if {@code off} is negative, or {@code len} is negative,
 272      *          or {@code off + len} is negative or greater than the length
 273      *          of the given string
 274      *
 275      * @throws  IOException
 276      *          If an I/O error occurs
 277      */
 278     public void write(String str, int off, int len) throws IOException {
 279         synchronized (lock) {
 280             char cbuf[];
 281             if (len <= WRITE_BUFFER_SIZE) {
 282                 if (writeBuffer == null) {
 283                     writeBuffer = new char[WRITE_BUFFER_SIZE];
 284                 }
 285                 cbuf = writeBuffer;
 286             } else {    // Don't permanently allocate very large buffers.
 287                 cbuf = new char[len];
 288             }
 289             str.getChars(off, (off + len), cbuf, 0);
 290             write(cbuf, 0, len);
 291         }
 292     }
 293 
 294     /**
 295      * Appends the specified character sequence to this writer.
 296      *
 297      * <p> An invocation of this method of the form {@code out.append(csq)}
 298      * behaves in exactly the same way as the invocation
 299      *
 300      * <pre>




 192      */
 193     public void write(int c) throws IOException {
 194         synchronized (lock) {
 195             if (writeBuffer == null){
 196                 writeBuffer = new char[WRITE_BUFFER_SIZE];
 197             }
 198             writeBuffer[0] = (char) c;
 199             write(writeBuffer, 0, 1);
 200         }
 201     }
 202 
 203     /**
 204      * Writes an array of characters.
 205      *
 206      * @param  cbuf
 207      *         Array of characters to be written
 208      *
 209      * @throws  IOException
 210      *          If an I/O error occurs
 211      */
 212     public void write(char[] cbuf) throws IOException {
 213         write(cbuf, 0, cbuf.length);
 214     }
 215 
 216     /**
 217      * Writes a portion of an array of characters.
 218      *
 219      * @param  cbuf
 220      *         Array of characters
 221      *
 222      * @param  off
 223      *         Offset from which to start writing characters
 224      *
 225      * @param  len
 226      *         Number of characters to write
 227      *
 228      * @throws  IndexOutOfBoundsException
 229      *          Implementations should throw this exception
 230      *          if {@code off} is negative, or {@code len} is negative,
 231      *          or {@code off + len} is negative or greater than the length
 232      *          of the given array
 233      *
 234      * @throws  IOException
 235      *          If an I/O error occurs
 236      */
 237     public abstract void write(char[] cbuf, int off, int len) throws IOException;
 238 
 239     /**
 240      * Writes a string.
 241      *
 242      * @param  str
 243      *         String to be written
 244      *
 245      * @throws  IOException
 246      *          If an I/O error occurs
 247      */
 248     public void write(String str) throws IOException {
 249         write(str, 0, str.length());
 250     }
 251 
 252     /**
 253      * Writes a portion of a string.
 254      *
 255      * @implSpec
 256      * The implementation in this class throws an
 257      * {@code IndexOutOfBoundsException} for the indicated conditions;


 260      * @param  str
 261      *         A String
 262      *
 263      * @param  off
 264      *         Offset from which to start writing characters
 265      *
 266      * @param  len
 267      *         Number of characters to write
 268      *
 269      * @throws  IndexOutOfBoundsException
 270      *          Implementations should throw this exception
 271      *          if {@code off} is negative, or {@code len} is negative,
 272      *          or {@code off + len} is negative or greater than the length
 273      *          of the given string
 274      *
 275      * @throws  IOException
 276      *          If an I/O error occurs
 277      */
 278     public void write(String str, int off, int len) throws IOException {
 279         synchronized (lock) {
 280             char[] cbuf;
 281             if (len <= WRITE_BUFFER_SIZE) {
 282                 if (writeBuffer == null) {
 283                     writeBuffer = new char[WRITE_BUFFER_SIZE];
 284                 }
 285                 cbuf = writeBuffer;
 286             } else {    // Don't permanently allocate very large buffers.
 287                 cbuf = new char[len];
 288             }
 289             str.getChars(off, (off + len), cbuf, 0);
 290             write(cbuf, 0, len);
 291         }
 292     }
 293 
 294     /**
 295      * Appends the specified character sequence to this writer.
 296      *
 297      * <p> An invocation of this method of the form {@code out.append(csq)}
 298      * behaves in exactly the same way as the invocation
 299      *
 300      * <pre>


< prev index next >