Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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.os.cts;
     18 
     19 import java.io.ByteArrayOutputStream;
     20 import java.io.File;
     21 import java.io.FileOutputStream;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 
     25 /** Bits and pieces copied from hidden API of android.os.FileUtils. */
     26 public class FileUtils {
     27 
     28     public static final int S_IFSOCK = 0140000;
     29     public static final int S_IFLNK = 0120000;
     30     public static final int S_IFREG = 0100000;
     31     public static final int S_IFBLK = 0060000;
     32     public static final int S_IFDIR = 0040000;
     33     public static final int S_IFCHR = 0020000;
     34     public static final int S_IFIFO = 0010000;
     35 
     36     public static final int S_ISUID = 0004000;
     37     public static final int S_ISGID = 0002000;
     38     public static final int S_ISVTX = 0001000;
     39 
     40     public static final int S_IRWXU = 00700;
     41     public static final int S_IRUSR = 00400;
     42     public static final int S_IWUSR = 00200;
     43     public static final int S_IXUSR = 00100;
     44 
     45     public static final int S_IRWXG = 00070;
     46     public static final int S_IRGRP = 00040;
     47     public static final int S_IWGRP = 00020;
     48     public static final int S_IXGRP = 00010;
     49 
     50     public static final int S_IRWXO = 00007;
     51     public static final int S_IROTH = 00004;
     52     public static final int S_IWOTH = 00002;
     53     public static final int S_IXOTH = 00001;
     54 
     55     static {
     56         System.loadLibrary("cts_jni");
     57     }
     58 
     59     public static class FileStatus {
     60 
     61         public int dev;
     62         public int ino;
     63         public int mode;
     64         public int nlink;
     65         public int uid;
     66         public int gid;
     67         public int rdev;
     68         public long size;
     69         public int blksize;
     70         public long blocks;
     71         public long atime;
     72         public long mtime;
     73         public long ctime;
     74 
     75         public boolean hasModeFlag(int flag) {
     76             return (mode & flag) == flag;
     77         }
     78     }
     79 
     80     /**
     81      * @param path of the file to stat
     82      * @param status object to set the fields on
     83      * @param statLinks or don't stat links (lstat vs stat)
     84      * @return whether or not we were able to stat the file
     85      */
     86     public native static boolean getFileStatus(String path, FileStatus status, boolean statLinks);
     87 
     88     public native static String getUserName(int uid);
     89 
     90     public native static String getGroupName(int gid);
     91 
     92     public native static int setPermissions(String file, int mode);
     93 
     94     /**
     95      * Copy data from a source stream to destFile.
     96      * Return true if succeed, return false if failed.
     97      */
     98     public static boolean copyToFile(InputStream inputStream, File destFile) {
     99         try {
    100             if (destFile.exists()) {
    101                 destFile.delete();
    102             }
    103             FileOutputStream out = new FileOutputStream(destFile);
    104             try {
    105                 byte[] buffer = new byte[4096];
    106                 int bytesRead;
    107                 while ((bytesRead = inputStream.read(buffer)) >= 0) {
    108                     out.write(buffer, 0, bytesRead);
    109                 }
    110             } finally {
    111                 out.flush();
    112                 try {
    113                     out.getFD().sync();
    114                 } catch (IOException e) {
    115                 }
    116                 out.close();
    117             }
    118             return true;
    119         } catch (IOException e) {
    120             return false;
    121         }
    122     }
    123 
    124     public static void createFile(File file, int numBytes) throws IOException {
    125         File parentFile = file.getParentFile();
    126         if (parentFile != null) {
    127             parentFile.mkdirs();
    128         }
    129         byte[] buffer = new byte[numBytes];
    130         FileOutputStream output = new FileOutputStream(file);
    131         try {
    132             output.write(buffer);
    133         } finally {
    134             output.close();
    135         }
    136     }
    137 
    138     public static byte[] readInputStreamFully(InputStream is) {
    139         ByteArrayOutputStream os = new ByteArrayOutputStream();
    140         byte[] buffer = new byte[32768];
    141         int count;
    142         try {
    143             while ((count = is.read(buffer)) != -1) {
    144                 os.write(buffer, 0, count);
    145             }
    146             is.close();
    147         } catch (IOException e) {
    148             throw new RuntimeException(e);
    149         }
    150         return os.toByteArray();
    151     }
    152 }
    153