Skip to content
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

Resources should be closed #3

Merged
merged 1 commit into from
May 21, 2016
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,10 @@ public void notifyOnChange(String key) {
}

protected JSONObject readDataFromFile(File file) {
InputStream is = null;
Reader reader = null;
try {
InputStream is = new FileInputStream(file);
Reader reader;
is = new FileInputStream(file);
try {
reader = new InputStreamReader(is, charset);
} catch (UnsupportedEncodingException e) {
Expand All @@ -425,11 +426,18 @@ protected JSONObject readDataFromFile(File file) {
while ((c = reader.read(buffer)) > 0) {
builder.append(buffer, 0, c);
}
reader.close();
is.close();
return new JSONObject(builder.toString());
} catch (Exception e) {
return new JSONObject();
} finally {
try {
if (reader != null)
reader.close();
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/main/java/zame/game/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ public static boolean copyFile(String srcFileName, String destFileName, int erro

public static boolean copyFile(String srcFileName, String destFileName, int errorMessageResId, boolean logExceptions) {
boolean success = true;

InputStream in = null;
OutputStream out = null;
try {
InputStream in = new FileInputStream(srcFileName);
OutputStream out = new FileOutputStream(destFileName);
in = new FileInputStream(srcFileName);
out = new FileOutputStream(destFileName);

byte[] buf = new byte[1024];
int len;
Expand All @@ -169,14 +170,31 @@ public static boolean copyFile(String srcFileName, String destFileName, int erro
out.write(buf, 0, len);
}

in.close();
out.close();
} catch (Exception ex) {
if (logExceptions) {
log(ex);
}

success = false;
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
if (logExceptions) {
log(e);
}
success = false;
}
try {
if (out != null)
out.close();
} catch (IOException e) {
if (logExceptions) {
log(e);
}
success = false;
}
}

if (!success && errorMessageResId != 0) {
Expand Down