AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / util / DoubleOutputStream.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.util;
23
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 public class DoubleOutputStream extends OutputStream {
28     private OutputStream[] oss;
29         private boolean[] close;
30
31         /**
32      * Create a Double Stream Writer
33      * Some Streams should not be closed by this object (i.e. System.out), therefore, mark them with booleans
34      */
35     public DoubleOutputStream(OutputStream a, boolean closeA, OutputStream b, boolean closeB) {
36                 oss = new OutputStream[] {a,b};
37                 close = new boolean[] {closeA,closeB};
38     }
39
40     /**
41      * Write a single character.
42      * @throws IOException 
43      */
44     @Override
45     public void write(int c) throws IOException {
46         for(OutputStream os : oss) {
47                 os.write(c);
48         }
49     }
50
51     /**
52      * Write a portion of an array of characters.
53      *
54      * @param  bbuf  Array of characters
55      * @param  off   Offset from which to start writing characters
56      * @param  len   Number of characters to write
57      * @throws IOException 
58      */
59     @Override
60     public void write(byte bbuf[], int off, int len) throws IOException {
61         for(OutputStream os : oss) {
62                 os.write(bbuf,off,len);
63         }
64     }
65
66     @Override
67         public void write(byte[] b) throws IOException {
68         for(OutputStream os : oss) {
69                 os.write(b);
70         }
71         }
72
73         /* (non-Javadoc)
74          * @see java.io.OutputStream#close()
75          */
76         @Override
77         public void close() throws IOException {
78                 for(int i=0;i<oss.length;++i) {
79                         if(close[i]) {
80                                 oss[i].close();
81                         }
82         }
83         }
84
85         /* (non-Javadoc)
86          * @see java.io.OutputStream#flush()
87          */
88         @Override
89         public void flush() throws IOException {
90         for(OutputStream os : oss) {
91                 os.flush();
92         }
93         }
94
95
96
97 }