| 1 |
// Copyright 2003, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.IOException; |
| 5 |
import java.io.OutputStream; |
| 6 |
import java.io.Writer; |
| 7 |
|
| 8 |
/** |
| 9 |
* The WriterOutputStream makes a Writer look like an OutputStream. |
| 10 |
* |
| 11 |
* @author Mark Donszelmann |
| 12 |
* @version $Id: WriterOutputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 13 |
*/ |
| 14 |
public class WriterOutputStream extends OutputStream { |
| 15 |
|
| 16 |
private Writer writer; |
| 17 |
|
| 18 |
/** |
| 19 |
* Create an Output Stream from given Writer. |
| 20 |
* |
| 21 |
* @param writer writer to write to |
| 22 |
*/ |
| 23 |
public WriterOutputStream(Writer writer) { |
| 24 |
this.writer = writer; |
| 25 |
} |
| 26 |
|
| 27 |
public void write(int b) throws IOException { |
| 28 |
writer.write(b & 0xFF); |
| 29 |
} |
| 30 |
|
| 31 |
public void write(byte[] b) throws IOException { |
| 32 |
write(b, 0, b.length); |
| 33 |
} |
| 34 |
|
| 35 |
public void write(byte[] b, int off, int len) throws IOException { |
| 36 |
for (int i = 0; i < len; i++) { |
| 37 |
writer.write(b[off + i]); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public void close() throws IOException { |
| 42 |
writer.close(); |
| 43 |
} |
| 44 |
|
| 45 |
public void flush() throws IOException { |
| 46 |
writer.flush(); |
| 47 |
} |
| 48 |
} |