equal
deleted
inserted
replaced
38 * This is the base class for network clients. |
38 * This is the base class for network clients. |
39 * |
39 * |
40 * @author Jonathan Payne |
40 * @author Jonathan Payne |
41 */ |
41 */ |
42 public class NetworkClient { |
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 |
43 protected Proxy proxy = Proxy.NO_PROXY; |
49 protected Proxy proxy = Proxy.NO_PROXY; |
44 /** Socket for communicating with server. */ |
50 /** Socket for communicating with server. */ |
45 protected Socket serverSocket = null; |
51 protected Socket serverSocket = null; |
46 |
52 |
47 /** Stream for printing to the server. */ |
53 /** Stream for printing to the server. */ |
51 public InputStream serverInput; |
57 public InputStream serverInput; |
52 |
58 |
53 protected static int defaultSoTimeout; |
59 protected static int defaultSoTimeout; |
54 protected static int defaultConnectTimeout; |
60 protected static int defaultConnectTimeout; |
55 |
61 |
56 protected int readTimeout = -1; |
62 protected int readTimeout = DEFAULT_READ_TIMEOUT; |
57 protected int connectTimeout = -1; |
63 protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT; |
58 /* Name of encoding to use for output */ |
64 /* Name of encoding to use for output */ |
59 protected static String encoding; |
65 protected static String encoding; |
60 |
66 |
61 static { |
67 static { |
62 final int vals[] = {0, 0}; |
68 final int vals[] = {0, 0}; |
69 vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue(); |
75 vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue(); |
70 encs[0] = System.getProperty("file.encoding", "ISO8859_1"); |
76 encs[0] = System.getProperty("file.encoding", "ISO8859_1"); |
71 return null; |
77 return null; |
72 } |
78 } |
73 }); |
79 }); |
74 if (vals[0] == 0) |
80 if (vals[0] != 0) { |
75 defaultSoTimeout = -1; |
|
76 else |
|
77 defaultSoTimeout = vals[0]; |
81 defaultSoTimeout = vals[0]; |
78 |
82 } |
79 if (vals[1] == 0) |
83 if (vals[1] != 0) { |
80 defaultConnectTimeout = -1; |
|
81 else |
|
82 defaultConnectTimeout = vals[1]; |
84 defaultConnectTimeout = vals[1]; |
83 |
85 } |
84 |
86 |
85 encoding = encs[0]; |
87 encoding = encs[0]; |
86 try { |
88 try { |
87 if (!isASCIISuperset (encoding)) { |
89 if (!isASCIISuperset (encoding)) { |
88 encoding = "ISO8859_1"; |
90 encoding = "ISO8859_1"; |
230 |
232 |
231 public int getConnectTimeout() { |
233 public int getConnectTimeout() { |
232 return connectTimeout; |
234 return connectTimeout; |
233 } |
235 } |
234 |
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 */ |
235 public void setReadTimeout(int timeout) { |
250 public void setReadTimeout(int timeout) { |
|
251 if (timeout == DEFAULT_READ_TIMEOUT) |
|
252 timeout = defaultSoTimeout; |
|
253 |
236 if (serverSocket != null && timeout >= 0) { |
254 if (serverSocket != null && timeout >= 0) { |
237 try { |
255 try { |
238 serverSocket.setSoTimeout(timeout); |
256 serverSocket.setSoTimeout(timeout); |
239 } catch(IOException e) { |
257 } catch(IOException e) { |
240 // We tried... |
258 // We tried... |