Skip to content
Open
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
3 changes: 1 addition & 2 deletions common/src/main/java/org/tron/core/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void resolveConfigFile(String fileName, File confFile) {
if (confFile.exists()) {
config = ConfigFactory.parseFile(confFile)
.withFallback(ConfigFactory.defaultReference());
} else if (Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)
} else if (Thread.currentThread().getContextClassLoader().getResource(fileName)
!= null) {
config = ConfigFactory.load(fileName);
} else {
Expand All @@ -59,4 +59,3 @@ private static void resolveConfigFile(String fileName, File confFile) {
}
}
}

11 changes: 4 additions & 7 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -1186,9 +1186,8 @@ public String getOutputDirectory() {
private static void printVersion() {
Properties properties = new Properties();
boolean noGitProperties = true;
try {
InputStream in = Thread.currentThread()
.getContextClassLoader().getResourceAsStream("git.properties");
try (InputStream in = Thread.currentThread()
.getContextClassLoader().getResourceAsStream("git.properties")) {
if (in != null) {
noGitProperties = false;
properties.load(in);
Expand Down Expand Up @@ -1276,9 +1275,8 @@ public static String upperFirst(String name) {

private static String getCommitIdAbbrev() {
Properties properties = new Properties();
try {
InputStream in = Thread.currentThread()
.getContextClassLoader().getResourceAsStream("git.properties");
try (InputStream in = Thread.currentThread()
.getContextClassLoader().getResourceAsStream("git.properties")) {
if (in == null) {
logger.warn("git.properties not found on classpath");
return "";
Expand Down Expand Up @@ -1315,4 +1313,3 @@ private static Map<String, String[]> getOptionGroup() {
return optionGroupMap;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ public static void librustzcashInitZksnarkParams() {
}

private static String getParamsFile(String fileName) {
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("params" + File.separator + fileName);
String resourcePath = "params" + File.separator + fileName;
Comment thread
Federico2014 marked this conversation as resolved.
File fileOut = new File(System.getProperty("java.io.tmpdir")
+ File.separator + fileName + "." + System.currentTimeMillis());
try {
try (InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(resourcePath)) {
if (in == null) {
throw new IllegalStateException("Resource not found: " + resourcePath);
Comment thread
Federico2014 marked this conversation as resolved.
}
FileUtils.copyToFile(in, fileOut);
} catch (IOException e) {
logger.error(e.getMessage(), e);
Expand Down
28 changes: 12 additions & 16 deletions framework/src/test/java/org/tron/common/utils/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -39,15 +34,16 @@ public void setUp() throws IOException {

@After
public void tearDown() throws IOException {
Files.walk(tempDir)
.sorted(Comparator.reverseOrder())
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
}
});
try (Stream<Path> paths = Files.walk(tempDir)) {
paths.sorted(Comparator.reverseOrder())
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

@Test
Expand Down Expand Up @@ -126,4 +122,4 @@ public void testCreateDirIfNotExists() {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ public void doPostTest() throws IOException {
}
Assert.assertNotNull(result);
in.close();
writer.flush();
FileInputStream fileInputStream = new FileInputStream("temp.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
writer.close();

StringBuilder sb = new StringBuilder();
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) {
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
}
Assert.assertTrue(sb.toString().contains("null"));
httpUrlConnection.disconnect();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ public void doPostTest() throws IOException {
}
Assert.assertNotNull(result);
in.close();
writer.flush();
FileInputStream fileInputStream = new FileInputStream("temp.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
writer.close();

StringBuilder sb = new StringBuilder();
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) {
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
}
Assert.assertTrue(sb.toString().contains("null"));
httpUrlConnection.disconnect();
Expand Down Expand Up @@ -185,18 +185,17 @@ public void doGetTest() throws IOException {
}
Assert.assertNotNull(result);
in.close();
writer.flush();
FileInputStream fileInputStream = new FileInputStream("temp.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
writer.close();

StringBuilder sb = new StringBuilder();
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) {
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
}
Assert.assertTrue(sb.toString().contains("null"));
httpUrlConnection.disconnect();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Properties;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -141,8 +142,10 @@ public static void copyDatabases(Path src, Path dest, List<String> subDirs)
subDirs.forEach(dir -> {
if (isExists(Paths.get(src.toString(), dir).toString())) {
try {
Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)
.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
try (Stream<Path> paths = Files.walk(
Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) {
paths.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
}
} catch (IOException e) {
logger.error("copy database failed, src: {}, dest: {}, error: {}",
Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage());
Expand All @@ -156,8 +159,10 @@ public static void copyDir(Path src, Path dest, String dir) {
if (isExists(Paths.get(src.toString(), dir).toString())) {
try {
if (createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) {
Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)
.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
try (Stream<Path> paths = Files.walk(
Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) {
paths.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
}
} else {
throw new IOException(String.format("dest %s create fail ",
Paths.get(dest.toString(), dir)));
Expand Down
Loading