Skip to content

Instantly share code, notes, and snippets.

@hetzge
Created April 17, 2020 14:41
Show Gist options
  • Select an option

  • Save hetzge/01c54bd04630e8e7d1dabfc8fae9fe1a to your computer and use it in GitHub Desktop.

Select an option

Save hetzge/01c54bd04630e8e7d1dabfc8fae9fe1a to your computer and use it in GitHub Desktop.
package com.zf.v2x.mapmanager.api.st;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Consumer;
public class ProgressInputStream extends FilterInputStream {
private volatile long totalReadByteCount;
private final Consumer<Long> progressConsumer;
public ProgressInputStream(InputStream inputStream, Consumer<Long> progressConsumer) {
super(inputStream);
this.progressConsumer = progressConsumer;
}
public long getTotalNumBytesRead() {
return totalReadByteCount;
}
@Override
public int read() throws IOException {
int b = super.read();
updateProgress(1);
return b;
}
@Override
public int read(byte[] b) throws IOException {
return (int) updateProgress(super.read(b));
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return (int) updateProgress(super.read(b, off, len));
}
@Override
public long skip(long n) throws IOException {
return updateProgress(super.skip(n));
}
@Override
public void mark(int readlimit) {
throw new UnsupportedOperationException();
}
@Override
public void reset() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean markSupported() {
return false;
}
private long updateProgress(long readByteCount) {
if (readByteCount > 0) {
this.totalReadByteCount += readByteCount;
progressConsumer.accept(this.totalReadByteCount);
}
return readByteCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment