Skip to content

Commit b199273

Browse files
committed
Check file size is valid and catch OOM error
1 parent fdfe6ce commit b199273

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

app/src/main/java/com/samsung/microbit/utils/FileUtils.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,50 @@ public static long fileSizeFromUri( Uri uri, Context ctx) {
196196
return -1;
197197
}
198198

199-
public static byte[] readBytesFromInputStream(InputStream is, final int size) {
199+
public static String runtimeMemory() {
200+
Runtime runtime = Runtime.getRuntime();
201+
long totalMemory = runtime.totalMemory();
202+
long maxMemory = runtime.maxMemory();
203+
long freeMemory = runtime.freeMemory();
204+
return "memory: total" + totalMemory + "; max " + maxMemory + "; free " + freeMemory;
205+
}
206+
207+
public static byte[] newBytes(final int size) {
208+
Log.d( TAG, "newBytes(" + size + ") " + runtimeMemory());
209+
if ( size <= 0) {
210+
return null;
211+
}
212+
200213
byte[] bytes = null;
201214
try {
202215
bytes = new byte[size];
216+
} catch ( Exception e) {
217+
bytes = null;
218+
} catch ( OutOfMemoryError oom) {
219+
bytes = null;
220+
}
221+
return bytes;
222+
}
223+
224+
public static byte[] readBytesFromInputStream(InputStream is, final int size) {
225+
if ( size <= 0) {
226+
return null;
227+
}
228+
229+
byte[] bytes = null;
230+
try {
231+
bytes = newBytes(size);
232+
if ( bytes == null) {
233+
return null;
234+
}
203235
int remain = size;
204236
int offset = 0;
205237
while ( remain > 0) {
206238
int read = is.read( bytes, offset, remain);
207239
remain -= read;
208240
offset += read;
209241
}
210-
} catch ( Exception e){
242+
} catch ( Exception e) {
211243
bytes = null;
212244
}
213245
return bytes;
@@ -216,8 +248,11 @@ public static byte[] readBytesFromInputStream(InputStream is, final int size) {
216248
public static byte[] readBytesFromFile( File file) {
217249
byte[] bytes = null;
218250
try {
219-
FileInputStream is = new FileInputStream( file);
220251
int size = (int) file.length();
252+
if ( size <= 0) {
253+
return null;
254+
}
255+
FileInputStream is = new FileInputStream( file);
221256
bytes = readBytesFromInputStream(is, size);
222257
is.close();
223258
} catch ( Exception e){
@@ -229,9 +264,12 @@ public static byte[] readBytesFromFile( File file) {
229264
public static byte[] readBytesFromUri( Uri uri, Context ctx) {
230265
byte[] bytes = null;
231266
try {
267+
int size = (int) fileSizeFromUri( uri, ctx);
268+
if ( size <= 0) {
269+
return null;
270+
}
232271
InputStream is = ctx.getContentResolver().openInputStream( uri);
233272
if ( is != null) {
234-
int size = (int) fileSizeFromUri( uri, ctx);
235273
bytes = readBytesFromInputStream( is, size);
236274
is.close();
237275
}
@@ -241,7 +279,6 @@ public static byte[] readBytesFromUri( Uri uri, Context ctx) {
241279
return bytes;
242280
}
243281

244-
245282
public static boolean stringBuilderAddStream( StringBuilder sb, InputStream is) {
246283
boolean ok = true;
247284
try {

0 commit comments

Comments
 (0)