Created
April 17, 2020 14:41
-
-
Save hetzge/01c54bd04630e8e7d1dabfc8fae9fe1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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