• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 com.android.compatibility.common.util;
18 
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 
27 /**
28  * A helper class for file related operations
29  */
30 public class FileUtil {
31 
32     /**
33      * Recursively delete given file or directory and all its contents.
34      *
35      * @param rootDir the directory or file to be deleted; can be null
36      */
recursiveDelete(File rootDir)37     public static void recursiveDelete(File rootDir) {
38         if (rootDir != null) {
39             if (rootDir.isDirectory()) {
40                 File[] childFiles = rootDir.listFiles();
41                 if (childFiles != null) {
42                     for (File child : childFiles) {
43                         recursiveDelete(child);
44                     }
45                 }
46             }
47             rootDir.delete();
48         }
49     }
50 
51     /**
52      * A helper method for writing stream data to file
53      *
54      * @param input the unbuffered input stream
55      * @param destFile the dest file to write to
56      */
writeToFile(InputStream input, File destFile)57     public static void writeToFile(InputStream input, File destFile) throws IOException {
58         InputStream origStream = null;
59         OutputStream destStream = null;
60         try {
61             origStream = new BufferedInputStream(input);
62             destStream = new BufferedOutputStream(new FileOutputStream(destFile));
63             StreamUtil.copyStreams(origStream, destStream);
64         } finally {
65             origStream.close();
66             destStream.flush();
67             destStream.close();
68         }
69     }
70 
71 }
72