Skip to content

Fix FileUtils #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions app/src/main/java/com/samsung/microbit/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,50 +197,89 @@ public static long fileSizeFromUri( Uri uri, Context ctx) {
}

public static byte[] readBytesFromInputStream(InputStream is, final int size) {
if ( size <= 0) {
return null;
}

byte[] bytes = null;
try {
bytes = new byte[size];
bytes = newBytes(size);
if ( bytes == null) {
return null;
}
int remain = size;
int offset = 0;
while ( remain > 0) {
int read = is.read( bytes, offset, remain);
remain -= read;
offset += read;
}
} catch ( Exception e){
} catch ( Exception e) {
bytes = null;
Log.e( TAG, "readBytesFromInputStream Exception");
e.printStackTrace();
}
return bytes;
}

public static byte[] readBytesFromFile( File file) {
byte[] bytes = null;
try {
long size = file.length();
if ( size <= 0 || size > Integer.MAX_VALUE) {
Log.e( TAG, "readBytesFromFile error - size");
return null;
}
FileInputStream is = new FileInputStream( file);
int size = (int) file.length();
bytes = readBytesFromInputStream(is, size);
bytes = readBytesFromInputStream(is, (int) size);
is.close();
} catch ( Exception e){
bytes = null;
Log.e( TAG, "readBytesFromFile Exception");
e.printStackTrace();
}
return bytes;
}

public static byte[] readBytesFromUri( Uri uri, Context ctx) {
byte[] bytes = null;
try {
long size = fileSizeFromUri( uri, ctx);
if ( size <= 0 || size > Integer.MAX_VALUE) {
Log.e( TAG, "readBytesFromUri error - size");
return null;
}
InputStream is = ctx.getContentResolver().openInputStream( uri);
if ( is != null) {
int size = (int) fileSizeFromUri( uri, ctx);
bytes = readBytesFromInputStream( is, size);
bytes = readBytesFromInputStream( is, (int) size);
is.close();
}
} catch ( Exception e){
bytes = null;
Log.e( TAG, "readBytesFromFile Exception");
e.printStackTrace();
}
return bytes;
}

public static byte[] newBytes(final int size) {
if ( size <= 0) {
return null;
}

byte[] bytes = null;
try {
bytes = new byte[size];
} catch ( Exception e) {
bytes = null;
Log.e( TAG, "newBytes(" + size + ") Exception");
e.printStackTrace();
} catch ( OutOfMemoryError oom) {
bytes = null;
Log.e( TAG, "newBytes(" + size + ") OutOfMemoryError");
}
return bytes;
}

public static boolean stringBuilderAddStream( StringBuilder sb, InputStream is) {
boolean ok = true;
Expand Down