Home | History | Annotate | Download | only in result
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.result;
     17 
     18 import com.android.tradefed.util.FileUtil;
     19 
     20 import java.io.File;
     21 import java.io.FileInputStream;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 
     25 /**
     26  * A {@link InputStreamSource} that takes an input file.
     27  *
     28  * <p>Caller is responsible for deleting the file
     29  */
     30 public class FileInputStreamSource implements InputStreamSource {
     31 
     32     private final File mFile;
     33     private boolean mIsCancelled = false;
     34     private boolean mDeleteOnCancel = false;
     35 
     36     public FileInputStreamSource(File file) {
     37         mFile = file;
     38     }
     39 
     40     /**
     41      * Ctor
     42      *
     43      * @param file {@link File} containing the data to be streamed
     44      * @param deleteFileOnCancel if true, the file associated will be deleted when {@link #close()}
     45      *     is called
     46      */
     47     public FileInputStreamSource(File file, boolean deleteFileOnCancel) {
     48         mFile = file;
     49         mDeleteOnCancel = deleteFileOnCancel;
     50     }
     51 
     52     /**
     53      * {@inheritDoc}
     54      */
     55     @Override
     56     public synchronized InputStream createInputStream() {
     57         if (mIsCancelled) {
     58             return null;
     59         }
     60         try {
     61             return new FileInputStream(mFile);
     62         } catch (IOException e) {
     63             return null;
     64         }
     65     }
     66 
     67     /** {@inheritDoc} */
     68     @Override
     69     public synchronized void close() {
     70         mIsCancelled = true;
     71         if (mDeleteOnCancel) {
     72             cleanFile();
     73         }
     74     }
     75 
     76     /**
     77      * {@inheritDoc}
     78      */
     79     @Override
     80     public long size() {
     81         return mFile.length();
     82     }
     83 
     84     /**
     85      * Convenience method to delete the file associated with the FileInputStreamSource. Not safe.
     86      */
     87     public void cleanFile() {
     88         FileUtil.deleteFile(mFile);
     89     }
     90 }
     91 
     92