Java InputStream Create getInputStream(String url)

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

Description

return input stream from specified url

License

Open Source License

Parameter

Parameter Description
url specified url

Return

java.io.InputStream

Declaration

public static InputStream getInputStream(String url) 

Method Source Code

//package com.java2s;
/*/* w  ww  .j a v a2s . co  m*/
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.io.File;

import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ByteArrayInputStream;

public class Main {
    /**
     * return input  stream from specified url
     *
     * @param url specified url
     * @return <code>java.io.InputStream</code>
     */
    public static InputStream getInputStream(String url) {
        InputStream is = null;
        if (url != null) {
            if (url.indexOf(File.separator) != -1) {
                byte[] infoArray = readFile(url);
                if (infoArray != null) {
                    is = new ByteArrayInputStream(infoArray);
                }
            } else {
                is = Thread.currentThread().getContextClassLoader().getResourceAsStream(url);
            }
        }
        return is;
    }

    /**
     * read file to byte[] with specified file path
     *
     * @param filePath specified file path
     * @return <code>byte[]</code> file content
     */
    public static byte[] readFile(String filePath) {
        File infoFile = new File(filePath);
        byte[] result = null;
        if (infoFile.exists()) {
            result = new byte[(int) infoFile.length()];
            try {
                FileInputStream fis = new FileInputStream(infoFile);
                fis.read(result);
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

Related

  1. getInputStream(String path)
  2. getInputStream(String sPath)
  3. getInputStream(String str)
  4. getInputStream(String text)
  5. getInputStream(String thePath)
  6. getInputStream(String xmlStr)
  7. getInputStream(String... subpaths)
  8. getInputStream(String[] args)
  9. inputStream(final File file)