OutputStreamWriterpublic class OutputStreamWriter extends Writer A class for turning a character stream into a byte stream. Data written to
the target input stream is converted into bytes by either a default or a
provided character converter. The default encoding is taken from the
"file.encoding" system property. {@code OutputStreamWriter} contains a buffer
of bytes to be written to target stream and converts these into characters as
needed. The buffer size is 8K. |
Fields Summary |
---|
private OutputStream | out | private CharsetEncoder | encoder | private ByteBuffer | bytes |
Constructors Summary |
---|
public OutputStreamWriter(OutputStream out)Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to. The default character encoding
is used.
super(out);
this.out = out;
String encoding = AccessController
.doPrivileged(new PriviAction<String>(
"file.encoding", "ISO8859_1")); //$NON-NLS-1$ //$NON-NLS-2$
encoder = Charset.forName(encoding).newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
| public OutputStreamWriter(OutputStream out, String enc)Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to and {@code enc} as the character
encoding. If the encoding cannot be found, an
UnsupportedEncodingException error is thrown.
super(out);
if (enc == null) {
throw new NullPointerException();
}
this.out = out;
try {
encoder = Charset.forName(enc).newEncoder();
} catch (Exception e) {
throw new UnsupportedEncodingException(enc);
}
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
| public OutputStreamWriter(OutputStream out, Charset cs)Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to and {@code cs} as the character
encoding.
super(out);
this.out = out;
encoder = cs.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
| public OutputStreamWriter(OutputStream out, CharsetEncoder enc)Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to and {@code enc} as the character
encoder.
super(out);
enc.charset();
this.out = out;
encoder = enc;
|
Methods Summary |
---|
private void | checkStatus()
if (encoder == null) {
// K005d=Writer is closed.
throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
}
| public void | close()Closes this writer. This implementation flushes the buffer as well as the
target stream. The target stream is then closed and the resources for the
buffer and converter are released.
Only the first invocation of this method has any effect. Subsequent calls
do nothing.
synchronized (lock) {
if (encoder != null) {
encoder.flush(bytes);
flush();
out.flush();
out.close();
encoder = null;
bytes = null;
}
}
| private void | convert(java.nio.CharBuffer chars)
CoderResult result = encoder.encode(chars, bytes, true);
while (true) {
if (result.isError()) {
throw new IOException(result.toString());
} else if (result.isOverflow()) {
// flush the output buffer
flush();
result = encoder.encode(chars, bytes, true);
continue;
}
break;
}
| public void | flush()Flushes this writer. This implementation ensures that all buffered bytes
are written to the target stream. After writing the bytes, the target
stream is flushed as well.
synchronized (lock) {
checkStatus();
int position;
if ((position = bytes.position()) > 0) {
bytes.flip();
out.write(bytes.array(), 0, position);
bytes.clear();
}
out.flush();
}
| public java.lang.String | getEncoding()Gets the name of the encoding that is used to convert characters to
bytes.
if (encoder == null) {
return null;
}
return InputStreamReader.HistoricalNamesUtil.getHistoricalName(encoder
.charset().name());
| public void | write(int oneChar)Writes the character {@code oneChar} to this writer. The lowest two bytes
of the integer {@code oneChar} are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
by converting this character, this writer is flushed.
synchronized (lock) {
checkStatus();
CharBuffer chars = CharBuffer.wrap(new char[] { (char) oneChar });
convert(chars);
}
| 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. The characters are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
as a result of the conversion, this writer is flushed.
synchronized (lock) {
// avoid int overflow
// 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 (str == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | count) < 0 || offset > str.length() - count) {
throw new StringIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
checkStatus();
CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
convert(chars);
}
| public void | write(char[] buf, int offset, int count)Writes {@code count} characters starting at {@code offset} in {@code buf}
to this writer. The characters are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
as a result of the conversion, this writer is flushed.
synchronized (lock) {
checkStatus();
// 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 (buf == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | count) < 0 || offset > buf.length - count) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
CharBuffer chars = CharBuffer.wrap(buf, offset, count);
convert(chars);
}
|
|