1
2 package jsdsi.sexp;
3
4 import java.io.FilterOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.io.OutputStreamWriter;
8 import java.io.Writer;
9
10 /***
11 * Writes serialized S-expressions to an underlying stream. Supports
12 * the canonical, transport, and readable S-expression encodings.
13 *
14 * @see SexpInputStream
15 *
16 * @author Sameer Ajmani
17 * @version $Revision: 1.1.6.1 $ $Date: 2005/11/08 03:12:52 $
18 */
19 public class SexpOutputStream extends FilterOutputStream {
20 /***
21 * Creates a new <code>SexpOutputStream</code> that writes to the given
22 * stream.
23 */
24 public SexpOutputStream(OutputStream os) {
25 super(os);
26 }
27
28 /***
29 * Writes an S-expression to the stream in readable form.
30 *
31 * @param s the S-expression to write.
32 * @param offset spaces indented from left.
33 * @param width total width of window, in characters.
34 * @param last spaces reserved on right (e.g., for closing parens).
35 * @throws IOException if there is an IO error.
36 */
37 public void writeReadable(Sexp s, int offset, int width, int last)
38 throws IOException {
39 Writer w = new OutputStreamWriter(out);
40 s.writeReadable(w, offset, width, last);
41 w.flush();
42 }
43
44 /***
45 * Writes an S-expression to the stream in transport form.
46 *
47 * @param s the S-expression to write.
48 * @throws IOException if there is an IO error.
49 */
50 public void writeTransport(Sexp s) throws IOException {
51 s.writeTransport(out);
52 }
53
54 /***
55 * Writes an S-expression to the stream in canonical form.
56 *
57 * @param s the S-expression to write
58 * @throws IOException if there is an IO error
59 */
60 public void writeCanonical(Sexp s) throws IOException {
61 s.writeCanonical(out);
62 }
63
64 /***
65 * Returns a new SexpOutput object that writes S-expressions to this
66 * stream in readable form.
67 *
68 * @param offset spaces indented from left.
69 * @param width total width of window, in characters.
70 * @param last spaces reserved on right (e.g., for closing parens).
71 **/
72 public SexpOutput toReadable(final int offset,
73 final int width,
74 final int last)
75 {
76 return new SexpOutput() {
77 public void writeSexp(Sexp s) throws IOException
78 {
79 SexpOutputStream.this.writeReadable
80 (s, offset, width, last);
81 }
82 public void flush() throws IOException
83 {
84 SexpOutputStream.this.flush();
85 }
86 };
87 }
88
89 /***
90 * Returns a new SexpOutput object that writes S-expressions to this
91 * stream in canonical form.
92 **/
93 public SexpOutput toCanonical() {
94 return new SexpOutput() {
95 public void writeSexp(Sexp s) throws IOException
96 {
97 SexpOutputStream.this.writeCanonical(s);
98 }
99 public void flush() throws IOException
100 {
101 SexpOutputStream.this.flush();
102 }
103 };
104 }
105
106 /***
107 * Returns a new SexpOutput object that writes S-expressions to this
108 * stream in transport form.
109 **/
110 public SexpOutput toTransport() {
111 return new SexpOutput() {
112 public void writeSexp(Sexp s) throws IOException
113 {
114 SexpOutputStream.this.writeTransport(s);
115 }
116 public void flush() throws IOException
117 {
118 SexpOutputStream.this.flush();
119 }
120 };
121 }
122 }
This page was automatically generated by Maven