code: public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task,
          StreamErrorHandler errorHandler) {
          try {
              task.execute(stream);
          } catch (Throwable t) {
              errorHandler.handle(t);
          } finally {
              if (stream != null) {
                  try {
                      stream.close();
                  } catch (final IOException ignore) {
                      if (log.isLoggable(Level.FINER)) {
                          log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring");
                      }
                  }
              }
          }
      }
docs: Helper method to run a specified task and automatically handle the closing of the stream.
  
  @param <S>
  @param task
  @param errorHandler