FsUtils.java revision 2e5982a55ac031110ed39515a76f7a5ec9ff2c14
1/*
2 * Copyright (C) 2010 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
17package com.android.dumprendertree2;
18
19import android.util.Log;
20
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.io.OutputStream;
26
27/**
28 *
29 */
30public class FsUtils {
31    public static final String LOG_TAG = "FsUtils";
32
33    public static void writeDataToStorage(File file, byte[] bytes, boolean append) {
34        Log.d(LOG_TAG, "writeDataToStorage(): " + file.getAbsolutePath());
35        try {
36            OutputStream outputStream = null;
37            try {
38                file.getParentFile().mkdirs();
39                file.createNewFile();
40                Log.d(LOG_TAG, "writeDataToStorage(): File created.");
41                outputStream = new FileOutputStream(file, append);
42                outputStream.write(bytes);
43            } finally {
44                if (outputStream != null) {
45                    outputStream.close();
46                }
47            }
48        } catch (IOException e) {
49            Log.e(LOG_TAG, "file.getAbsolutePath=" + file.getAbsolutePath(), e);
50        }
51    }
52
53    public static byte[] readDataFromStorage(File file) {
54        if (!file.exists()) {
55            Log.d(LOG_TAG, "readDataFromStorage(): File does not exist: "
56                    + file.getAbsolutePath());
57            return null;
58        }
59
60        byte[] bytes = null;
61        try {
62            FileInputStream fis = null;
63            try {
64                fis = new FileInputStream(file);
65                bytes = new byte[(int) file.length()];
66                fis.read(bytes);
67            } finally {
68                if (fis != null) {
69                    fis.close();
70                }
71            }
72        } catch (IOException e) {
73            Log.e(LOG_TAG, "file.getAbsolutePath=" + file.getAbsolutePath(), e);
74        }
75
76        return bytes;
77    }
78}