package de.ivu.elect.business.konfiguration.entity;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamMitGroesse {

  private final InputStream inputStream;

  private final Integer size;

  private StreamCloseListener streamCloseListener;

  public InputStreamMitGroesse(InputStream inputStream, Integer size) {
    this.inputStream = inputStream;
    this.size = size;
  }

  public InputStream getInputStream() {
    if (streamCloseListener == null) {
      return inputStream;
    }
    return new FilterInputStream(inputStream) {
      @Override
      public void close() throws IOException {
        try {
          super.close();
        } finally {
          streamCloseListener.closeEvent();
        }
      }
    };
  }

  public Integer getSize() {
    return size;
  }

  public void setStreamCloseListener(StreamCloseListener streamCloseListener) {
    this.streamCloseListener = streamCloseListener;
  }
}
