Java URL to InputStream getInputStream(URL url)

Here you can find the source of getInputStream(URL url)

Description

Returns an input stream for the given URL.

License

Open Source License

Parameter

Parameter Description
url the url to open as a stream

Exception

Parameter Description
IOException if there is trouble creating the stream

Return

the stream associated with the URL

Declaration

public static InputStream getInputStream(URL url) throws IOException 

Method Source Code

//package com.java2s;
/**//  w  w w  . jav a  2  s  . c o  m
 * Copyright 2001 Sun Microsystems, Inc.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 */

import java.net.URL;
import java.io.FileInputStream;

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

public class Main {
    /** Returns an input stream for the given URL. If the URL is pointing to a local file, returns a file input stream
     * suitable for MemoryMapped IO, otherwise, returns a buffered input stream.
     * @param url the url to open as a stream
     * @return the stream associated with the URL
     * @throws IOException if there is trouble creating the stream */
    public static InputStream getInputStream(URL url) throws IOException {
        if (url.getProtocol().equals("file")) {
            return new FileInputStream(url.getFile());
        } else {
            return url.openStream();
        }
    }
}

Related

  1. getInputStream(String url, int timeout)
  2. getInputStream(URL sourceURL)
  3. getInputStream(URL url)
  4. getInputStream(URL url)
  5. getInputStream(URL url)
  6. getInputStream(URL urlFile)
  7. getInputStream(URLConnection c)
  8. getInputStream(URLConnection connection)
  9. getInputStream(URLConnection paramURLConnection)