jt2594838 commented on a change in pull request #31: Fix sonar
URL: https://github.com/apache/incubator-iotdb/pull/31#discussion_r251003971
##########
File path:
iotdb/src/main/java/org/apache/iotdb/db/postback/receiver/ServerServiceImpl.java
##########
@@ -144,40 +157,37 @@ public String startReceiving(String md5OfSender,
List<String> filePathSplit,
if (!file.getParentFile().exists()) {
try {
file.getParentFile().mkdirs();
- file.createNewFile();
+ if (file.createNewFile()) {
+ LOGGER.error("IoTDB post back receiver: cannot create file {}",
file.getAbsoluteFile());
+ }
} catch (IOException e) {
LOGGER.error("IoTDB post back receiver: cannot make file because
{}", e.getMessage());
}
}
- try {
- fos = new FileOutputStream(file, true); // append new data
+ try (FileOutputStream fos = new FileOutputStream(file, true)) {// append
new data
channel = fos.getChannel();
channel.write(dataToReceive);
- channel.close();
- fos.close();
} catch (Exception e) {
LOGGER.error("IoTDB post back receiver: cannot write data to file
because {}",
e.getMessage());
}
} else { // all data in the same file has received successfully
- try {
- FileInputStream fis = new FileInputStream(filePath);
+ try (FileInputStream fis = new FileInputStream(filePath)) {
MessageDigest md = MessageDigest.getInstance("MD5");
int mBufferSize = 8 * 1024 * 1024;
byte[] buffer = new byte[mBufferSize];
int n;
while ((n = fis.read(buffer)) != -1) {
md.update(buffer, 0, n);
}
- fis.close();
md5OfReceiver = (new BigInteger(1, md.digest())).toString(16);
if (md5OfSender.equals(md5OfReceiver)) {
fileNum.set(fileNum.get() + 1);
LOGGER.info("IoTDB post back receiver : Receiver has received " +
fileNum.get()
+ " "
+ "files from sender!");
} else {
- new File(filePath).delete();
+ Files.deleteIfExists((java.nio.file.Path) new Path(filePath));
Review comment:
I consider this problem settled, but I'll still leave a mark here in case
something is missed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services