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.
| Constructor: |
public OutputStreamWriter(OutputStream out) {
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);
}
Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to. The default character encoding
is used. Parameters:
out -
the non-null target stream to write converted bytes to.
|
public OutputStreamWriter(OutputStream out,
String enc) throws UnsupportedEncodingException {
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);
}
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. Parameters:
out -
the target stream to write converted bytes to.
enc -
the string describing the desired character encoding.
Throws:
NullPointerException -
if {@code enc} is {@code null}.
UnsupportedEncodingException -
if the encoding specified by {@code enc} cannot be found.
|
public OutputStreamWriter(OutputStream out,
Charset cs) {
super(out);
this.out = out;
encoder = cs.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to and {@code cs} as the character
encoding. Parameters:
out -
the target stream to write converted bytes to.
cs -
the {@code Charset} that specifies the character encoding.
|
public OutputStreamWriter(OutputStream out,
CharsetEncoder enc) {
super(out);
enc.charset();
this.out = out;
encoder = enc;
}
Constructs a new OutputStreamWriter using {@code out} as the target
stream to write converted characters to and {@code enc} as the character
encoder. Parameters:
out -
the target stream to write converted bytes to.
enc -
the character encoder used for character conversion.
|
| Method from java.io.OutputStreamWriter Detail: |
boolean checkError() {
return out.checkError();
}
|
public void close() throws IOException {
synchronized (lock) {
if (encoder != null) {
encoder.flush(bytes);
flush();
out.flush();
out.close();
encoder = null;
bytes = null;
}
}
}
|
public void flush() throws IOException {
synchronized (lock) {
checkStatus();
int position;
if ((position = bytes.position()) > 0) {
bytes.flip();
out.write(bytes.array(), 0, position);
bytes.clear();
}
out.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. |
public String getEncoding() {
if (encoder == null) {
return null;
}
return HistoricalNamesUtil.getHistoricalName(encoder.charset().name());
}
Gets the name of the encoding that is used to convert characters to
bytes. |
public void write(int oneChar) throws IOException {
synchronized (lock) {
checkStatus();
CharBuffer chars = CharBuffer.wrap(new char[] { (char) oneChar });
convert(chars);
}
}
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. |
public void write(char[] buf,
int offset,
int count) throws IOException {
synchronized (lock) {
checkStatus();
if (offset < 0 || offset > buf.length - count || count < 0) {
throw new IndexOutOfBoundsException();
}
CharBuffer chars = CharBuffer.wrap(buf, offset, count);
convert(chars);
}
}
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. |
public void write(String str,
int offset,
int count) throws IOException {
synchronized (lock) {
// avoid int overflow
if (count < 0) {
throw new IndexOutOfBoundsException();
}
if (offset > str.length() - count || offset < 0) {
throw new StringIndexOutOfBoundsException();
}
checkStatus();
CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
convert(chars);
}
}
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. |