class ReleasableInputStream extends InputStream {

    @Override
    public int read() throws IOException {
        try {
            int read = delegate.read();
            if (read < 0) {
                safeClose();
            }
            return read;
        } catch (IOException e) {
            safeClose();
            throw e;
        }
    }

    public  ReleasableInputStream(InputStream delegate, Runnable onClose);

}