Skip to content

Commit

Permalink
Merge pull request #10 from DevFactory/release/try-with-resource-shou…
Browse files Browse the repository at this point in the history
…ld-be-used-fix-1

Code quality fix - Try-with-resources should be used.
  • Loading branch information
YiuChoi committed May 27, 2016
2 parents 64fad2a + 539835b commit 3c4b359
Showing 1 changed file with 12 additions and 67 deletions.
79 changes: 12 additions & 67 deletions app/src/main/java/name/caiyao/microreader/utils/CacheUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ public void close() throws IOException {
*/
public void put(String key, String value) {
File file = mCache.newFile(key);
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(file), 1024);
try (BufferedWriter out = new BufferedWriter(new FileWriter(file), 1024)){
out.write(value);
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -232,9 +230,9 @@ public String getAsString(String key) {
if (!file.exists())
return null;
boolean removeFile = false;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));

try (BufferedReader in = new BufferedReader(new FileReader(file))) {

String readString = "";
String currentLine;
while ((currentLine = in.readLine()) != null) {
Expand All @@ -250,13 +248,6 @@ public String getAsString(String key) {
e.printStackTrace();
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (removeFile)
remove(key);
}
Expand Down Expand Up @@ -357,21 +348,11 @@ public JSONArray getAsJSONArray(String key) {
*/
public void put(String key, byte[] value) {
File file = mCache.newFile(key);
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
try (FileOutputStream out = new FileOutputStream(file)) {
out.write(value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
mCache.put(file);
}
}
Expand Down Expand Up @@ -417,13 +398,11 @@ public void put(String key, byte[] value, int saveTime) {
* @return byte 数据
*/
public byte[] getAsBinary(String key) {
RandomAccessFile raFile = null;
boolean removeFile = false;
try {
File file = mCache.get(key);
if (!file.exists())
return null;
raFile = new RandomAccessFile(file, "r");
File file = mCache.get(key);
if (!file.exists())
return null;
try (RandomAccessFile raFile = new RandomAccessFile(file, "r")) {
byte[] byteArray = new byte[(int) raFile.length()];
raFile.read(byteArray);
if (!Utils.isDue(byteArray)) {
Expand All @@ -436,13 +415,6 @@ public byte[] getAsBinary(String key) {
e.printStackTrace();
return null;
} finally {
if (raFile != null) {
try {
raFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (removeFile)
remove(key);
}
Expand Down Expand Up @@ -470,11 +442,8 @@ public void put(String key, Serializable value) {
* @param saveTime 保存的时间,单位:秒
*/
public void put(String key, Serializable value, int saveTime) {
ByteArrayOutputStream baos;
ObjectOutputStream oos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(value);
byte[] data = baos.toByteArray();
if (saveTime != -1) {
Expand All @@ -484,13 +453,6 @@ public void put(String key, Serializable value, int saveTime) {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
} catch (IOException ignored) {
}
}
}

Expand All @@ -503,29 +465,12 @@ public void put(String key, Serializable value, int saveTime) {
public Object getAsObject(String key) {
byte[] data = getAsBinary(key);
if (data != null) {
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
bais = new ByteArrayInputStream(data);
ois = new ObjectInputStream(bais);
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))){
Object reObject = ois.readObject();
return reObject;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (bais != null)
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
Expand Down

0 comments on commit 3c4b359

Please sign in to comment.