1 /* 2 * Copyright (C) 2013 DroidDriver committers 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 io.appium.droiddriver.util; 18 19 import android.annotation.TargetApi; 20 import android.os.Build; 21 import android.util.Log; 22 23 import java.io.BufferedOutputStream; 24 import java.io.File; 25 import java.io.FileNotFoundException; 26 import java.io.FileOutputStream; 27 28 import io.appium.droiddriver.exceptions.DroidDriverException; 29 30 /** 31 * Internal helper methods for manipulating files. 32 */ 33 public class FileUtils { 34 /** 35 * Opens file at {@code path} to output. If any directories on {@code path} do 36 * not exist, they will be created. The file will be readable and writable to 37 * all. 38 */ 39 @TargetApi(Build.VERSION_CODES.GINGERBREAD) open(String path)40 public static BufferedOutputStream open(String path) throws FileNotFoundException { 41 File file = getAbsoluteFile(path); 42 43 Logs.log(Log.INFO, "opening file " + file.getAbsolutePath()); 44 BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file)); 45 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 46 file.setReadable(true /* readable */, false/* ownerOnly */); 47 file.setWritable(true /* readable */, false/* ownerOnly */); 48 } 49 return stream; 50 } 51 52 /** 53 * Returns a new file constructed using the absolute path of {@code path}. 54 * Unlike {@link File#getAbsoluteFile()}, default parent is "java.io.tmpdir" 55 * instead of "user.dir". 56 * <p> 57 * If any directories on {@code path} do not exist, they will be created. 58 */ getAbsoluteFile(String path)59 public static File getAbsoluteFile(String path) { 60 File file = new File(path); 61 if (!file.isAbsolute()) { 62 file = new File(System.getProperty("java.io.tmpdir"), path); 63 } 64 mkdirs(file.getParentFile()); 65 return file; 66 } 67 68 @TargetApi(Build.VERSION_CODES.GINGERBREAD) mkdirs(File dir)69 private static void mkdirs(File dir) { 70 if (dir == null || dir.exists()) { 71 return; 72 } 73 74 mkdirs(dir.getParentFile()); 75 if (!dir.mkdir()) { 76 throw new DroidDriverException("failed to mkdir " + dir); 77 } 78 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 79 dir.setReadable(true /* readable */, false/* ownerOnly */); 80 dir.setWritable(true /* readable */, false/* ownerOnly */); 81 dir.setExecutable(true /* executable */, false/* ownerOnly */); 82 } 83 } 84 } 85