01: /*
02: * Created on May 19, 2005
03: */
04: package uk.org.ponder.streamutil.write;
05:
06: import java.io.IOException;
07: import java.io.Writer;
08:
09: import uk.org.ponder.streamutil.StreamCloseUtil;
10: import uk.org.ponder.util.UniversalRuntimeException;
11:
12: /**
13: * A wrapper to convert a Java Writer object into a PrintOutputStream. All
14: * IOExceptions (or others) will be wrapped into unchecked
15: * UniversalRuntimeExceptions, q.v. Note that since Writers have horrible
16: * dependence on synchronization, you'd be better off using an OutputStreamPOS.
17: * @author Antranig Basman (antranig@caret.cam.ac.uk)
18: *
19: */
20: public class WriterPOS implements PrintOutputStream {
21: private Writer w;
22:
23: public WriterPOS(Writer w) {
24: this .w = w;
25: }
26:
27: public void println(String toprint) {
28: try {
29: w.write(toprint);
30: w.write("\n");
31: } catch (Exception e) {
32: throw UniversalRuntimeException.accumulate(e);
33: }
34: }
35:
36: public void flush() {
37: try {
38: w.flush();
39: } catch (IOException e) {
40: throw UniversalRuntimeException.accumulate(e);
41: }
42: }
43:
44: public void close() {
45: StreamCloseUtil.closeWriter(w);
46: }
47:
48: public PrintOutputStream print(String string) {
49: try {
50: w.write(string);
51: } catch (Exception e) {
52: throw UniversalRuntimeException.accumulate(e);
53: }
54: return this ;
55: }
56:
57: public void println() {
58: print("\n");
59: }
60:
61: public void println(Object obj) {
62: println(obj.toString());
63: }
64:
65: public void write(char[] storage, int offset, int size) {
66: try {
67: w.write(storage, offset, size);
68: } catch (IOException e) {
69: throw UniversalRuntimeException.accumulate(e);
70: }
71: }
72:
73: }
|