Fix Sonar Bug 85/32685/2
authorTomek Kaminski <tomasz.kaminski@nokia.com>
Fri, 23 Feb 2018 14:43:45 +0000 (15:43 +0100)
committerPatrick Brady <pb071s@att.com>
Mon, 26 Feb 2018 19:10:14 +0000 (19:10 +0000)
Fix for: https://sonar.onap.org/project/issues?id=org.onap.appc%3Aappc&issues=AWGYEYwt8TZzbCgU6gOm&open=AWGYEYwt8TZzbCgU6gOm

Change-Id: I632c21efb8e69c04fc346841900d98d4908b360e
Issue-ID: APPC-668
Signed-off-by: Tomek Kaminski <tomasz.kaminski@nokia.com>
appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java

index d812507..4d81b6c 100644 (file)
@@ -549,28 +549,28 @@ public class SshJcraftWrapper {
     }
 
     public String getLastFewLinesOfFile(File file, int linesToRead) throws IOException {
-        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
-        int lines = 0;
-        StringBuilder builder = new StringBuilder();
         String tail = "";
-        long length = file.length();
-        length--;
-        randomAccessFile.seek(length);
-        for (long seek = length; seek >= 0; --seek) {
-            randomAccessFile.seek(seek);
-            char c = (char) randomAccessFile.read();
-            builder.append(c);
-            if (c == '\n') {
-                builder = builder.reverse();
-                tail = builder.toString() + tail;
-                lines++;
-                builder.setLength(0);
-                if (lines == linesToRead) {
-                    break;
+        try(RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
+            int lines = 0;
+            StringBuilder builder = new StringBuilder();
+            long length = file.length();
+            length--;
+            randomAccessFile.seek(length);
+            for (long seek = length; seek >= 0; --seek) {
+                randomAccessFile.seek(seek);
+                char c = (char) randomAccessFile.read();
+                builder.append(c);
+                if (c == '\n') {
+                    builder = builder.reverse();
+                    tail = builder.append(tail).toString();
+                    lines++;
+                    builder.setLength(0);
+                    if (lines == linesToRead) {
+                        break;
+                    }
                 }
             }
         }
-        randomAccessFile.close();
         if (log.isDebugEnabled()) {
             log.debug("Content read from file={0} was tail={1}", file.getName(), tail);
         }