View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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   */
19  package org.apache.hadoop.hbase.io;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  
26  /**
27   * An output stream that writes to two streams on each operation. Does not
28   * attempt to handle exceptions gracefully. If any operation other than
29   * {@link #close()} fails on the first stream, it is not called on the second
30   * stream.
31   */
32  @InterfaceAudience.Private
33  public class DoubleOutputStream extends OutputStream {
34    private OutputStream out1;
35    private OutputStream out2;
36  
37    public DoubleOutputStream(OutputStream out1, OutputStream out2) {
38      this.out1 = out1;
39      this.out2 = out2;
40    }
41  
42    @Override
43    public void write(int b) throws IOException {
44      out1.write(b);
45      out2.write(b);
46    }
47  
48    @Override
49    public void write(byte b[]) throws IOException {
50      out1.write(b, 0, b.length);
51      out2.write(b, 0, b.length);
52    }
53  
54    @Override
55    public void write(byte b[], int off, int len) throws IOException {
56      out1.write(b, off, len);
57      out2.write(b, off, len);
58    }
59  
60    @Override
61    public void flush() throws IOException {
62      out1.flush();
63      out2.flush();
64    }
65  
66    @Override
67    public void close() throws IOException {
68      try {
69        out1.close();
70      } finally {
71        // Make sure we at least attempt to close both streams.
72        out2.close();
73      }
74    }
75  
76  }