Skip to content

Instantly share code, notes, and snippets.

@alterakey
Created December 10, 2011 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alterakey/1454764 to your computer and use it in GitHub Desktop.
Save alterakey/1454764 to your computer and use it in GitHub Desktop.
WatchingInputStream: Progress-reporting InputStream wrapper
/**
* WatchingInputStream: Progress-reporting InputStream wrapper.
* This file is in public domain.
*/
public class WatchingInputStream extends FilterInputStream
{
public interface ProgressListener
{
void onAdvance(long at, long length);
}
private int marked = 0;
private long position = 0;
private ProgressListener listener;
public WatchingInputStream(InputStream in, ProgressListener listener)
{
super(in);
this.listener = listener;
}
@Override
public int read(byte[] buffer, int offset, int count) throws IOException
{
int advanced = super.read(buffer, offset, count);
this.position += advanced;
this.report();
return advanced;
}
@Override
public synchronized void reset() throws IOException
{
super.reset();
this.position = this.marked;
}
@Override
public synchronized void mark(int readlimit)
{
super.mark(readlimit);
this.marked = readlimit;
}
@Override
public long skip(long byteCount) throws IOException
{
long advanced = super.skip(byteCount);
this.position += advanced;
this.report();
return advanced;
}
private void report()
{
if (this.listener == null)
return;
try
{
this.listener.onAdvance(this.position, this.position + this.in.available());
}
catch (IOException e)
{
this.listener.onAdvance(this.position, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment