Skip to content

Commit 3ec6b34

Browse files
bactgoneall
authored andcommitted
Ensure no trailing newline
Also use try-resource to manage the closing of the resource instead of checking it in finally Signed-off-by: Arthit Suriyawongkul <[email protected]>
1 parent ecd647d commit 3ec6b34

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,27 @@ public static String removeCommentChars(String s) {
9898
return "";
9999
}
100100
StringBuilder sb = new StringBuilder();
101-
BufferedReader reader = null;
102-
try {
103-
reader = new BufferedReader(new StringReader(s));
101+
try (BufferedReader reader = new BufferedReader(new StringReader(s))) {
104102
String line = reader.readLine();
103+
boolean firstLine = true;
105104
while (line != null) {
106105
line = END_COMMENT_PATTERN.matcher(line).replaceAll("");
107106
line = START_COMMENT_PATTERN.matcher(line).replaceAll("");
108107
line = BEGIN_OPTIONAL_COMMENT_PATTERN.matcher(line).replaceAll("<<beginOptional>>");
108+
109+
if (!firstLine) {
110+
sb.append("\n");
111+
} else {
112+
firstLine = false;
113+
}
109114
sb.append(line);
110-
sb.append("\n");
111115
line = reader.readLine();
112116
}
113-
return sb.toString();
114117
} catch (IOException e) {
115118
logger.warn("IO error reading strings?!?", e);
116119
return s;
117-
} finally {
118-
if (Objects.nonNull(reader)) {
119-
try {
120-
reader.close();
121-
} catch (IOException e) {
122-
logger.warn("IO error closing a string reader?!?", e);
123-
}
124-
}
125120
}
121+
return sb.toString();
126122
}
127123

128124
/**

0 commit comments

Comments
 (0)