BufferedWriterpublic class BufferedWriter extends Writer Wraps an existing {@link Writer} and buffers the output. Expensive
interaction with the underlying reader is minimized, since most (smaller)
requests can be satisfied by accessing the buffer alone. The drawback is that
some extra space is required to hold the buffer and that copying takes place
when filling that buffer, but this is usually outweighed by the performance
benefits.
A typical application pattern for the class looks like this:
BufferedWriter buf = new BufferedWriter(new FileWriter("file.java"));
|
Fields Summary |
---|
private Writer | out | private char[] | buf | private int | pos | private final String | lineSeparator |
Constructors Summary |
---|
public BufferedWriter(Writer out)Constructs a new {@code BufferedWriter} with {@code out} as the writer
for which to buffer write operations. The buffer size is set to the
default value of 8 KB. //$NON-NLS-1$
super(out);
this.out = out;
buf = new char[8192];
// BEGIN android-added
/*
* For Android, we want to discourage the use of this constructor (with
* its arguably too-large default), so we note its use in the log. We
* don't disable it, nor do we alter the default, however, because we
* still aim to behave compatibly, and the default value, though not
* documented, is established by convention.
*/
Logger.global.info(
"Default buffer size used in BufferedWriter " +
"constructor. It would be " +
"better to be explicit if an 8k-char buffer is required.");
// END android-added
| public BufferedWriter(Writer out, int size)Constructs a new {@code BufferedWriter} with {@code out} as the writer
for which to buffer write operations. The buffer size is set to {@code
size}.
super(out);
if (size <= 0) {
throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
}
this.out = out;
this.buf = new char[size];
|
Methods Summary |
---|
public void | close()Closes this writer. The contents of the buffer are flushed, the target
writer is closed, and the buffer is released. Only the first invocation
of close has any effect.
synchronized (lock) {
if (!isClosed()) {
flush();
out.close();
buf = null;
out = null;
}
}
| public void | flush()Flushes this writer. The contents of the buffer are committed to the
target writer and it is then flushed.
synchronized (lock) {
if (isClosed()) {
throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
}
if (pos > 0) {
out.write(buf, 0, pos);
}
pos = 0;
out.flush();
}
| private boolean | isClosed()Indicates whether this writer is closed.
return out == null;
| public void | newLine()Writes a newline to this writer. A newline is determined by the System
property "line.separator". The target writer may or may not be flushed
when a newline is written.
write(lineSeparator, 0, lineSeparator.length());
| public void | write(char[] cbuf, int offset, int count)Writes {@code count} characters starting at {@code offset} in
{@code cbuf} to this writer. If {@code count} is greater than this
writer's buffer, then the buffer is flushed and the characters are
written directly to the target writer.
synchronized (lock) {
if (isClosed()) {
throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
}
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// made implicit null check explicit, used (offset | count) < 0
// instead of (offset < 0) || (count < 0) to safe one operation
if (cbuf == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | count) < 0 || offset > cbuf.length - count) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
if (pos == 0 && count >= this.buf.length) {
out.write(cbuf, offset, count);
return;
}
int available = this.buf.length - pos;
if (count < available) {
available = count;
}
if (available > 0) {
System.arraycopy(cbuf, offset, this.buf, pos, available);
pos += available;
}
if (pos == this.buf.length) {
out.write(this.buf, 0, this.buf.length);
pos = 0;
if (count > available) {
offset += available;
available = count - available;
if (available >= this.buf.length) {
out.write(cbuf, offset, available);
return;
}
System.arraycopy(cbuf, offset, this.buf, pos, available);
pos += available;
}
}
}
| public void | write(int oneChar)Writes the character {@code oneChar} to this writer. If the buffer
gets full by writing this character, this writer is flushed. Only the
lower two bytes of the integer {@code oneChar} are written.
synchronized (lock) {
if (isClosed()) {
throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
}
if (pos >= buf.length) {
out.write(buf, 0, buf.length);
pos = 0;
}
buf[pos++] = (char) oneChar;
}
| public void | write(java.lang.String str, int offset, int count)Writes {@code count} characters starting at {@code offset} in {@code str}
to this writer. If {@code count} is greater than this writer's buffer,
then this writer is flushed and the remaining characters are written
directly to the target writer. If count is negative no characters are
written to the buffer. This differs from the behavior of the superclass.
synchronized (lock) {
if (isClosed()) {
throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
}
if (count <= 0) {
return;
}
if (offset > str.length() - count || offset < 0) {
throw new StringIndexOutOfBoundsException();
}
if (pos == 0 && count >= buf.length) {
char[] chars = new char[count];
str.getChars(offset, offset + count, chars, 0);
out.write(chars, 0, count);
return;
}
int available = buf.length - pos;
if (count < available) {
available = count;
}
if (available > 0) {
str.getChars(offset, offset + available, buf, pos);
pos += available;
}
if (pos == buf.length) {
out.write(this.buf, 0, this.buf.length);
pos = 0;
if (count > available) {
offset += available;
available = count - available;
if (available >= buf.length) {
char[] chars = new char[count];
str.getChars(offset, offset + available, chars, 0);
out.write(chars, 0, available);
return;
}
str.getChars(offset, offset + available, buf, pos);
pos += available;
}
}
}
|
|