All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gc.iotools.stream.os.CloseOnceOutputStream Maven / Gradle / Ivy

Go to download

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.os;

/*
 * Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
 * under the BSD License.
 */
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 

* A decorating OutputStream that prevents multiple invocations of * the {@linkplain #close()} method on 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 non standard OutputStream that don't allow * close() to be called multiple times. *

* * @author Gabriele Contini * @since 1.2.14 * @param * Type of the OutputStream passed in the constructor. * @version $Id: CloseOnceOutputStream.java 523 2013-01-02 15:46:17Z * $ */ public class CloseOnceOutputStream extends FilterOutputStream { private int closeCount = 0; /** * Construct a CloseOnceOutputStream that forwards the calls to * the source OutputStream passed in the constructor. * * @param source * original OutputStream */ public CloseOnceOutputStream(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.DiagnosticOutputStream * @return Number of times that close was called */ public int getCloseCount() { return this.closeCount; } /** *

* Returns the wrapped (original) OutputStream passed in the * constructor. *

* * @return The original OutputStream passed in the constructor */ public T getWrappedOutputStream() { @SuppressWarnings("unchecked") final T result = (T) super.out; return result; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy