package io.github.duckasteroid.progress.utils;

import io.github.duckasteroid.progress.ProgressMonitor;
import java.io.IOException;
import java.io.InputStream;

/**
 * Wraps an input stream to report 1 unit of work for every byte {@link #read()}.
 * Clients should set the {@link ProgressMonitor#setSize(long)} prior to invoking for this
 * to report correctly.
 * When the stream is {@link #close() closed} the monitor is marked done
 */
public class InputStreamProgress extends InputStream {
  private final transient  InputStream delegate;
  private final transient ProgressMonitor monitor;

  public InputStreamProgress(InputStream delegate, ProgressMonitor monitor) {
    this.delegate = delegate;
    this.monitor = monitor;
  }

  @Override
  public int read() throws IOException {
    monitor.worked(1);
    return delegate.read();
  }

  @Override
  public void close() throws IOException {
    monitor.done();
    super.close();
  }
}

@@ -774,32 +774,139 @@
 s IOException %7B%0A
+    if (monitor.isCancelled()) %7B%0A      throw new IOException(%22Read operation cancelled by monitor%22);%0A    %7D%0A
     monitor.work

Throw Ex in stream if monitor cancelled