Skip to content

Commit

Permalink
make use of autoclosable for stream writers/readers to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
sdargutev committed Feb 23, 2018
1 parent 3f55a08 commit cc4390f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ src/main/resources/META-INF/
/.project
/.classpath
/.settings/
/.apt_generated/

# output directory
/output-directory/

18 changes: 7 additions & 11 deletions src/main/java/org/tron/common/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,23 @@ public static boolean recursiveDelete(String fileName) {
}

public static void saveData(String filePath, String data, boolean append) {
File priFile = new File(filePath);
try {
File priFile = new File(filePath);
priFile.createNewFile();
FileWriter fw = new FileWriter(priFile, append);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.flush();
bw.close();
try (BufferedWriter bw = new BufferedWriter(new FileWriter(priFile, append))) {
bw.write(data);
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static int readData(String filePath, char[] buf) {
int len;
try {
File file = new File(filePath);
FileReader fileReader = new FileReader(file);
BufferedReader bufRead = new BufferedReader(fileReader);
File file = new File(filePath);
try (BufferedReader bufRead = new BufferedReader(new FileReader(file))) {
len = bufRead.read(buf, 0, buf.length);
bufRead.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return 0;
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/tron/core/Sha256Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
*/

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.io.ByteStreams;
import com.google.common.primitives.Ints;
import com.google.protobuf.ByteString;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -30,6 +26,9 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import com.google.common.io.ByteStreams;
import com.google.common.primitives.Ints;
import com.google.protobuf.ByteString;

/**
* A Sha256Hash just wraps a byte[] so that equals and hashcode work correctly, allowing it to be
Expand Down Expand Up @@ -96,11 +95,9 @@ public static Sha256Hash of(byte[] contents) {
* @throws IOException if an error occurs while reading the file
*/
public static Sha256Hash of(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
try {

try (FileInputStream in = new FileInputStream(file)) {
return of(ByteStreams.toByteArray(in));
} finally {
in.close();
}
}

Expand Down
22 changes: 4 additions & 18 deletions src/main/java/org/tron/core/TronBlockChainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@


import static org.tron.core.Constant.LAST_HASH;

import com.google.protobuf.InvalidProtocolBufferException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand All @@ -36,6 +34,7 @@
import org.tron.core.config.SystemProperties;
import org.tron.core.db.BlockStoreInput;
import org.tron.protos.Protocal.Block;
import com.google.protobuf.InvalidProtocolBufferException;

@Component
public class TronBlockChainImpl implements TronBlockChain, org.tron.core.facade.TronBlockChain {
Expand Down Expand Up @@ -111,8 +110,6 @@ private void recordBlock(Block block) {
String dumpDir = config.databaseDir() + "/" + config.dumpDir();

File dumpFile = new File(dumpDir + "/blocks-rec.dmp");
FileWriter fw = null;
BufferedWriter bw = null;

try {

Expand All @@ -121,24 +118,13 @@ private void recordBlock(Block block) {
dumpFile.createNewFile();
}

fw = new FileWriter(dumpFile.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
try (BufferedWriter bw =
new BufferedWriter(new FileWriter(dumpFile.getAbsoluteFile(), true))) {
bw.write(Hex.toHexString(block.toByteArray()));
bw.write("\n");

}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
16 changes: 2 additions & 14 deletions src/test/java/org/tron/core/consensus/client/ReadIp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,15 @@ public class ReadIp {
* readFile from path.
*/
public String readFile(String path) {
BufferedReader reader = null;
String laststr = "";
try {
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
reader = new BufferedReader(inputStreamReader);
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"))) {
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr += tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return laststr;
}
Expand Down

0 comments on commit cc4390f

Please sign in to comment.