1 28 29 package com.caucho.vfs; 30 31 import javax.net.ssl.SSLContext; 32 import javax.net.ssl.SSLSocketFactory; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.net.Socket ; 37 import java.net.SocketException ; 38 39 43 class TcpStream extends StreamImpl { 44 private Socket _s; 45 private InputStream _is; 46 private OutputStream _os; 47 48 private TcpStream(TcpPath path, long timeout) throws IOException 49 { 50 setPath(path); 51 52 _s = new Socket (); 54 55 if (timeout > 0) 56 _s.connect(path.getSocketAddress(), (int) timeout); 57 else 58 _s.connect(path.getSocketAddress()); 59 60 if (! _s.isConnected()) 61 throw new IOException ("connection timeout"); 62 63 if (timeout < 0) 64 timeout = 120000; 65 66 _s.setSoTimeout((int) timeout); 67 68 try { 69 if (path instanceof TcpsPath) { 70 SSLContext context = SSLContext.getInstance("TLS"); 71 72 javax.net.ssl.TrustManager tm = 73 new javax.net.ssl.X509TrustManager() { 74 public java.security.cert.X509Certificate [] 75 getAcceptedIssuers() { 76 return null; 77 } 78 public void checkClientTrusted( 79 java.security.cert.X509Certificate [] cert, String foo) { 80 } 81 public void checkServerTrusted( 82 java.security.cert.X509Certificate [] cert, String foo) { 83 } 84 }; 85 86 87 context.init(null, new javax.net.ssl.TrustManager[] { tm }, null); 88 SSLSocketFactory factory = context.getSocketFactory(); 89 90 _s = factory.createSocket(_s, path.getHost(), path.getPort(), true); 91 } 92 } catch (IOException e) { 93 throw e; 94 } catch (RuntimeException e) { 95 throw e; 96 } catch (Exception e) { 97 throw new IOExceptionWrapper(e); 98 } 99 100 _is = _s.getInputStream(); 101 _os = _s.getOutputStream(); 102 } 103 104 public void setAttribute(String name, Object value) 105 { 106 if (name.equals("timeout")) { 107 try { 108 if (value instanceof Number ) 109 _s.setSoTimeout(((Number ) value).intValue()); 110 else 111 _s.setSoTimeout(Integer.parseInt(String.valueOf(value))); 112 } catch (SocketException e) { 113 } 114 } 115 } 116 117 static TcpStream openRead(TcpPath path, long timeout) throws IOException 118 { 119 return new TcpStream(path, timeout); 120 } 121 122 static TcpStream openReadWrite(TcpPath path, long timeout) throws IOException 123 { 124 return new TcpStream(path, timeout); 125 } 126 127 public boolean canWrite() 128 { 129 return _os != null; 130 } 131 132 140 public void write(byte []buf, int offset, int length, boolean isEnd) 141 throws IOException 142 { 143 if (_os != null) 144 _os.write(buf, offset, length); 145 } 146 147 public boolean canRead() 148 { 149 return _is != null; 150 } 151 152 public int getAvailable() throws IOException 153 { 154 if (_is != null) 155 return _is.available(); 156 else 157 return -1; 158 } 159 160 public int read(byte []buf, int offset, int length) throws IOException 161 { 162 if (_is != null) 163 return _is.read(buf, offset, length); 164 else 165 return -1; 166 } 167 168 public void closeWrite() throws IOException 169 { 170 OutputStream os = _os; 171 _os = null; 172 173 try { 174 if (os != null) 175 _s.shutdownOutput(); 176 } finally { 177 if (_is == null) { 178 Socket s = _s; 179 _s = null; 180 181 if (s != null) 182 s.close(); 183 } 184 } 185 } 186 187 public void close() throws IOException 188 { 189 InputStream is = _is; 190 _is = null; 191 192 OutputStream os = _os; 193 _os = null; 194 195 Socket s = _s; 196 _s = null; 197 198 try { 199 if (os != null) 200 os.close(); 201 202 if (is != null) 203 is.close(); 204 } finally { 205 if (s != null) 206 s.close(); 207 } 208 } 209 } 210 | Popular Tags |