Java Resource File toStream(String resource)

Here you can find the source of toStream(String resource)

Description

to Stream

License

Open Source License

Declaration

private static InputStream toStream(String resource)
            throws MalformedURLException, IOException 

Method Source Code

//package com.java2s;
/**//from   w ww  .j a  va2  s  . com
 * Copyright (c) 2008 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static final String PROTOCOL_DELIM = "://";
    public static final int PROTOCOL_DELIM_LEN = PROTOCOL_DELIM.length();
    public static final String[] URL_PROTOCOLS = new String[] { "http",
            "https", "dav", "file", "davs", "webdav", "webdavs",
            "dav+http", "dav+https" };

    private static InputStream toStream(String resource)
            throws MalformedURLException, IOException {
        if (resource == null)
            return null;

        int ind = resource.indexOf(PROTOCOL_DELIM);

        if (ind > 1) {
            String protocol = resource.substring(0, ind);
            resource = resource.substring(ind + PROTOCOL_DELIM_LEN);

            for (int i = 0; i < URL_PROTOCOLS.length; i++) {
                String p = URL_PROTOCOLS[i];

                if (protocol.regionMatches(true, 0, p, 0, p.length()))
                    return new URL(p + PROTOCOL_DELIM + resource)
                            .openStream();
            }
        }

        return new FileInputStream(new File(resource));
    }
}

Related

  1. put(String resource, Map headers, String data)
  2. resourceExists(String s)
  3. runtimeResourceLocation(String src)
  4. sanitizeResource(String inputResource)
  5. toFile(Class c, String resource)
  6. unpackDeps(final String resource, final ClassLoader cl)