From a050062ff4a19586d52a045e79f2f065477adec0 Mon Sep 17 00:00:00 2001 From: Tomek Kaminski Date: Fri, 23 Feb 2018 15:43:45 +0100 Subject: Fix Sonar Bug 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 --- .../org/onap/appc/ccadaptor/SshJcraftWrapper.java | 36 +++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java index d812507ea..4d81b6c24 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java @@ -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); } -- cgit 1.2.3-korg