Skip to content

Commit 6a294f9

Browse files
committed
More checks
1 parent b199273 commit 6a294f9

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

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

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

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-
213-
byte[] bytes = null;
214-
try {
215-
bytes = new byte[size];
216-
} catch ( Exception e) {
217-
bytes = null;
218-
} catch ( OutOfMemoryError oom) {
219-
bytes = null;
220-
}
221-
return bytes;
222-
}
223-
224199
public static byte[] readBytesFromInputStream(InputStream is, final int size) {
225200
if ( size <= 0) {
226201
return null;
@@ -241,40 +216,75 @@ public static byte[] readBytesFromInputStream(InputStream is, final int size) {
241216
}
242217
} catch ( Exception e) {
243218
bytes = null;
219+
Log.e( TAG, "readBytesFromInputStream Exception");
220+
e.printStackTrace();
244221
}
245222
return bytes;
246223
}
247224

248225
public static byte[] readBytesFromFile( File file) {
249226
byte[] bytes = null;
250227
try {
251-
int size = (int) file.length();
228+
long size = file.length();
229+
if ( size > Integer.MAX_VALUE) {
230+
Log.e( TAG, "readBytesFromFile too big");
231+
return null;
232+
}
252233
if ( size <= 0) {
234+
Log.e( TAG, "readBytesFromFile size unknown");
253235
return null;
254236
}
255237
FileInputStream is = new FileInputStream( file);
256-
bytes = readBytesFromInputStream(is, size);
238+
bytes = readBytesFromInputStream(is, (int) size);
257239
is.close();
258240
} catch ( Exception e){
259241
bytes = null;
242+
Log.e( TAG, "readBytesFromFile Exception");
243+
e.printStackTrace();
260244
}
261245
return bytes;
262246
}
263247

264248
public static byte[] readBytesFromUri( Uri uri, Context ctx) {
265249
byte[] bytes = null;
266250
try {
267-
int size = (int) fileSizeFromUri( uri, ctx);
251+
long size = fileSizeFromUri( uri, ctx);
252+
if ( size > Integer.MAX_VALUE) {
253+
Log.e( TAG, "readBytesFromUri too big");
254+
return null;
255+
}
268256
if ( size <= 0) {
257+
Log.e( TAG, "readBytesFromUri size unknown");
269258
return null;
270259
}
271260
InputStream is = ctx.getContentResolver().openInputStream( uri);
272261
if ( is != null) {
273-
bytes = readBytesFromInputStream( is, size);
262+
bytes = readBytesFromInputStream( is, (int) size);
274263
is.close();
275264
}
276265
} catch ( Exception e){
277266
bytes = null;
267+
Log.e( TAG, "readBytesFromFile Exception");
268+
e.printStackTrace();
269+
}
270+
return bytes;
271+
}
272+
273+
public static byte[] newBytes(final int size) {
274+
if ( size <= 0) {
275+
return null;
276+
}
277+
278+
byte[] bytes = null;
279+
try {
280+
bytes = new byte[size];
281+
} catch ( Exception e) {
282+
bytes = null;
283+
Log.e( TAG, "newBytes(" + size + ") Exception");
284+
e.printStackTrace();
285+
} catch ( OutOfMemoryError oom) {
286+
bytes = null;
287+
Log.e( TAG, "newBytes(" + size + ") OutOfMemoryError");
278288
}
279289
return bytes;
280290
}

0 commit comments

Comments
 (0)