Java Utililty Methods InputStream Create

List of utility methods to do InputStream Create

Description

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

Method

InputStreamasInputStream(byte[] bytes)
as Input Stream
return new ByteArrayInputStream(bytes);
InputStreamasInputStream(File file)
Return an input stream to the file.
return new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
InputStreamasInputStream(Object content)
Returns an input stream for the specified content.
if (content instanceof byte[]) {
    return new ByteArrayInputStream((byte[]) content);
} else if (content instanceof File) {
    return new FileInputStream((File) content);
} else {
    return new ByteArrayInputStream(content.toString().getBytes("UTF-8"));
InputStreamasInputStream(String str)
as Input Stream
try {
    return new ByteArrayInputStream(str.getBytes(CHARSET));
} catch (UnsupportedEncodingException e) {
    assert false;
return null;
InputStreamgetInputStream(File f)
Gets the input stream for a given file (for tar archives)
InputStream is = new FileInputStream(f);
try {
    return new GZIPInputStream(is);
} catch (IOException e) {
    return new FileInputStream(f);
InputStreamgetInputStream(File jarFile, String fileName)
get Input Stream
ZipFile zipFile = new ZipFile(jarFile);
ZipEntry zipEntry = zipFile.getEntry(fileName);
return zipFile.getInputStream(zipEntry);
InputStreamgetInputStream(File tarFile)
If tarFile's extension is simply tar, then returns a new FileInputStream.
String name = tarFile.getName().toLowerCase();
if (name.endsWith(".tar")) {
    return new FileInputStream(tarFile);
} else if (name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
    return new GZIPInputStream(new FileInputStream(tarFile));
} else {
    throw new IllegalArgumentException(
            "tarFile = " + tarFile.getPath() + " has an invalid extension for a tar or tar/gzip file");
...
InputStreamgetInputStream(File tarFile)
get Input Stream
return (new FileInputStream(tarFile));
InputStreamgetInputStream(File TheFile)
get Input Stream
validateReadableFile(TheFile);
try {
    InputStream in = new FileInputStream(TheFile);
    return (in);
} catch (FileNotFoundException ex) {
    String Fullpath = TheFile.getAbsolutePath();
    throw new IllegalStateException(
            "Requested File '" + Fullpath + "' does not exist -  but has been tested - HUH???");
...
InputStreamgetInputStream(FileInputStream fileInput)
get Input Stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = -1;
InputStream inputStream = null;
try {
    while ((n = fileInput.read(buffer)) != -1) {
        baos.write(buffer, 0, n);
    byte[] byteArray = baos.toByteArray();
    inputStream = new ByteArrayInputStream(byteArray);
    return inputStream;
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return null;
} catch (IOException e) {
    e.printStackTrace();
    return null;
} finally {
    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();