OutputStreamWriterクラスはOutputStreamに対して書き込むためのWriterクラスである。java.io.Writerクラスを継承している。インスタンス変数として,sun.nio.cs.StreamEncoderというクラスが使われており,このクラスでエンコードしていると思われる。
コンストラクタとしては,次のようなものが定義されている。
- OutputStreamWriter(OutputStream out, String charsetName)
- OutputStreamWriter(OutputStream out)
- OutputStreamWriter(OutputStream out, Charset cs)
- OutputStreamWriter(OutputStream out, CharsetEncoder enc)
write(int c)メソッドはOutputStreamに対して,int型の引数で与えられた文字1文字を書き込む。write(char cbuf[], int off, int len)メソッドはchar[]型の引数で与えられた配列の指定のオフセットから指定の長さだけOutputStreamに書き込む。write(String str, int off, int len)メソッドはchar[]型配列の代わりにString型のオブジェクトを書き込む。
append(CharSequence csq, int start, int end)メソッドは CharSequence インタフェースを実装したオブジェクトのインデックスstartからendまでを切り出して,追加する。append(CharSequence csq)メソッドはそのまま引数で与えられたオブジェクトを追加してOutputStreamへ書き込む。
/*
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the “Classpath” exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import sun.nio.cs.StreamEncoder;
public class OutputStreamWriter extends Writer {
private final StreamEncoder se;
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException(“charsetName”);
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
public OutputStreamWriter(OutputStream out, Charset cs) {
super(out);
if (cs == null)
throw new NullPointerException(“charset”);
se = StreamEncoder.forOutputStreamWriter(out, this, cs);
}
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
super(out);
if (enc == null)
throw new NullPointerException(“charset encoder”);
se = StreamEncoder.forOutputStreamWriter(out, this, enc);
}
public String getEncoding() {
return se.getEncoding();
}
void flushBuffer() throws IOException {
se.flushBuffer();
}
public void write(int c) throws IOException {
se.write(c);
}
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
@Override
public Writer append(CharSequence csq, int start, int end) throws IOException {
if (csq == null) csq = “null”;
return append(csq.subSequence(start, end));
}
@Override
public Writer append(CharSequence csq) throws IOException {
if (csq instanceof CharBuffer) {
se.write((CharBuffer) csq);
} else {
se.write(String.valueOf(csq));
}
return this;
}
public void flush() throws IOException {
se.flush();
}
public void close() throws IOException {
se.close();
}
}
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the “Classpath” exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import sun.nio.cs.StreamEncoder;
public class OutputStreamWriter extends Writer {
private final StreamEncoder se;
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException(“charsetName”);
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
public OutputStreamWriter(OutputStream out, Charset cs) {
super(out);
if (cs == null)
throw new NullPointerException(“charset”);
se = StreamEncoder.forOutputStreamWriter(out, this, cs);
}
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
super(out);
if (enc == null)
throw new NullPointerException(“charset encoder”);
se = StreamEncoder.forOutputStreamWriter(out, this, enc);
}
public String getEncoding() {
return se.getEncoding();
}
void flushBuffer() throws IOException {
se.flushBuffer();
}
public void write(int c) throws IOException {
se.write(c);
}
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
@Override
public Writer append(CharSequence csq, int start, int end) throws IOException {
if (csq == null) csq = “null”;
return append(csq.subSequence(start, end));
}
@Override
public Writer append(CharSequence csq) throws IOException {
if (csq instanceof CharBuffer) {
se.write((CharBuffer) csq);
} else {
se.write(String.valueOf(csq));
}
return this;
}
public void flush() throws IOException {
se.flush();
}
public void close() throws IOException {
se.close();
}
}