@@ -196,31 +196,6 @@ public static long fileSizeFromUri( Uri uri, Context ctx) {
196
196
return -1 ;
197
197
}
198
198
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
-
224
199
public static byte [] readBytesFromInputStream (InputStream is , final int size ) {
225
200
if ( size <= 0 ) {
226
201
return null ;
@@ -241,40 +216,75 @@ public static byte[] readBytesFromInputStream(InputStream is, final int size) {
241
216
}
242
217
} catch ( Exception e ) {
243
218
bytes = null ;
219
+ Log .e ( TAG , "readBytesFromInputStream Exception" );
220
+ e .printStackTrace ();
244
221
}
245
222
return bytes ;
246
223
}
247
224
248
225
public static byte [] readBytesFromFile ( File file ) {
249
226
byte [] bytes = null ;
250
227
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
+ }
252
233
if ( size <= 0 ) {
234
+ Log .e ( TAG , "readBytesFromFile size unknown" );
253
235
return null ;
254
236
}
255
237
FileInputStream is = new FileInputStream ( file );
256
- bytes = readBytesFromInputStream (is , size );
238
+ bytes = readBytesFromInputStream (is , ( int ) size );
257
239
is .close ();
258
240
} catch ( Exception e ){
259
241
bytes = null ;
242
+ Log .e ( TAG , "readBytesFromFile Exception" );
243
+ e .printStackTrace ();
260
244
}
261
245
return bytes ;
262
246
}
263
247
264
248
public static byte [] readBytesFromUri ( Uri uri , Context ctx ) {
265
249
byte [] bytes = null ;
266
250
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
+ }
268
256
if ( size <= 0 ) {
257
+ Log .e ( TAG , "readBytesFromUri size unknown" );
269
258
return null ;
270
259
}
271
260
InputStream is = ctx .getContentResolver ().openInputStream ( uri );
272
261
if ( is != null ) {
273
- bytes = readBytesFromInputStream ( is , size );
262
+ bytes = readBytesFromInputStream ( is , ( int ) size );
274
263
is .close ();
275
264
}
276
265
} catch ( Exception e ){
277
266
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" );
278
288
}
279
289
return bytes ;
280
290
}
0 commit comments