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

StringgetFileContent(InputStream in)
Helper method to obtain the content of a source file.
String content = "";
BufferedReader inReader = new BufferedReader(new InputStreamReader(in));
while (inReader.ready()) {
    String line = inReader.readLine();
    content = content + line + "\n";
return content;
StringgetFileContent(InputStream inputStream)
get File Content
byte[] bytes = new byte[28];
try {
    inputStream.read(bytes, 0, 28);
} catch (IOException e) {
    throw new RuntimeException("read file content error!");
return bytesToHexString(bytes);
StringgetFileContent(InputStream is)
There is a much cleaner implementation of doing this, but good enough since it is only used for testing
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer buff = new StringBuffer();
while ((line = br.readLine()) != null) {
    buff.append(line + "\n");
return buff.toString();
StringgetFileContent(InputStream stream)
get File Content
InputStreamReader reader = new InputStreamReader(stream);
char[] arr = new char[8 * 1024]; 
StringBuilder buf = new StringBuilder();
int numChars;
while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
    buf.append(arr, 0, numChars);
return buf.toString().intern(); 
...
InputStreamgetInputStream(Class clazz)
get Input Stream
InputStream is = null;
is = clazz.getResourceAsStream(DEFAULT_STRATEGIES_PATH);
if (is == null) {
    throw new FileNotFoundException(
            DEFAULT_STRATEGIES_PATH + " cannot be opened because it does not exist");
return is;
InputStreamgetInputStream(Class ownerClass, String subName, String extension)
Opens an InputStream for a class-associated resource.
String localName;
if (subName == null) {
    localName = ownerClass.getSimpleName() + '.' + extension;
} else {
    localName = ownerClass.getSimpleName() + '_' + subName + '.' + extension;
return ownerClass.getResourceAsStream(localName);
InputStreamgetInputstream(File compressedFile)
Gets the compressed input stream for an image
try {
    return new GZIPInputStream(new FileInputStream(compressedFile));
catch (Exception e) {
    e.printStackTrace();
return null;
InputStreamgetInputStream(JarFile file, ZipEntry entry)
get Input Stream
try {
    return file.getInputStream(entry);
} catch (IOException e) {
    throw new IllegalStateException(e);
InputStreamgetInputStream(Object caller, String name)
Return an appropriate InputStream for the specified test file (which must be inside our current package.
return (caller.getClass().getResourceAsStream("/org/apache/commons/digester3/plugins/" + name));
InputStreamgetInputStreamAsResource(Class clazz, String fileName)
get Input Stream As Resource
InputStream stream = clazz.getResourceAsStream(fileName);
return stream;