Reading a file, reading a resource in the jar file, reading a URL.
You would think there would be a method for this in Java, well no there isn't.

You need to develop your own code to read the input stream.

So if you don't have commons-io and its IOUtils or Ant and its FileUtils in you classpath, here is the code:

public final static byte[] readBytes(InputStream stream, int bufferSize) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
        byte[] b = new byte[bufferSize];
        int len = -1;
        while ((len = stream.read(b)) != -1) {
            baos.write(b, 0, len);
        }
        stream.close();
        return baos.toByteArray();
    } catch(IOException ioe) {
        return new byte[0];
    }
}


Your Option (Login or Post by anonymous)