
com.gc.iotools.stream.is.CloseOnceInputStream Maven / Gradle / Ivy
Show all versions of easystream Show documentation
package com.gc.iotools.stream.is;
/*
* Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
* under the BSD License.
*/
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
* A CloseOnceInputStream
contains some other input stream, which
* it uses as its basic source of data. The class
* CloseOnceInputStream
pass all requests to the contained input
* stream, except the {@linkplain #close()} method that is passed only one
* time to the underlying stream.
*
*
* Multiple invocation of the close()
method will result in only
* one invocation of the same method on the underlying stream. This is useful
* with some buggy InputStream
that don't allow
* close()
to be called multiple times.
*
*
* @author dvd.smnt
* @since 1.2.6
* @param
* Type of the InputStream passed in the constructor.
* @version $Id: CloseOnceInputStream.java 576 2015-03-28 00:03:33Z gcontini $
*/
public class CloseOnceInputStream extends
FilterInputStream {
private int closeCount = 0;
/**
* Construct a CloseOnceInputStream
that forwards the calls
* to the source InputStream passed in the constructor.
*
* @param source
* original InputStream
*/
public CloseOnceInputStream(final T source) {
super(source);
}
/**
* {@inheritDoc}
*
*
* Multiple invocation of this method will result in only one invocation
* of the close()
on the underlying stream.
*
*/
@Override
public void close() throws IOException {
synchronized (this) {
this.closeCount++;
if (this.closeCount > 1) {
return;
}
}
super.close();
}
/**
* Returns the number of time that close was called.
*
* @see com.gc.iotools.stream.is.inspection.DiagnosticInputStream
* @return Number of times that close was called
*/
public int getCloseCount() {
return this.closeCount;
}
/**
*
* Returns the wrapped (original) InputStream
passed in the
* constructor.
*
*
* @return The original InputStream
passed in the constructor
*/
public T getWrappedInputStream() {
@SuppressWarnings("unchecked")
final T result = (T) super.in;
return result;
}
}