package class5_3_StreamDecorator_start; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; /** * Adapter for a Writer to behave like an OutputStream. * * An Adapter is something like a Decorator, except the wrapping class does not implement the same interface * as the class that is wrapped. * * Bytes are converted to chars using the platform default encoding. * If this encoding is not a single-byte encoding, some data may be lost. * * Courtesy of http://www.coderanch.com/t/278540/java-io/java/create-PrintStream-PrintWriter */ public class WriterOutputStream extends OutputStream { private final Writer writer; public WriterOutputStream(Writer writer) { this.writer = writer; } public void write(int b) throws IOException { // Jim Yingst from coderanch: // It's tempting to use writer.write((char) b), but that may get the encoding wrong // This is inefficient, but it works write(new byte[] {(byte) b}, 0, 1); } public void write(byte b[], int off, int len) throws IOException { writer.write(new String(b, off, len)); } public void flush() throws IOException { writer.flush(); } public void close() throws IOException { writer.close(); } }