Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/sun/net/NetworkClient.java
          +++ new/src/share/classes/sun/net/NetworkClient.java
↓ open down ↓ 32 lines elided ↑ open up ↑
  33   33  import java.util.Arrays;
  34   34  import java.security.AccessController;
  35   35  import java.security.PrivilegedAction;
  36   36  
  37   37  /**
  38   38   * This is the base class for network clients.
  39   39   *
  40   40   * @author      Jonathan Payne
  41   41   */
  42   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   49      protected Proxy     proxy = Proxy.NO_PROXY;
  44   50      /** Socket for communicating with server. */
  45   51      protected Socket    serverSocket = null;
  46   52  
  47   53      /** Stream for printing to the server. */
  48   54      public PrintStream  serverOutput;
  49   55  
  50   56      /** Buffered stream for reading replies from server. */
  51   57      public InputStream  serverInput;
  52   58  
  53   59      protected static int defaultSoTimeout;
  54   60      protected static int defaultConnectTimeout;
  55   61  
  56      -    protected int readTimeout = -1;
  57      -    protected int connectTimeout = -1;
       62 +    protected int readTimeout = DEFAULT_READ_TIMEOUT;
       63 +    protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
  58   64      /* Name of encoding to use for output */
  59   65      protected static String encoding;
  60   66  
  61   67      static {
  62   68          final int vals[] = {0, 0};
  63   69          final String encs[] = { null };
  64   70  
  65   71          AccessController.doPrivileged(
  66   72                  new PrivilegedAction<Void>() {
  67   73                      public Void run() {
  68   74                          vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue();
  69   75                          vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue();
  70   76                          encs[0] = System.getProperty("file.encoding", "ISO8859_1");
  71   77                          return null;
  72   78              }
  73   79          });
  74      -        if (vals[0] == 0)
  75      -            defaultSoTimeout = -1;
  76      -        else
       80 +        if (vals[0] != 0) {
  77   81              defaultSoTimeout = vals[0];
  78      -
  79      -        if (vals[1] == 0)
  80      -            defaultConnectTimeout = -1;
  81      -        else
       82 +        }
       83 +        if (vals[1] != 0) {
  82   84              defaultConnectTimeout = vals[1];
       85 +        }
  83   86  
  84      -
  85   87          encoding = encs[0];
  86   88          try {
  87   89              if (!isASCIISuperset (encoding)) {
  88   90                  encoding = "ISO8859_1";
  89   91              }
  90   92          } catch (Exception e) {
  91   93              encoding = "ISO8859_1";
  92   94          }
  93   95      }
  94   96  
↓ open down ↓ 130 lines elided ↑ open up ↑
 225  227      public NetworkClient() {}
 226  228  
 227  229      public void setConnectTimeout(int timeout) {
 228  230          connectTimeout = timeout;
 229  231      }
 230  232  
 231  233      public int getConnectTimeout() {
 232  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  250      public void setReadTimeout(int timeout) {
      251 +        if (timeout == DEFAULT_READ_TIMEOUT)
      252 +            timeout = defaultSoTimeout;
      253 +
 236  254          if (serverSocket != null && timeout >= 0) {
 237  255              try {
 238  256                  serverSocket.setSoTimeout(timeout);
 239  257              } catch(IOException e) {
 240  258                  // We tried...
 241  259              }
 242  260          }
 243  261          readTimeout = timeout;
 244  262      }
 245  263  
 246  264      public int getReadTimeout() {
 247  265          return readTimeout;
 248  266      }
 249  267  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX