KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > RemoteWriter


1 package hudson.remoting;
2
3 import java.io.IOException JavaDoc;
4 import java.io.ObjectInputStream JavaDoc;
5 import java.io.ObjectOutputStream JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import java.io.Writer JavaDoc;
8
9 /**
10  * {@link Writer} that can be sent over to the remote {@link Channel},
11  * so that the remote {@link Callable} can write to a local {@link Writer}.
12  *
13  * <h2>Usage</h2>
14  * <pre>
15  * final Writer out = new RemoteWriter(w);
16  *
17  * channel.call(new Callable() {
18  * public Object call() {
19  * // this will write to 'w'.
20  * out.write(...);
21  * }
22  * });
23  * </pre>
24  *
25  * @see RemoteInputStream
26  * @author Kohsuke Kawaguchi
27  */

28 public final class RemoteWriter extends Writer JavaDoc implements Serializable JavaDoc {
29     /**
30      * On local machine, this points to the {@link Writer} where
31      * the data will be sent ultimately.
32      *
33      * On remote machine, this points to {@link ProxyOutputStream} that
34      * does the network proxy.
35      */

36     private transient Writer JavaDoc core;
37
38     public RemoteWriter(Writer JavaDoc core) {
39         this.core = core;
40     }
41
42     private void writeObject(ObjectOutputStream JavaDoc oos) throws IOException JavaDoc {
43         int id = Channel.current().export(core);
44         oos.writeInt(id);
45     }
46
47     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
48         final Channel channel = Channel.current();
49         assert channel !=null;
50
51         this.core = new ProxyWriter(channel, ois.readInt());
52     }
53
54     private static final long serialVersionUID = 1L;
55
56
57 //
58
//
59
// delegation to core
60
//
61
//
62
public void write(int c) throws IOException JavaDoc {
63         core.write(c);
64     }
65
66     public void write(char[] cbuf) throws IOException JavaDoc {
67         core.write(cbuf);
68     }
69
70     public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
71         core.write(cbuf, off, len);
72     }
73
74     public void write(String JavaDoc str) throws IOException JavaDoc {
75         core.write(str);
76     }
77
78     public void write(String JavaDoc str, int off, int len) throws IOException JavaDoc {
79         core.write(str, off, len);
80     }
81
82     public Writer JavaDoc append(CharSequence JavaDoc csq) throws IOException JavaDoc {
83         return core.append(csq);
84     }
85
86     public Writer JavaDoc append(CharSequence JavaDoc csq, int start, int end) throws IOException JavaDoc {
87         return core.append(csq, start, end);
88     }
89
90     public Writer JavaDoc append(char c) throws IOException JavaDoc {
91         return core.append(c);
92     }
93
94     public void flush() throws IOException JavaDoc {
95         core.flush();
96     }
97
98     public void close() throws IOException JavaDoc {
99         core.close();
100     }
101 }
102
Popular Tags