/* * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Eike Stepper - initial API and implementation */ package org.eclipse.net4j.util.io; import java.io.IOException; import java.io.OutputStream; /** * This class is the superclass of all classes that filter output streams. These streams sit on top of an already * existing output stream (the underlying output stream) which it uses as its basic sink of data, but possibly * transforming the data along the way or providing additional functionality. *

* The class DelegatingOutputStream itself simply overrides all methods of OutputStream with * versions that pass all requests to the underlying output stream. Subclasses of DelegatingOutputStream * may further override some of these methods as well as provide additional methods and fields. *

* Note: The only difference to {@link java.io.FilterOutputStream} is that DelegatingOutputStream * does not override {@link #write(byte[])} or {@link #write(byte[], int, int)} but rather exposes the original * implementations of InputStream which call {@link #write(int)} instead of their delegate counterparts. * * @author Eike Stepper */ public class DelegatingOutputStream extends OutputStream { /** * The underlying output stream to be filtered. */ protected OutputStream out; /** * Creates an output stream filter built on top of the specified underlying output stream. * * @param out * the underlying output stream to be assigned to the field this.out for later use, or * null if this instance is to be created without an underlying stream. */ public DelegatingOutputStream(OutputStream out) { this.out = out; } public OutputStream getDelegate() { return out; } /** * Writes the specified byte to this output stream. *

* The write method of DelegatingOutputStream calls the write method of its * underlying output stream, that is, it performs out.write(b). *

* Implements the abstract write method of OutputStream. * * @param b * the byte. * @exception IOException * if an I/O error occurs. */ @Override public void write(int b) throws IOException { out.write(b); } /** * Flushes this output stream and forces any buffered output bytes to be written out to the stream. *

* The flush method of DelegatingOutputStream calls the flush method of its * underlying output stream. * * @exception IOException * if an I/O error occurs. * @see DelegatingOutputStream#out */ @Override public void flush() throws IOException { out.flush(); } /** * Closes this output stream and releases any system resources associated with the stream. *

* The close method of DelegatingOutputStream calls its flush method, and then * calls the close method of its underlying output stream. * * @exception IOException * if an I/O error occurs. * @see DelegatingOutputStream#flush() * @see DelegatingOutputStream#out */ @Override public void close() throws IOException { try { flush(); } catch (IOException ignored) { } out.close(); } }