jixuan1989 commented on a change in pull request #31: Fix sonar
URL: https://github.com/apache/incubator-iotdb/pull/31#discussion_r250971966
 
 

 ##########
 File path: 
iotdb/src/main/java/org/apache/iotdb/db/auth/role/LocalFileRoleAccessor.java
 ##########
 @@ -100,24 +100,25 @@ public Role loadRole(String rolename) throws IOException 
{
   public void saveRole(Role role) throws IOException {
     File roleProfile = new File(
         roleDirPath + File.separator + role.getName() + 
IoTDBConstant.PROFILE_SUFFIX + TEMP_SUFFIX);
-    BufferedOutputStream outputStream = new BufferedOutputStream(new 
FileOutputStream(roleProfile));
-    try {
-      IOUtils.writeString(outputStream, role.getName(), STRING_ENCODING, 
encodingBufferLocal);
-
-      role.getPrivilegeList().sort(PathPrivilege.referenceDescentSorter);
-      int privilegeNum = role.getPrivilegeList().size();
-      IOUtils.writeInt(outputStream, privilegeNum, encodingBufferLocal);
-      for (int i = 0; i < privilegeNum; i++) {
-        PathPrivilege pathPrivilege = role.getPrivilegeList().get(i);
-        IOUtils
-            .writePathPrivilege(outputStream, pathPrivilege, STRING_ENCODING, 
encodingBufferLocal);
+    try (BufferedOutputStream outputStream = new BufferedOutputStream(new 
FileOutputStream(roleProfile))) {
+      try {
+        IOUtils.writeString(outputStream, role.getName(), STRING_ENCODING, 
encodingBufferLocal);
+
+        role.getPrivilegeList().sort(PathPrivilege.referenceDescentSorter);
+        int privilegeNum = role.getPrivilegeList().size();
+        IOUtils.writeInt(outputStream, privilegeNum, encodingBufferLocal);
+        for (int i = 0; i < privilegeNum; i++) {
+          PathPrivilege pathPrivilege = role.getPrivilegeList().get(i);
+          IOUtils
+              .writePathPrivilege(outputStream, pathPrivilege, STRING_ENCODING,
+                  encodingBufferLocal);
+        }
+
+      } catch (Exception e) {
+        throw new IOException(e.getMessage());
+      } finally {
+        outputStream.flush();
 
 Review comment:
   put the flush() operation in try{ } rather than finally{}. 
   And `LocalFileUserAccessor.java` has the same problem.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to