1 /* 2 * Copyright 2011 The Apache Software Foundation 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.hadoop.hbase.io; 21 22 import java.io.IOException; 23 import java.io.OutputStream; 24 25 /** 26 * An output stream that writes to two streams on each operation. Does not 27 * attempt to handle exceptions gracefully. If any operation other than 28 * {@link #close()} fails on the first stream, it is not called on the second 29 * stream. 30 */ 31 public class DoubleOutputStream extends OutputStream { 32 private OutputStream out1; 33 private OutputStream out2; 34 35 public DoubleOutputStream(OutputStream out1, OutputStream out2) { 36 this.out1 = out1; 37 this.out2 = out2; 38 } 39 40 @Override 41 public void write(int b) throws IOException { 42 out1.write(b); 43 out2.write(b); 44 } 45 46 @Override 47 public void write(byte b[]) throws IOException { 48 out1.write(b, 0, b.length); 49 out2.write(b, 0, b.length); 50 } 51 52 @Override 53 public void write(byte b[], int off, int len) throws IOException { 54 out1.write(b, off, len); 55 out2.write(b, off, len); 56 } 57 58 @Override 59 public void flush() throws IOException { 60 out1.flush(); 61 out2.flush(); 62 } 63 64 @Override 65 public void close() throws IOException { 66 try { 67 out1.close(); 68 } finally { 69 // Make sure we at least attempt to close both streams. 70 out2.close(); 71 } 72 } 73 74 }