1 29 30 package com.caucho.vfs; 31 32 import com.caucho.util.CharBuffer; 33 34 import java.io.IOException ; 35 import java.net.InetSocketAddress ; 36 import java.net.SocketAddress ; 37 import java.util.Map ; 38 39 42 public class TcpPath extends Path { 43 public static final String CONNECT_TIMEOUT = "connect-timeout"; 45 46 protected String _host; 47 protected int _port; 48 protected SocketAddress _address; 49 protected long _timeout = 5000L; 50 51 public TcpPath(TcpPath root, String userPath, 52 Map <String ,Object > newAttributes, 53 String host, int port) 54 { 55 super(root); 56 57 setUserPath(userPath); 58 59 _host = host; 60 _port = port == 0 ? 80 : port; 61 62 if (newAttributes != null) { 63 Object timeout = newAttributes.get("connect-timeout"); 64 65 if (timeout instanceof Number ) 66 _timeout = ((Number ) timeout).longValue(); 67 } 68 } 69 70 73 protected Path schemeWalk(String userPath, 74 Map <String ,Object > newAttributes, 75 String uri, 76 int offset) 77 { 78 int length = uri.length(); 79 80 if (length < 2 + offset || 81 uri.charAt(offset) != '/' || 82 uri.charAt(1 + offset) != '/') 83 throw new RuntimeException ("bad scheme"); 84 85 CharBuffer buf = new CharBuffer(); 86 int i = 2 + offset; 87 int ch = 0; 88 for (; i < length && (ch = uri.charAt(i)) != ':' && ch != '/' && ch != '?'; 89 i++) { 90 buf.append((char) ch); 91 } 92 93 String host = buf.toString(); 94 if (host.length() == 0) 95 throw new RuntimeException ("bad host"); 96 97 int port = 0; 98 if (ch == ':') { 99 for (i++; i < length && (ch = uri.charAt(i)) >= '0' && ch <= '9'; i++) { 100 port = 10 * port + uri.charAt(i) - '0'; 101 } 102 } 103 104 return create(this, userPath, newAttributes, host, port); 105 } 106 107 protected TcpPath create(TcpPath root, 108 String userPath, Map <String ,Object > newAttributes, 109 String host, int port) 110 { 111 return new TcpPath(root, userPath, newAttributes, host, port); 112 } 113 114 public String getScheme() 115 { 116 return "tcp"; 117 } 118 119 public String getURL() 120 { 121 return (getScheme() + "://" + getHost() + ":" + getPort()); 122 } 123 124 public String getPath() 125 { 126 return ""; 127 } 128 129 public String getHost() 130 { 131 return _host; 132 } 133 134 public int getPort() 135 { 136 return _port; 137 } 138 139 public SocketAddress getSocketAddress() 140 { 141 if (_address == null) 142 _address = new InetSocketAddress (_host, _port); 143 144 return _address; 145 } 146 147 159 160 public StreamImpl openReadImpl() throws IOException 161 { 162 return TcpStream.openRead(this, _timeout); 163 } 164 165 public StreamImpl openReadWriteImpl() throws IOException 166 { 167 return TcpStream.openReadWrite(this, _timeout); 168 } 169 170 @Override 171 protected Path cacheCopy() 172 { 173 return new TcpPath(null, getUserPath(), null, _host, _port); 174 } 175 176 public String toString() 177 { 178 return getURL(); 179 } 180 } 181 | Popular Tags |