
com.gc.iotools.stream.reader.CloseShieldReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easystream Show documentation
Show all versions of easystream Show documentation
EasyStream is a small set of utilities for dealing with
streams (InputStreams
and OutputStreams).
The aim is to ease the use of
pipes when they're required.
Main features are:
* "Convert" an
OutputStream to an InputStream.
* Count the number of bytes read or
wrote to a given stream.
* While reading the data from an InputStream
copy it to a supplied
OutputStream.
* Read the content of an InputStream
multiple times or seek to a
definite position
The newest version!
package com.gc.iotools.stream.reader;
/*
* Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
* under the BSD License.
*/
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
/**
*
* A CloseShieldReader
wraps some other Reader
,
* which it uses as its basic source of data. The class
* CloseShieldReader
pass all requests to the contained stream,
* except the {@linkplain #close()} method that is not to the underlying
* stream.
*
*
* This class is typically used in cases where a Reader
needs to
* be passed to a component that wants to explicitly close the stream even if
* more input would still be available to other components.
*
*
* @author dvd.smnt
* @since 1.2.8
* @param
* Type of the Reader passed in the constructor.
* @version $Id: CloseShieldReader.java 576 2015-03-28 00:03:33Z gcontini $
*/
public class CloseShieldReader extends FilterReader {
private int closeCount = 0;
/**
* Construct a CloseShieldReader
that forwards the calls to
* the source Reader passed in the constructor.
*
* @param source
* original Reader
* @param a T object.
*/
public CloseShieldReader(final T source) {
super(source);
if (source == null) {
throw new IllegalArgumentException("Source reader can't be null");
}
}
/**
* {@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 {
this.closeCount++;
}
/**
* Returns the number of time that close was called.
*
* @return Number of times that close was called
*/
public int getCloseCount() {
return this.closeCount;
}
/**
*
* Returns the wrapped (original) Reader
passed in the
* constructor.
*
*
* @return The original Reader
passed in the constructor
*/
public T getWrappedReader() {
@SuppressWarnings("unchecked")
final T result = (T) this.in;
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy