Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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 
     17 package android.provider.cts;
     18 
     19 import android.content.Context;
     20 
     21 import java.io.File;
     22 import java.io.FileNotFoundException;
     23 import java.io.FileOutputStream;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.OutputStream;
     27 import java.util.ArrayList;
     28 
     29 /**
     30  * The Class FileCopyHelper is used to copy files from resources to the
     31  * application directory and responsible for deleting the files.
     32  *
     33  * @see MediaStore_VideoTest
     34  * @see MediaStore_Images_MediaTest
     35  * @see MediaStore_Images_ThumbnailsTest
     36  */
     37 public class FileCopyHelper {
     38     /** The context. */
     39     private Context mContext;
     40 
     41     /** The files added. */
     42     private ArrayList<String> mFilesList;
     43 
     44     /**
     45      * Instantiates a new file copy helper.
     46      *
     47      * @param context the context
     48      */
     49     public FileCopyHelper(Context context) {
     50         mContext = context;
     51         mFilesList = new ArrayList<String>();
     52     }
     53 
     54     /**
     55      * Copy the file from the resources with a filename .
     56      *
     57      * @param resId the res id
     58      * @param fileName the file name
     59      *
     60      * @return the absolute path of the destination file
     61      * @throws IOException
     62      */
     63     public String copy(int resId, String fileName) throws IOException {
     64         InputStream source = mContext.getResources().openRawResource(resId);
     65         OutputStream target = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
     66         copyFile(source, target);
     67         mFilesList.add(fileName);
     68         return mContext.getFileStreamPath(fileName).getAbsolutePath();
     69     }
     70 
     71     public void copyToExternalStorage(int resId, File path) throws IOException {
     72         InputStream source = mContext.getResources().openRawResource(resId);
     73         OutputStream target = new FileOutputStream(path);
     74         copyFile(source, target);
     75     }
     76 
     77     private void copyFile(InputStream source, OutputStream target) throws IOException {
     78         try {
     79             byte[] buffer = new byte[1024];
     80             for (int len = source.read(buffer); len > 0; len = source.read(buffer)) {
     81                 target.write(buffer, 0, len);
     82             }
     83         } finally {
     84             if (source != null) {
     85                 source.close();
     86             }
     87             if (target != null) {
     88                 target.close();
     89             }
     90         }
     91     }
     92 
     93     /**
     94      * Delete all the files copied by the helper.
     95      */
     96     public void clear(){
     97         for (String path : mFilesList) {
     98             mContext.deleteFile(path);
     99         }
    100     }
    101 
    102     public static void createFile(File file, int numBytes) throws IOException {
    103         File parentFile = file.getParentFile();
    104         if (parentFile != null) {
    105             parentFile.mkdirs();
    106         }
    107         byte[] buffer = new byte[numBytes];
    108         FileOutputStream output = new FileOutputStream(file);
    109         try {
    110             output.write(buffer);
    111         } finally {
    112             output.close();
    113         }
    114     }
    115 }
    116