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      private final boolean isTransientStorage;
036    
037      /**
038       * Create an object with a data output stream, a checksum output stream
039       * and a checksum.
040       */
041      public ReplicaOutputStreams(OutputStream dataOut, OutputStream checksumOut,
042          DataChecksum checksum, boolean isTransientStorage) {
043        this.dataOut = dataOut;
044        this.checksumOut = checksumOut;
045        this.checksum = checksum;
046        this.isTransientStorage = isTransientStorage;
047      }
048    
049      /** @return the data output stream. */
050      public OutputStream getDataOut() {
051        return dataOut;
052      }
053    
054      /** @return the checksum output stream. */
055      public OutputStream getChecksumOut() {
056        return checksumOut;
057      }
058    
059      /** @return the checksum. */
060      public DataChecksum getChecksum() {
061        return checksum;
062      }
063    
064      /** @return is writing to a transient storage? */
065      public boolean isTransientStorage() {
066        return isTransientStorage;
067      }
068    
069      @Override
070      public void close() {
071        IOUtils.closeStream(dataOut);
072        IOUtils.closeStream(checksumOut);
073      }
074    
075      /**
076       * Sync the data stream if it supports it.
077       */
078      public void syncDataOut() throws IOException {
079        if (dataOut instanceof FileOutputStream) {
080          ((FileOutputStream)dataOut).getChannel().force(true);
081        }
082      }
083      
084      /**
085       * Sync the checksum stream if it supports it.
086       */
087      public void syncChecksumOut() throws IOException {
088        if (checksumOut instanceof FileOutputStream) {
089          ((FileOutputStream)checksumOut).getChannel().force(true);
090        }
091      }
092    
093    }