Java URL to InputStream openStream(URL url)

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

Description

open Stream

License

Apache License

Declaration

public static InputStream openStream(URL url) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w ww  .j  a v a2s .com*/
 * 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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static InputStream openStream(URL url) throws IOException {
        // Handle file:<relative path> which strictly speaking is not a valid file URL  
        File file = toFile(url);
        if (file != null) {
            return new FileInputStream(file);
        }
        URLConnection connection = url.openConnection();
        if (connection instanceof JarURLConnection) {
            // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041014
            connection.setUseCaches(false);
        }
        InputStream is = connection.getInputStream();
        return is;
    }

    /**
     * Returns the File object representing  the given URL.
     *
     * @param url
     * @return
     */
    public static File toFile(URL url) {
        if (url == null || !url.getProtocol().equals("file")) {
            return null;
        } else {
            String filename = url.getFile().replace('/', File.separatorChar);
            int pos = 0;
            while ((pos = filename.indexOf('%', pos)) >= 0) {
                if (pos + 2 < filename.length()) {
                    String hexStr = filename.substring(pos + 1, pos + 3);
                    char ch = (char) Integer.parseInt(hexStr, 16);
                    filename = filename.substring(0, pos) + ch + filename.substring(pos + 3);
                }
            }
            return new File(filename);
        }
    }
}

Related

  1. openFileOrURL(String fileNameOrUrl)
  2. openFileOrURL(String name)
  3. openInputStream(URL url)
  4. openStream(String urlStr)
  5. openStream(URL url)
  6. openStream(URL url)
  7. openStream(URL url)
  8. openStream(URL url)
  9. openStream(URL url)