1 package hudson.remoting; 2 3 import java.io.CharArrayWriter ; 4 import java.io.IOException ; 5 import java.io.OutputStream ; 6 import java.io.Writer ; 7 8 12 final class ProxyWriter extends Writer { 13 private Channel channel; 14 private int oid; 15 16 20 private CharArrayWriter tmp; 21 22 25 private boolean closed; 26 27 33 public ProxyWriter() { 34 } 35 36 42 public ProxyWriter(Channel channel, int oid) throws IOException { 43 connect(channel,oid); 44 } 45 46 49 synchronized void connect(Channel channel, int oid) throws IOException { 50 if(this.channel!=null) 51 throw new IllegalStateException ("Cannot connect twice"); 52 this.channel = channel; 53 this.oid = oid; 54 55 if(tmp!=null) { 57 write(tmp.toCharArray()); 58 tmp = null; 59 } 60 if(closed) close(); 62 } 63 64 public void write(int c) throws IOException { 65 write(new char[]{(char)c},0,1); 66 } 67 68 public void write(char[] cbuf, int off, int len) throws IOException { 69 if(closed) 70 throw new IOException ("stream is already closed"); 71 if(off==0 && len==cbuf.length) 72 write(cbuf); 73 else { 74 char[] buf = new char[len]; 75 System.arraycopy(cbuf,off,buf,0,len); 76 write(buf); 77 } 78 } 79 80 81 82 public synchronized void write(char[] cbuf) throws IOException { 83 if(closed) 84 throw new IOException ("stream is already closed"); 85 if(channel==null) { 86 if(tmp==null) 87 tmp = new CharArrayWriter (); 88 tmp.write(cbuf); 89 } else { 90 channel.send(new Chunk(oid,cbuf)); 91 } 92 } 93 94 public void flush() throws IOException { 95 } 97 98 public synchronized void close() throws IOException { 99 closed = true; 100 if(channel!=null) { 101 channel.send(new EOF(oid)); 102 channel = null; 103 oid = -1; 104 } 105 } 106 107 protected void finalize() throws Throwable { 108 super.finalize(); 109 close(); 110 } 111 112 115 private static final class Chunk extends Command { 116 private final int oid; 117 private final char[] buf; 118 119 public Chunk(int oid, char[] buf) { 120 this.oid = oid; 121 this.buf = buf; 122 } 123 124 protected void execute(Channel channel) { 125 Writer os = (Writer ) channel.getExportedObject(oid); 126 try { 127 os.write(buf); 128 } catch (IOException e) { 129 } 131 } 132 133 public String toString() { 134 return "Pipe.Chunk("+oid+","+buf.length+")"; 135 } 136 137 private static final long serialVersionUID = 1L; 138 } 139 140 143 private static final class EOF extends Command { 144 private final int oid; 145 146 public EOF(int oid) { 147 this.oid = oid; 148 } 149 150 protected void execute(Channel channel) { 151 OutputStream os = (OutputStream ) channel.getExportedObject(oid); 152 channel.unexport(oid); 153 try { 154 os.close(); 155 } catch (IOException e) { 156 } 158 } 159 160 public String toString() { 161 return "Pipe.EOF("+oid+")"; 162 } 163 164 private static final long serialVersionUID = 1L; 165 } 166 } 167 | Popular Tags |