001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017package org.opends.server.util; 018 019 020 021import java.io.OutputStream; 022 023import org.forgerock.util.Reject; 024import org.forgerock.i18n.slf4j.LocalizedLogger; 025 026 027/** 028 * This class defines a simple {@code OutputStream} object that can be used to 029 * write all messages to multiple targets at the same time, much like the UNIX 030 * "tee" command. Note that this class will never throw any exceptions 031 */ 032@org.opends.server.types.PublicAPI( 033 stability=org.opends.server.types.StabilityLevel.UNCOMMITTED, 034 mayInstantiate=true, 035 mayExtend=false, 036 mayInvoke=true) 037public final class MultiOutputStream 038 extends OutputStream 039{ 040 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 041 042 043 044 /** The set of target output streams to which all messages will be written. */ 045 private final OutputStream[] targetStreams; 046 047 048 049 /** 050 * Creates a new {@code MultiOutputStream} object that will write all messages 051 * to all of the target streams. 052 * 053 * @param targetStreams The set of print streams to which all messages 054 * should be written. This must not be {@code null}, 055 * nor may it contain any {@code null} elements. 056 */ 057 public MultiOutputStream(OutputStream... targetStreams) 058 { 059 Reject.ifNull(targetStreams); 060 061 this.targetStreams = targetStreams; 062 } 063 064 065 066 /** 067 * Closes all of the underlying output streams. 068 */ 069 public void close() 070 { 071 for (OutputStream s : targetStreams) 072 { 073 try 074 { 075 s.close(); 076 } 077 catch (Exception e) 078 { 079 logger.traceException(e); 080 } 081 } 082 } 083 084 085 086 /** 087 * Flushes all of the underlying output streams. 088 */ 089 public void flush() 090 { 091 for (OutputStream s : targetStreams) 092 { 093 try 094 { 095 s.flush(); 096 } 097 catch (Exception e) 098 { 099 logger.traceException(e); 100 } 101 } 102 } 103 104 105 106 /** 107 * Writes the contents of the provided byte array to all of the underlying 108 * output streams. 109 * 110 * @param b The byte array containing the data to be written. 111 */ 112 public void write(byte[] b) 113 { 114 for (OutputStream s : targetStreams) 115 { 116 try 117 { 118 s.write(b); 119 } 120 catch (Exception e) 121 { 122 logger.traceException(e); 123 } 124 } 125 } 126 127 128 129 /** 130 * Writes the specified portion of the provided byte array to all of the 131 * underlying output streams. 132 * 133 * @param b The byte array containing the data to be written. 134 * @param off The position at which the data to write begins in the array. 135 * @param len The number of bytes to b written. 136 */ 137 public void write(byte[] b, int off, int len) 138 { 139 for (OutputStream s : targetStreams) 140 { 141 try 142 { 143 s.write(b, off, len); 144 } 145 catch (Exception e) 146 { 147 logger.traceException(e); 148 } 149 } 150 } 151 152 153 154 /** 155 * Writes the specified byte to the set of target output streams. 156 * 157 * @param b The byte to be written. 158 */ 159 public void write(int b) 160 { 161 for (OutputStream s : targetStreams) 162 { 163 try 164 { 165 s.write(b); 166 } 167 catch (Exception e) 168 { 169 logger.traceException(e); 170 } 171 } 172 } 173} 174