Java Utililty Methods InputStream Read

List of utility methods to do InputStream Read

Description

The list of methods to do InputStream Read are organized into topic(s).

Method

InputStreamgetInputStreamFile(String URLString)
Get the InputStream from a File
final String eLabel = "ResourceRequestUtils.getInputStreamFile: ";
OutputStream os = null;
try {
    File file = new File(URLString);
    if ((!file.exists()) || (!file.isFile())) {
        throw new Exception("File resource does not exist: " + URLString);
    return new FileInputStream(file);
...
InputStreamgetInputStreamFor(String path, Class clazz)
get Input Stream For
return clazz.getClassLoader().getResourceAsStream(path);
InputStreamgetInputStreamForFileAbsolutePath(String _path)
Will check that the path is absolute and is pointing to an existing file.
InputStream result = null;
if (null != _path) {
    File file = new File(_path);
    if (file.exists() && file.isAbsolute() && !file.isDirectory()) {
        try {
            result = new FileInputStream(file);
        } catch (FileNotFoundException e) {
return result;
InputStreamgetInputStreamForResource(String name)
get Input Stream For Resource
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return classLoader.getResourceAsStream(name);
InputStreamgetInputStreamForResource(String relativePath)
get Input Stream For Resource
String resourcePath = null;
InputStream is = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
resourcePath = relativePath.replace("\\", "/");
is = loader.getResourceAsStream(resourcePath);
if (is == null) {
    throw new RuntimeException(
            "Unable to create an input stream as the following resoure was mot found from the classpath "
...
InputStreamgetInputStreamFromAbsolutePath(String resource)
get Input Stream From Absolute Path
try {
    return new FileInputStream(resource);
} catch (IOException e) {
    return null;
InputStreamgetInputStreamFromClassPath(String filename)
get Input Stream From Class Path
return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
InputStreamgetInputStreamFromFile(File pFile)
Gets an InputStream for the contents of a file - if the file is .gz it will return a stream of the decompressed contents
if (!pFile.exists())
    return null;
try {
    return new GZIPInputStream(new FileInputStream(pFile));
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    try {
...
InputStreamgetInputStreamFromFile(final File file)
Reads a file an returns an InputStream from it.
return new FileInputStream(file);
InputStreamgetInputStreamFromZip(ZipInputStream zis, String filename)
Read file from ZIP
ZipEntry zi = null;
InputStream is = null;
while ((zi = zis.getNextEntry()) != null) {
    if (filename.equals(zi.getName())) {
        is = zis;
        break;
return is;