KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > RemoteOutputStream


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.OutputStream JavaDoc;
7 import java.io.Serializable JavaDoc;
8
9 /**
10  * {@link OutputStream} that can be sent over to the remote {@link Channel},
11  * so that the remote {@link Callable} can write to a local {@link OutputStream}.
12  *
13  * <h2>Usage</h2>
14  * <p>
15  * To have a remote machine write to a local {@link OutputStream}:
16  * <pre>
17  * final OutputStream out = new RemoteOutputStream(os);
18  *
19  * channel.call(new Callable() {
20  * public Object call() {
21  * // this will write to 'os'.
22  * out.write(...);
23  * }
24  * });
25  * </pre>
26  *
27  * <p>
28  * To have a local machine write to a remote {@link OutputStream}:
29  *
30  * <pre>
31  * OutputStream os = channel.call(new Callable() {
32  * public Object call() {
33  * OutputStream os = new FileOutputStream(...); // or any other OutputStream
34  * return new RemoteOutputStream(os);
35  * }
36  * });
37  * </pre>
38  *
39  * @see RemoteInputStream
40  * @author Kohsuke Kawaguchi
41  */

42 public final class RemoteOutputStream extends OutputStream JavaDoc implements Serializable JavaDoc {
43     /**
44      * On local machine, this points to the {@link OutputStream} where
45      * the data will be sent ultimately.
46      *
47      * On remote machine, this points to {@link ProxyOutputStream} that
48      * does the network proxy.
49      */

50     private transient OutputStream JavaDoc core;
51
52     public RemoteOutputStream(OutputStream JavaDoc core) {
53         this.core = core;
54     }
55
56     private void writeObject(ObjectOutputStream JavaDoc oos) throws IOException JavaDoc {
57         int id = Channel.current().export(core);
58         oos.writeInt(id);
59     }
60
61     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
62         final Channel channel = Channel.current();
63         assert channel !=null;
64
65         this.core = new ProxyOutputStream(channel, ois.readInt());
66     }
67
68     private static final long serialVersionUID = 1L;
69
70
71 //
72
//
73
// delegation to core
74
//
75
//
76
public void write(int b) throws IOException JavaDoc {
77         core.write(b);
78     }
79
80     public void write(byte[] b) throws IOException JavaDoc {
81         core.write(b);
82     }
83
84     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
85         core.write(b, off, len);
86     }
87
88     public void flush() throws IOException JavaDoc {
89         core.flush();
90     }
91
92     public void close() throws IOException JavaDoc {
93         core.close();
94     }
95 }
96
Popular Tags