Java URL Open openURL(URL source_url)

Here you can find the source of openURL(URL source_url)

Description

Note1: it is the caller's responsibility to close the returned InputStream.

License

Open Source License

Declaration

public static final InputStream openURL(URL source_url) throws IOException, SecurityException 

Method Source Code

//package com.java2s;
/* //from www.j a v a2 s .c o m
  This file is part of JIV.  
  Copyright (C) 2000, 2001 Chris A. Cocosco (crisco@bic.mni.mcgill.ca)
    
  JIV is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your
  option) any later version.
    
  JIV is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.
    
  You should have received a copy of the GNU General Public License
  along with JIV; if not, write to the Free Software Foundation, Inc.,
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, 
  or see http://www.gnu.org/copyleft/gpl.html
*/

import java.net.*;
import java.io.*;
import java.util.zip.*;

public class Main {
    /** 
    Note1: it is the caller's responsibility to close the returned
    InputStream.
        
    Note2: it's not _required_ to declare SecurityException (since
    it's a subclass of RuntimeException), but we do it for clarity
    -- this error is likely to happen when working with url-s, so
    it should be treated as a "checked exception" ...  
    */
    public static final InputStream openURL(URL source_url) throws IOException, SecurityException {

        if (false) {
            System.out.println("Util::openUrl( " + source_url + " )");
        }

        InputStream input_stream = null;

        URLConnection url_connection = source_url.openConnection();
        // NB: the connection is not yet opened at this point; it'll be 
        // actually done when calling getInputStream() below.
        url_connection.setUseCaches(true);

        if (url_connection instanceof HttpURLConnection) {
            HttpURLConnection http_conn = (HttpURLConnection) url_connection;
            if (http_conn.getResponseCode() != HttpURLConnection.HTTP_OK)
                throw new IOException(source_url.toString() + " : " + http_conn.getResponseCode() + " "
                        + http_conn.getResponseMessage());
        }
        input_stream = url_connection.getInputStream();
        if (source_url.toString().endsWith(".gz"))
            // use a larger decompression buffer than the 512 default
            input_stream = new GZIPInputStream(input_stream, 4096);

        // TODO: I once got a "IOException: server status 206" when loading
        // the applet via http ... but couldn't reproduce it ever since.
        // what went wrong?
        // (btw, 206 == HTTP_PARTIAL : 
        // "HTTP response code that means the partial request has been fulfilled") 

        return input_stream;
    }
}

Related

  1. openReader(String aUrl)
  2. openStream(String aURL)
  3. openURLConnection(final URL couchdbURL)