Given a Java InputStream, how can I determine the current offset in the stream?

You’ll need to follow the Decorator pattern established in java.io to implement this. Let’s give it a try here: import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; public final class PositionInputStream extends FilterInputStream { private long pos = 0; private long mark = 0; public PositionInputStream(InputStream in) { super(in); } /** * <p>Get the stream position.</p> * … Read more

Closing inputstreams in Java

By convention, wrapper streams (which wrap existing streams) close the underlying stream when they are closed, so only have to close bufferedreader in your example. Also, it is usually harmless to close an already closed stream, so closing all 3 streams won’t hurt.

ClassLoader getResourceAsStream returns null

If it’s in the same package use InputStream is = Driver.class.getResourceAsStream(“myconfig.txt”); The way you have it InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(“myconfig.txt”); It’s looking for the file in the root of the classpath. You could use InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(“com/me/myapp/myconfig.txt”); The rules for searching are explained in the javadoc of ClassLoader#getResource(String) and the javadoc of Class#getResource(String).

Looping Error on Android Emulator

Android is trying to listen on the microphone, which isn’t available on the emulator, so it fills logcat with useless stack traces. To stop this, go to the Settings app in Android, and click: Apps and notifications App permissions Microphone Then disallow microphone use for all the apps.

Wrapping a ByteBuffer with an InputStream

There seem to be some bugs with the implementation referred to by Thilo, and also copy and pasted on other sites verbatim: ByteBufferBackedInputStream.read() returns a sign extended int representation of the byte it reads, which is wrong (value should be in range [-1..255]) ByteBufferBackedInputStream.read(byte[], int, int) does not return -1 when there are no bytes … Read more

How to Cache InputStream for Multiple Use

you can decorate InputStream being passed to POIFSFileSystem with a version that when close() is called it respond with reset(): class ResetOnCloseInputStream extends InputStream { private final InputStream decorated; public ResetOnCloseInputStream(InputStream anInputStream) { if (!anInputStream.markSupported()) { throw new IllegalArgumentException(“marking not supported”); } anInputStream.mark( 1 << 24); // magic constant: BEWARE decorated = anInputStream; } @Override … Read more