23 * questions. 24 */ 25 package sun.net; 26 27 import java.io.*; 28 import java.net.Socket; 29 import java.net.InetAddress; 30 import java.net.InetSocketAddress; 31 import java.net.UnknownHostException; 32 import java.net.Proxy; 33 import java.util.Arrays; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 37 /** 38 * This is the base class for network clients. 39 * 40 * @author Jonathan Payne 41 */ 42 public class NetworkClient { 43 protected Proxy proxy = Proxy.NO_PROXY; 44 /** Socket for communicating with server. */ 45 protected Socket serverSocket = null; 46 47 /** Stream for printing to the server. */ 48 public PrintStream serverOutput; 49 50 /** Buffered stream for reading replies from server. */ 51 public InputStream serverInput; 52 53 protected static int defaultSoTimeout; 54 protected static int defaultConnectTimeout; 55 56 protected int readTimeout = -1; 57 protected int connectTimeout = -1; 58 /* Name of encoding to use for output */ 59 protected static String encoding; 60 61 static { 62 final int vals[] = {0, 0}; 63 final String encs[] = { null }; 64 65 AccessController.doPrivileged( 66 new PrivilegedAction<Void>() { 67 public Void run() { 68 vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue(); 69 vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue(); 70 encs[0] = System.getProperty("file.encoding", "ISO8859_1"); 71 return null; 72 } 73 }); 74 if (vals[0] == 0) 75 defaultSoTimeout = -1; 76 else 77 defaultSoTimeout = vals[0]; 78 79 if (vals[1] == 0) 80 defaultConnectTimeout = -1; 81 else 82 defaultConnectTimeout = vals[1]; 83 84 85 encoding = encs[0]; 86 try { 87 if (!isASCIISuperset (encoding)) { 88 encoding = "ISO8859_1"; 89 } 90 } catch (Exception e) { 91 encoding = "ISO8859_1"; 92 } 93 } 94 95 96 /** 97 * Test the named character encoding to verify that it converts ASCII 98 * characters correctly. We have to use an ASCII based encoding, or else 99 * the NetworkClients will not work correctly in EBCDIC based systems. 100 * However, we cannot just use ASCII or ISO8859_1 universally, because in 101 * Asian locales, non-ASCII characters may be embedded in otherwise 102 * ASCII based protocols (eg. HTTP). The specifications (RFC2616, 2398) 103 * are a little ambiguous in this matter. For instance, RFC2398 [part 2.1] 104 * says that the HTTP request URI should be escaped using a defined 215 /** Return server connection status */ 216 public boolean serverIsOpen() { 217 return serverSocket != null; 218 } 219 220 /** Create connection with host <i>host</i> on port <i>port</i> */ 221 public NetworkClient(String host, int port) throws IOException { 222 openServer(host, port); 223 } 224 225 public NetworkClient() {} 226 227 public void setConnectTimeout(int timeout) { 228 connectTimeout = timeout; 229 } 230 231 public int getConnectTimeout() { 232 return connectTimeout; 233 } 234 235 public void setReadTimeout(int timeout) { 236 if (serverSocket != null && timeout >= 0) { 237 try { 238 serverSocket.setSoTimeout(timeout); 239 } catch(IOException e) { 240 // We tried... 241 } 242 } 243 readTimeout = timeout; 244 } 245 246 public int getReadTimeout() { 247 return readTimeout; 248 } 249 } | 23 * questions. 24 */ 25 package sun.net; 26 27 import java.io.*; 28 import java.net.Socket; 29 import java.net.InetAddress; 30 import java.net.InetSocketAddress; 31 import java.net.UnknownHostException; 32 import java.net.Proxy; 33 import java.util.Arrays; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 37 /** 38 * This is the base class for network clients. 39 * 40 * @author Jonathan Payne 41 */ 42 public class NetworkClient { 43 /* Default value of read timeout, if not specified (infinity) */ 44 public static final int DEFAULT_READ_TIMEOUT = -1; 45 46 /* Default value of connect timeout, if not specified (infinity) */ 47 public static final int DEFAULT_CONNECT_TIMEOUT = -1; 48 49 protected Proxy proxy = Proxy.NO_PROXY; 50 /** Socket for communicating with server. */ 51 protected Socket serverSocket = null; 52 53 /** Stream for printing to the server. */ 54 public PrintStream serverOutput; 55 56 /** Buffered stream for reading replies from server. */ 57 public InputStream serverInput; 58 59 protected static int defaultSoTimeout; 60 protected static int defaultConnectTimeout; 61 62 protected int readTimeout = DEFAULT_READ_TIMEOUT; 63 protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT; 64 /* Name of encoding to use for output */ 65 protected static String encoding; 66 67 static { 68 final int vals[] = {0, 0}; 69 final String encs[] = { null }; 70 71 AccessController.doPrivileged( 72 new PrivilegedAction<Void>() { 73 public Void run() { 74 vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue(); 75 vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue(); 76 encs[0] = System.getProperty("file.encoding", "ISO8859_1"); 77 return null; 78 } 79 }); 80 if (vals[0] != 0) { 81 defaultSoTimeout = vals[0]; 82 } 83 if (vals[1] != 0) { 84 defaultConnectTimeout = vals[1]; 85 } 86 87 encoding = encs[0]; 88 try { 89 if (!isASCIISuperset (encoding)) { 90 encoding = "ISO8859_1"; 91 } 92 } catch (Exception e) { 93 encoding = "ISO8859_1"; 94 } 95 } 96 97 98 /** 99 * Test the named character encoding to verify that it converts ASCII 100 * characters correctly. We have to use an ASCII based encoding, or else 101 * the NetworkClients will not work correctly in EBCDIC based systems. 102 * However, we cannot just use ASCII or ISO8859_1 universally, because in 103 * Asian locales, non-ASCII characters may be embedded in otherwise 104 * ASCII based protocols (eg. HTTP). The specifications (RFC2616, 2398) 105 * are a little ambiguous in this matter. For instance, RFC2398 [part 2.1] 106 * says that the HTTP request URI should be escaped using a defined 217 /** Return server connection status */ 218 public boolean serverIsOpen() { 219 return serverSocket != null; 220 } 221 222 /** Create connection with host <i>host</i> on port <i>port</i> */ 223 public NetworkClient(String host, int port) throws IOException { 224 openServer(host, port); 225 } 226 227 public NetworkClient() {} 228 229 public void setConnectTimeout(int timeout) { 230 connectTimeout = timeout; 231 } 232 233 public int getConnectTimeout() { 234 return connectTimeout; 235 } 236 237 /** 238 * Sets the read timeout. 239 * 240 * Note: Public URLConnection (and protocol specific implementations) 241 * protect against negative timeout values being set. This implemenation, 242 * and protocol specific implementations, use -1 to represent the default 243 * read timeout. 244 * 245 * This method may be invoked with the default timeout value when the 246 * protocol handler is trying to reset the timeout after doing a 247 * potentially blocking internal operation, e.g. cleaning up unread 248 * response data, buffering error stream response data, etc 249 */ 250 public void setReadTimeout(int timeout) { 251 if (timeout == DEFAULT_READ_TIMEOUT) 252 timeout = defaultSoTimeout; 253 254 if (serverSocket != null && timeout >= 0) { 255 try { 256 serverSocket.setSoTimeout(timeout); 257 } catch(IOException e) { 258 // We tried... 259 } 260 } 261 readTimeout = timeout; 262 } 263 264 public int getReadTimeout() { 265 return readTimeout; 266 } 267 } |