Java URL to InputStream openStream(URL url, int connectTimeout, int readTimeout)

Here you can find the source of openStream(URL url, int connectTimeout, int readTimeout)

Description

Open a stream for the given URL with custom timeouts

License

Apache License

Exception

Parameter Description
IOException an exception

Declaration

public static InputStream openStream(URL url, int connectTimeout, int readTimeout) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w  w  . j av  a  2s .co m
 *    Copyright 2010-2012 University of Toronto
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import java.io.*;
import java.net.*;

public class Main {
    private static final int CONNECT_TIMEOUT = 30000;
    private static final int READ_TIMEOUT = 30000;

    /**
     * Open a stream for the given URL with the CONNECT_TIMEOUT and READ_TIMEOUT.
     * @throws IOException
     */
    public static InputStream openStream(URL url) throws IOException {
        return openStream(url, CONNECT_TIMEOUT, READ_TIMEOUT);
    }

    /**
     * Open a stream for the given URL with custom timeouts
     * @throws IOException
     */
    public static InputStream openStream(URL url, int connectTimeout, int readTimeout) throws IOException {
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(connectTimeout);
        conn.setReadTimeout(readTimeout);
        return conn.getInputStream();
    }
}

Related

  1. openStream(URL url)
  2. openStream(URL url)
  3. openStream(URL url)
  4. openStream(URL url)
  5. openStream(URL url)
  6. openStreamUseCache(URL url)
  7. openURL(String url)
  8. openURLStream(String in)