Java URL Connection getURLInputStream(final URL url)

Here you can find the source of getURLInputStream(final URL url)

Description

Get an InputStream from a URL.

License

Open Source License

Parameter

Parameter Description
url the url being accessed.

Exception

Parameter Description
IOException if an error occurs.

Return

an InputStream to content at URL.

Declaration

public static InputStream getURLInputStream(final URL url) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;

import java.io.ByteArrayInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

public class Main {
    /** Default connect timeout for url connections. */
    public static final int DEFAULT_URL_CONNECT_TIMEOUT = 15000;
    /** Default read timeout for url connections. */
    public static final int DEFAULT_URL_READ_TIMEOUT = 15000;

    /**//from   w w  w  . j  a  va 2 s.  c o  m
     * Get an InputStream from a URL. If URL is a HTTP url, attempts gzip
     * compression.
     *
     * @param url
     *            the url being accessed.
     * @return an InputStream to content at URL.
     * @throws IOException
     *             if an error occurs.
     */
    public static InputStream getURLInputStream(final URL url) throws IOException {
        return getURLInputStream(url, DEFAULT_URL_CONNECT_TIMEOUT, DEFAULT_URL_READ_TIMEOUT);
    }

    /**
     * Get an InputStream from a URL. If URL is a HTTP url, attempts gzip
     * compression.
     *
     * @param url
     *            the url being accessed.
     * @param connectTimeout allowed time in milliseconds before connection.
     * @param readTimeout allowed time in milliseconds before read.
     * @return an InputStream to content at URL.
     * @throws IOException
     *             if an error occurs.
     */
    public static InputStream getURLInputStream(final URL url, final int connectTimeout, final int readTimeout)
            throws IOException {
        InputStream in = null;

        // initialize connection
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("Accept-Encoding", "gzip");
        conn.setConnectTimeout(connectTimeout);
        conn.setReadTimeout(readTimeout);

        // connect
        conn.connect();

        // get response
        in = conn.getInputStream();
        String contentEncoding = conn.getContentEncoding();
        if (contentEncoding != null && contentEncoding.equals("gzip")) {
            in = new GZIPInputStream(in);
        }

        return in;
    }

    /**
     * Get an input stream for an Object if possible.
     *
     * @param obj
     *            an InputStream, File, byte[], or String.
     * @return an InputStream or null. If obj is a File, the stream is Buffered.
     * @throws IOException
     *             if an error occurs.
     * @throws IllegalArgumentException
     *             if obj is not an InputStream, URL, File, byte[], or String.
     */
    public static InputStream getInputStream(final Object obj) throws IOException, IllegalArgumentException {
        InputStream stream = null;
        byte[] bytes = null;

        if (obj instanceof InputStream) {
            stream = (InputStream) obj;
        } else if (obj instanceof URL) {
            stream = getURLInputStream((URL) obj);
        } else if (obj instanceof File) {
            stream = new BufferedInputStream(new FileInputStream((File) obj));
        } else if (obj instanceof byte[]) {
            bytes = (byte[]) obj;
        } else if (obj instanceof String) {
            bytes = ((String) obj).getBytes();
        } else {
            throw new IllegalArgumentException("Expected an InputStream, URL, File, byte[], or String");
        }

        if (bytes != null) {
            stream = new ByteArrayInputStream(bytes);
        }

        return stream;
    }
}

Related

  1. getURLContents(URL u, Map data)
  2. getURLContentsAsString(URL url)
  3. getURLDataList(URL StrurlStringing)
  4. getUrlEncoding(URLConnection connection)
  5. getURLForward(URL url)
  6. getUrlSource(String url)
  7. getURLStream(URL url, int level)
  8. getWebPageHtmlContent(String url)
  9. getWebsiteContents(URL url)