Java URL Download get(URL host, String endpoint, String customer, String name, String version, OutputStream out)

Here you can find the source of get(URL host, String endpoint, String customer, String name, String version, OutputStream out)

Description

get

License

Apache License

Declaration

static int get(URL host, String endpoint, String customer, String name, String version, OutputStream out)
            throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w ww .  j  a  va  2s. c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    private static final int COPY_BUFFER_SIZE = 4096;

    static int get(URL host, String endpoint, String customer, String name, String version, OutputStream out)
            throws IOException {
        int responseCode;

        URL url = new URL(host, endpoint + "?customer=" + customer + "&name=" + name + "&version=" + version);

        InputStream input = null;
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        try {
            responseCode = connection.getResponseCode();
            input = connection.getInputStream();

            copy(input, out);
            out.flush();
        } catch (IOException e) {
            responseCode = handleIOException(connection);
        } finally {
            closeSilently(input);
            closeSilently(connection);
        }

        return responseCode;
    }

    static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[COPY_BUFFER_SIZE];
        int bytes = in.read(buffer);
        while (bytes != -1) {
            out.write(buffer, 0, bytes);
            bytes = in.read(buffer);
        }
    }

    /**
     * @see http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html
     */
    static int handleIOException(HttpURLConnection conn) {
        int respCode = -1;
        try {
            respCode = conn.getResponseCode();
            flushStream(conn.getErrorStream());
        } catch (IOException ex) {
            // deal with the exception
        }
        return respCode;
    }

    static void closeSilently(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException exception) {
                // Ignore...
            }
        }
    }

    static void closeSilently(HttpURLConnection resource) {
        if (resource != null) {
            resource.disconnect();
        }
    }

    static void flushStream(InputStream is) {
        byte[] buf = new byte[COPY_BUFFER_SIZE];
        try {
            while (is.read(buf) > 0) {
                // Ignore...
            }
        } catch (IOException ex) {
            // deal with the exception
        } finally {
            closeSilently(is);
        }
    }
}

Related

  1. get(String url, Map headers)
  2. get(String url, Map params)
  3. get(String url, String encoding)
  4. get(String urlStr)
  5. get(String urlString)
  6. get(URL url)
  7. get(URL url, String acceptType)