Ordinarily this method stores characters from the given array into
* this stream's buffer, flushing the buffer to the underlying stream as
* needed. If the requested length is at least as large as the buffer,
* however, then this method will flush the buffer and write the characters
* directly to the underlying stream. Thus redundant
* BufferedWriter
s will not copy data unnecessarily.
*
* @param cbuf A character array
* @param off Offset from which to start reading characters
* @param len Number of characters to write
*
* @exception IOException If an I/O error occurs
*/
public void write(char cbuf[], int off, int len) throws IOException {
if((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length)
|| ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if(len == 0) {
return;
}
if(len >= nChars) {
/* If the request length exceeds the size of the output buffer,
flush the buffer and then write the data directly. In this
way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(cbuf, off, len);
return;
}
int b = off, t = off + len;
while(b < t) {
int d = min(nChars - nextChar, t - b);
System.arraycopy(cbuf, b, cb, nextChar, d);
b += d;
nextChar += d;
if(nextChar >= nChars)
flushBuffer();
}
}
/**
* Writes a portion of a String.
*
*
If the value of the len parameter is negative then no * characters are written. This is contrary to the specification of this * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int) * superclass}, which requires that an {@link IndexOutOfBoundsException} be * thrown. * * @param s String to be written * @param off Offset from which to start reading characters * @param len Number of characters to be written * * @exception IOException If an I/O error occurs */ public void write(String s, int off, int len) throws IOException { int b = off, t = off + len; while(b < t) { int d = min(nChars - nextChar, t - b); s.getChars(b, b + d, cb, nextChar); b += d; nextChar += d; if(nextChar >= nChars) flushBuffer(); } } /** * Flushes the stream. * * @exception IOException If an I/O error occurs */ public void flush() throws IOException { flushBuffer(); out.flush(); } public void close() throws IOException { if(out == null) { return; } try { flushBuffer(); } finally { out.close(); out = null; cb = null; } } }