Writerpublic abstract class Writer extends Object implements Appendable, Closeable, FlushableThe base class for all writers. A writer is a means of writing data to a
target in a character-wise manner. Most output streams expect the
{@link #flush()} method to be called before closing the stream, to ensure all
data is actually written out.
This abstract class does not provide a fully working implementation, so it
needs to be subclassed, and at least the {@link #write(char[], int, int)},
{@link #close()} and {@link #flush()} methods needs to be overridden.
Overriding some of the non-abstract methods is also often advised, since it
might result in higher efficiency.
Many specialized readers for purposes like reading from a file already exist
in this package. |
Fields Summary |
---|
static final String | TOKEN_NULL | protected Object | lockThe object used to synchronize access to the writer. |
Constructors Summary |
---|
protected Writer()Constructs a new {@code Writer} with {@code this} as the object used to
synchronize critical sections.
super();
lock = this;
| protected Writer(Object lock)Constructs a new {@code Writer} with {@code lock} used to synchronize
critical sections.
if (lock == null) {
throw new NullPointerException();
}
this.lock = lock;
|
Methods Summary |
---|
public java.io.Writer | append(char c)Appends the character {@code c} to the target. This method works the same
way as {@link #write(int)}.
write(c);
return this;
| public java.io.Writer | append(java.lang.CharSequence csq)Appends the character sequence {@code csq} to the target. This method
works the same way as {@code Writer.write(csq.toString())}. If {@code
csq} is {@code null}, then the string "null" is written to the target
stream.
if (null == csq) {
write(TOKEN_NULL);
} else {
write(csq.toString());
}
return this;
| public java.io.Writer | append(java.lang.CharSequence csq, int start, int end)Appends a subsequence of the character sequence {@code csq} to the
target. This method works the same way as {@code
Writer.writer(csq.subsequence(start, end).toString())}. If {@code
csq} is {@code null}, then the specified subsequence of the string "null"
will be written to the target.
if (null == csq) {
write(TOKEN_NULL.substring(start, end));
} else {
write(csq.subSequence(start, end).toString());
}
return this;
| public abstract void | close()Closes this writer. Implementations of this method should free any
resources associated with the writer.
| public abstract void | flush()Flushes this writer. Implementations of this method should ensure that
all buffered characters are written to the target.
| public void | write(char[] buf)Writes the entire character buffer {@code buf} to the target.
// BEGIN android-note
// changed array notation to be consistent with the rest of harmony
// END android-note
write(buf, 0, buf.length);
| public abstract void | write(char[] buf, int offset, int count)Writes {@code count} characters starting at {@code offset} in {@code buf}
to the target.
| public void | write(int oneChar)Writes one character to the target. Only the two least significant bytes
of the integer {@code oneChar} are written.
synchronized (lock) {
char oneCharArray[] = new char[1];
oneCharArray[0] = (char) oneChar;
write(oneCharArray);
}
| public void | write(java.lang.String str)Writes the characters from the specified string to the target.
char buf[] = new char[str.length()];
str.getChars(0, buf.length, buf, 0);
synchronized (lock) {
write(buf);
}
| public void | write(java.lang.String str, int offset, int count)Writes {@code count} characters from {@code str} starting at {@code
offset} to the target.
if (count < 0) { // other cases tested by getChars()
throw new StringIndexOutOfBoundsException();
}
char buf[] = new char[count];
str.getChars(offset, offset + count, buf, 0);
synchronized (lock) {
write(buf);
}
|
|