Java URL to InputStream getInputStream(URLConnection connection)

Here you can find the source of getInputStream(URLConnection connection)

Description

gets the input stream from an url connection

License

Open Source License

Parameter

Parameter Description
connection connection which supplies the stream

Return

connection.getInputStream() which may be wrapped in a decoder

Declaration

public static InputStream getInputStream(URLConnection connection) throws IOException 

Method Source Code

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

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

import java.net.URLConnection;

import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

public class Main {
    /**// w  ww. java2  s . co  m
     * gets the input stream from an url connection
     * @param connection connection which supplies the stream
     * @return connection.getInputStream() which may be wrapped in a decoder
     */
    public static InputStream getInputStream(URLConnection connection) throws IOException {
        InputStream in = connection.getInputStream();
        String encoding = connection.getContentEncoding();
        if (encoding != null) {
            if (encoding.equals("deflate"))
                return new InflaterInputStream(in);
            else if (encoding.equals("gzip"))
                return new GZIPInputStream(in);
            else
                throw new IOException("unexpected encoding: " + encoding);
        }
        return in;
    }
}

Related

  1. getInputStream(URL url)
  2. getInputStream(URL url)
  3. getInputStream(URL url)
  4. getInputStream(URL urlFile)
  5. getInputStream(URLConnection c)
  6. getInputStream(URLConnection paramURLConnection)
  7. getInputStreamForURL(URL url)
  8. getInputStreamForURL(URL url)
  9. getInputStreamForURL(URL url)