1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
package com.ford.syncV4.android.utils;
import android.os.Looper;
import android.util.Log;
import com.ford.syncV4.android.MainApp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created with Android Studio.
* Author: Chernyshov Yuriy - Mobile Development
* Date: 1/30/14
* Time: 2:15 PM
*/
public class AppUtils {
private static final String TAG = "AppUtils";
public static boolean isRunningUIThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
/**
* Returns the file contents from the specified resource.
*
* @param resource Resource id (in res/ directory)
* @return The resource file's contents
*/
public static byte[] contentsOfResource(int resource) {
return contentsOfResource(MainApp.getInstance().getResources().openRawResource(resource));
}
/**
* Read a File and return bytes array
* @param file
* @return
*/
public static byte[] contentsOfResource(File file) {
try {
return contentsOfResource(new FileInputStream(file));
} catch (FileNotFoundException e) {
Log.e(TAG, "Contents Of Resource exception", e);
}
return new byte[0];
}
public static byte[] contentsOfResource(String fileName) {
return contentsOfResource(new File(fileName));
}
/**
* Save bytes to a file
* @param data
* @param filePath
* @return
*/
public static boolean saveDataToFile(byte[] data, String filePath) {
File mFile = new File(filePath);
Log.d(TAG, "Saving data to file '" + filePath + "', exists:" + mFile.exists());
if (mFile.exists()) {
mFile.delete();
}
FileOutputStream mFileOutputStream = null;
boolean result = false;
try {
mFileOutputStream = new FileOutputStream(mFile.getPath());
mFileOutputStream.write(data);
result = true;
} catch (IOException e) {
Log.e(TAG, "Save Data To File IOException", e);
} finally {
if (mFileOutputStream != null) {
try {
mFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* Read a content of the Input Stream into bytes array
*
* @param inputStream a stream of incoming bytes
* @return bytes array
*/
private static byte[] contentsOfResource(InputStream inputStream) {
try {
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(inputStream.available());
final int bufferSize = 4096;
final byte[] buffer = new byte[bufferSize];
int available;
while ((available = inputStream.read(buffer)) >= 0) {
byteArrayOutputStream.write(buffer, 0, available);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
Log.w(TAG, "Can't read file", e);
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
}
}
}
|