java.lang.Math
if we've run out of file
* descriptors and we're trying to print a stack trace.
*/
private final int min(int a, int b)
{
return (a < b ? a : b);
}
/**
* Write a portion of an array of characters.
*
* 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
* DiscardableBufferedWriter
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
* @throws IOException
*
*/
public final void write(char cbuf[], int off, int len)
throws IOException
{
if (bufferSize == 0)
{
writer.write(cbuf, off, len);
return;
}
if (len == 0)
{
return;
}
if (len >= bufferSize)
{
/* 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. */
if (autoFlush)
flushBuffer();
else
bufferOverflow();
writer.write(cbuf, off, len);
return;
}
int b = off, t = off + len;
while (b < t)
{
int d = min(bufferSize - nextChar, t - b);
System.arraycopy(cbuf, b, cb, nextChar, d);
b += d;
nextChar += d;
if (nextChar >= bufferSize)
if (autoFlush)
flushBuffer();
else
bufferOverflow();
}
}
/**
* Write an array of characters. This method cannot be inherited from the
* Writer class because it must suppress I/O exceptions.
* @param buf
* @throws IOException
*/
public final void write(char buf[]) throws IOException
{
write(buf, 0, buf.length);
}
/**
* Write a portion of a String.
*
* @param s String to be written
* @param off Offset from which to start reading characters
* @param len Number of characters to be written
* @throws IOException
*
*/
public final void write(String s, int off, int len) throws IOException
{
if (bufferSize == 0)
{
writer.write(s, off, len);
return;
}
int b = off, t = off + len;
while (b < t)
{
int d = min(bufferSize - nextChar, t - b);
s.getChars(b, b + d, cb, nextChar);
b += d;
nextChar += d;
if (nextChar >= bufferSize)
if (autoFlush)
flushBuffer();
else
bufferOverflow();
}
}
/**
* Write a string. This method cannot be inherited from the Writer class
* because it must suppress I/O exceptions.
* @param s
* @throws IOException
*/
public final void write(String s) throws IOException
{
if (s != null)
{
write(s, 0, s.length());
}
}
/**
* resets this class so that it can be reused
* @param writer
*
*/
public final void recycle(Writer writer)
{
this.writer = writer;
clear();
}
}