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

org.conqat.lib.commons.io.MultiplexedOutputStream Maven / Gradle / Ivy

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.conqat.lib.commons.io;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.UnmodifiableList;
import org.conqat.lib.commons.string.StringUtils;

/**
 * This class enables multiplexing of output streams. It can be e.g. used to output content to
 * multiple files.
 */
public class MultiplexedOutputStream extends OutputStream {

	/** The underlying output streams. */
	private final OutputStream[] streams;

	/**
	 * Create new multiplexed output streams.
	 * 
	 * @param streams
	 *            any number of output streams.
	 */
	public MultiplexedOutputStream(OutputStream... streams) {
		this.streams = streams;
	}

	/**
	 * Forwards close operation to all underlying output streams.
	 * 
	 * @throws MultiIOException
	 *             if one or more of the underlying streams raised an exception
	 */
	@Override
	public void close() throws MultiIOException {
		List exceptions = new ArrayList<>();
		for (OutputStream stream : streams) {
			try {
				stream.close();
			} catch (IOException e) {
				exceptions.add(e);
			}
		}
		checkExceptions(exceptions);
	}

	/**
	 * Forwards flush operation to all underlying output streams.
	 * 
	 * @throws MultiIOException
	 *             if one or more of the underlying streams raised an exception
	 */
	@Override
	public void flush() throws MultiIOException {
		List exceptions = new ArrayList<>();
		for (OutputStream stream : streams) {
			try {
				stream.flush();
			} catch (IOException e) {
				exceptions.add(e);
			}
		}
		checkExceptions(exceptions);
	}

	/**
	 * Forwards write operation to all underlying output streams.
	 * 
	 * @throws MultiIOException
	 *             if one or more of the underlying streams raised an exception
	 */
	@Override
	public void write(int b) throws MultiIOException {
		List exceptions = new ArrayList<>();
		for (OutputStream stream : streams) {
			try {
				stream.write(b);
			} catch (IOException e) {
				exceptions.add(e);
			}
		}
		checkExceptions(exceptions);
	}

	/**
	 * Raises an {@link MultiIOException} if the provided collection is not empty.
	 */
	private static void checkExceptions(Collection exceptions) throws MultiIOException {
		if (!exceptions.isEmpty()) {
			throw new MultiIOException(exceptions);
		}
	}

	/** Exception class that encapsulates multiple {@link IOException}s. */
	public static class MultiIOException extends IOException {

		/** Serial version UID. */
		private static final long serialVersionUID = 1;

		/** The exceptions. */
		private final List exceptions = new ArrayList<>();

		/**
		 * Create exception
		 * 
		 * @throws AssertionError
		 *             if provided collection is empty.
		 */
		public MultiIOException(Collection exceptions) {
			CCSMAssert.isFalse(exceptions.isEmpty(), "Must have at least one exception.");
			this.exceptions.addAll(exceptions);
		}

		/** Returns messages of all exceptions. */
		@Override
		public String getMessage() {
			StringBuilder result = new StringBuilder();
			for (IOException ex : exceptions) {
				result.append(ex.getMessage());
				result.append(StringUtils.LINE_SEPARATOR);
			}
			return result.toString();
		}

		/** Get exceptions. */
		public UnmodifiableList getExceptions() {
			return CollectionUtils.asUnmodifiable(exceptions);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy