source: dispositivos_moviles/TibisayMovil/src/ve/gob/cenditel/tibisaymovil/FsUtils.java @ 6638d33

Last change on this file since 6638d33 was 09c6eb7, checked in by Antonio Araujo Brett <aaraujo@…>, 11 years ago

Se agregan al control de versiones clases y archivos de layout para la gestión del repositorio de certificados

  • Property mode set to 100644
File size: 5.0 KB
Line 
1package ve.gob.cenditel.tibisaymovil;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.OutputStream;
10import java.nio.channels.FileChannel;
11import java.util.Collection;
12
13import android.content.Context;
14import android.net.Uri;
15import android.os.Environment;
16import android.os.StatFs;
17import android.text.format.DateFormat;
18import android.util.Log;
19
20/**
21 * File system related utilities.
22 *
23 * @author José M. Prieto (jmprieto@emergya.com)
24 */
25public class FsUtils {
26
27    private static final String TAG = "FsUtils";
28
29    /**
30     * Save raw data to a File.
31     *
32     * @param data the data to be saved
33     * @param targetFilename name of the file to be created
34     * @throws FileAlreadyExistsException if the file already exists
35     * @throws FileNotFoundException if the file cannot be open for writing
36     * @throws IOException if there is an I/O error
37     */
38    public static Uri saveToFile(byte[] data, String targetFilename)
39    throws FileAlreadyExistsException, FileNotFoundException, IOException {
40
41        File file = createNewFile(targetFilename);
42        OutputStream stream = new FileOutputStream(file);
43        stream.write(data);
44        stream.close();
45        return Uri.fromFile(file);
46    }
47
48    public static Uri saveToFile(InputStream data, String targetFilename)
49    throws FileAlreadyExistsException, FileNotFoundException, IOException {
50
51        File file = createNewFile(targetFilename);
52        OutputStream stream = new FileOutputStream(file);
53        byte[] buf = new byte[8192];
54        int n;
55        while ((n = data.read(buf)) > 0) {
56            stream.write(buf, 0, n);
57        }
58        data.close();
59        stream.close();
60        return Uri.fromFile(file);
61    }
62
63    public static void copyTo(File file, String directory)
64    throws IOException {
65
66        String target = directory + File.separator + file.getName();
67        FileChannel source = null;
68        FileChannel destination = null;
69
70        try {
71            destination = new FileOutputStream(createNewFile(target)).getChannel();
72            source = new FileInputStream(file).getChannel();
73            destination.transferFrom(source, 0, source.size());
74        } finally {
75            if (source != null) {
76                source.close();
77            }
78            if (destination != null) {
79                destination.close();
80            }
81        }
82    }
83
84    private static File createNewFile(String targetFilename)
85    throws FileNotFoundException {
86
87        File file = new File(targetFilename);
88        String base = null;
89        String extension = null;
90        if (file.exists()) {
91            int n;
92            if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf(File.separator)) {
93                base = targetFilename.substring(0, n);
94                extension = targetFilename.substring(n);
95            } else {
96                base = targetFilename;
97                extension = "";
98            }
99        }
100        for (int i = 1; file.exists(); i++) {
101            file = new File(base + "-" + i + extension);
102        }
103        return file;
104    }
105
106    public static void checkOutOfSpace(Exception e, boolean external)
107    throws OutOfSpaceError {
108
109        String path;
110        if (external) {
111            path = Environment.getExternalStorageDirectory().getAbsolutePath();
112        } else {
113            path = Environment.getDataDirectory().getAbsolutePath();
114        }
115        StatFs stat = new StatFs(path);
116        int available = stat.getAvailableBlocks() * stat.getBlockSize();
117        if (available < 8192) {
118            throw new OutOfSpaceError(e);
119        }
120    }
121
122    public static String createOutputDirectory(Context context) {
123
124        File root = new File(Environment.getExternalStorageDirectory(),
125                context.getResources().getString(R.string.signature_directory));
126        root.mkdirs();
127        try {
128            new File(root.getAbsolutePath() + File.separator + ".nomedia").createNewFile();
129        } catch (IOException e) {
130            Log.e(TAG, "Error creating .nomedia file", e);
131        }
132
133        CharSequence datePart = DateFormat.format("yyyy-MM-dd_kkmmss", System.currentTimeMillis());
134        String output = root.getAbsolutePath() + File.separator + datePart;
135
136        if (new File(output).mkdirs()) {
137            return output;
138        } else {
139            return null;
140        }
141    }
142
143    public static boolean isPDF(String inputFilename) {
144
145        return inputFilename.toLowerCase().endsWith(".pdf");
146    }
147
148    public static boolean containsPDF(Collection<Uri> uris) {
149
150        for (Uri uri : uris) {
151            if (isPDF(uri.getPath())) {
152                return true;
153            }
154        }
155        return false;
156    }
157
158    public static boolean isHiddenOrNotReadable(File f) {
159
160        String name = f.getName();
161        boolean notReadable = !f.canRead();
162        boolean hidden = name.startsWith(".") && !name.equals(".") && !name.equals("..");
163        return notReadable || hidden;
164    }
165}
Note: See TracBrowser for help on using the repository browser.