001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.hdfs.server.datanode.fsdataset;
019    
020    import java.io.Closeable;
021    import java.io.FileOutputStream;
022    import java.io.OutputStream;
023    import java.io.IOException;
024    
025    import org.apache.hadoop.io.IOUtils;
026    import org.apache.hadoop.util.DataChecksum;
027    
028    /**
029     * Contains the output streams for the data and checksum of a replica.
030     */
031    public class ReplicaOutputStreams implements Closeable {
032      private final OutputStream dataOut;
033      private final OutputStream checksumOut;
034      private final DataChecksum checksum;
035    
036      /**
037       * Create an object with a data output stream, a checksum output stream
038       * and a checksum.
039       */
040      public ReplicaOutputStreams(OutputStream dataOut, OutputStream checksumOut,
041          DataChecksum checksum) {
042        this.dataOut = dataOut;
043        this.checksumOut = checksumOut;
044        this.checksum = checksum;
045      }
046    
047      /** @return the data output stream. */
048      public OutputStream getDataOut() {
049        return dataOut;
050      }
051    
052      /** @return the checksum output stream. */
053      public OutputStream getChecksumOut() {
054        return checksumOut;
055      }
056    
057      /** @return the checksum. */
058      public DataChecksum getChecksum() {
059        return checksum;
060      }
061    
062      @Override
063      public void close() {
064        IOUtils.closeStream(dataOut);
065        IOUtils.closeStream(checksumOut);
066      }
067    
068      /**
069       * Sync the data stream if it supports it.
070       */
071      public void syncDataOut() throws IOException {
072        if (dataOut instanceof FileOutputStream) {
073          ((FileOutputStream)dataOut).getChannel().force(true);
074        }
075      }
076      
077      /**
078       * Sync the checksum stream if it supports it.
079       */
080      public void syncChecksumOut() throws IOException {
081        if (checksumOut instanceof FileOutputStream) {
082          ((FileOutputStream)checksumOut).getChannel().force(true);
083        }
084      }
085    
086    }