Here you can find the source of getInputStream(HttpURLConnection connection)
urlConnection
encoding.
Parameter | Description |
---|---|
connection | connection |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred.java.io.InputStream |
public static InputStream getInputStream(HttpURLConnection connection) throws IOException
//package com.java2s; /* =================================================== * Copyright 2013 Kroboth Software/*w w w.ja va 2 s. c o m*/ * * 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.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; public class Main { /** * Gets the correct {@link java.io.InputStream} based on * <code>urlConnection</code> encoding. * * @param connection * connection * @return inputstream from encoding. If not supported returns normal * @throws IOException * Signals that an I/O exception has occurred. * {@link java.io.InputStream} * @since SNC 1.0 */ public static InputStream getInputStream(HttpURLConnection connection) throws IOException { if (connection.getRequestMethod().equals("HEAD")) return null; String encoding = connection.getContentEncoding(); if (encoding == null) return connection.getInputStream(); else if (encoding.equalsIgnoreCase("gzip")) return new GZIPInputStream(connection.getInputStream()); else if (encoding.equalsIgnoreCase("deflate")) return new InflaterInputStream(connection.getInputStream(), new Inflater(true)); return null; } }