Skip to content

Commit e8a328e

Browse files
vmassolKimi Code
andcommitted
[Misc] Use try-with-resources where semantics are preserved (SonarQube java:S2093)
* AbstractFileLoggerTail: extracted the read block into a private helper holding the try-with-resources (checkstyle NestedTryDepth); close order and error path unchanged. Co-Authored-By: Kimi Code <noreply@moonshot.cn>
1 parent 2b48f57 commit e8a328e

5 files changed

Lines changed: 18 additions & 32 deletions

File tree

xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/ExtensionPackager.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ public void generateExtension(String classPackageFolder, URL descriptorUrl) thro
157157
// Make sure the folder exists
158158
packageFile.getParentFile().mkdirs();
159159

160-
FileOutputStream fos = new FileOutputStream(packageFile);
161-
try {
160+
try (FileOutputStream fos = new FileOutputStream(packageFile)) {
162161
ZipOutputStream zos;
163162
if (type.equals("jar")) {
164163
Manifest manifest = new Manifest();
@@ -187,8 +186,6 @@ public void generateExtension(String classPackageFolder, URL descriptorUrl) thro
187186

188187
// Register the extension
189188
this.extensionsFiles.put(new ExtensionId(id, version), packageFile);
190-
} finally {
191-
fos.close();
192189
}
193190
}
194191

xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/FileExtensionRepository.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,13 @@ public boolean accept(File dir, String name)
167167
return name.startsWith(id + '-') && name.endsWith(".xed");
168168
}
169169
})) {
170-
FileInputStream fis = null;
171-
try {
172-
fis = new FileInputStream(file);
173-
170+
try (FileInputStream fis = new FileInputStream(file)) {
174171
DefaultLocalExtension localExtension =
175172
this.extensionSerializer.loadLocalExtensionDescriptor(null, fis);
176173

177174
if (localExtension.getId().getId().equals(id)) {
178175
versions.add(localExtension.getId().getVersion());
179176
}
180-
} finally {
181-
if (fis != null) {
182-
fis.close();
183-
}
184177
}
185178
}
186179
} catch (Exception e) {

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-test/src/main/java/org/xwiki/filter/test/integration/TestDataParser.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,9 @@ public TestResourceData parse(InputStream source, String resourceName) throws IO
111111
TestResourceData data = new TestResourceData();
112112

113113
// Resources should always be encoded as UTF-8, to reduce the dependency on the system encoding
114-
BufferedReader reader = new BufferedReader(new InputStreamReader(source, UTF8));
115-
116114
// Read each line and look for lines starting with ".". When this happens it means we've found a separate
117115
// test case.
118-
try {
116+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(source, UTF8))) {
119117
String action = null;
120118
String typeId = null;
121119
boolean skip = false;
@@ -168,9 +166,6 @@ public TestResourceData parse(InputStream source, String resourceName) throws IO
168166
if (!skip) {
169167
saveData(data, action, typeId, buffer, configuration);
170168
}
171-
172-
} finally {
173-
reader.close();
174169
}
175170

176171
return data;

xwiki-commons-core/xwiki-commons-filter/xwiki-commons-filter-test/src/main/java/org/xwiki/filter/test/internal/ZIPFileAssertComparator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ private static Map<String, byte[]> unzip(File filename) throws IOException
6666
{
6767
Map<String, byte[]> zipContent = new HashMap<>();
6868

69-
ZipFile zipFile = new ZipFile(filename);
70-
71-
try {
69+
try (ZipFile zipFile = new ZipFile(filename)) {
7270
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
7371
while (entries.hasMoreElements()) {
7472
ZipArchiveEntry entry = entries.nextElement();
@@ -80,8 +78,6 @@ private static Map<String, byte[]> unzip(File filename) throws IOException
8078
inputStream.close();
8179
}
8280
}
83-
} finally {
84-
zipFile.close();
8581
}
8682

8783
return zipContent;

xwiki-commons-core/xwiki-commons-logging/xwiki-commons-logging-common/src/main/java/org/xwiki/logging/internal/tail/AbstractFileLoggerTail.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,7 @@ private LogEvent getLogEvent(int index, IndexEntry indexEntry)
322322
boolean open = open();
323323

324324
try {
325-
this.logStore.seek(indexEntry.position);
326-
327-
Long toPosition =
328-
this.index.size() > index + 1 ? this.index.get(index + 1).position : this.logStore.length();
329-
330-
BoundedInputStream stream = new BoundedInputStream(new InputStreamDataInput(this.logStore),
331-
toPosition - indexEntry.position);
332-
333-
return read(stream);
325+
return readLogEvent(index, indexEntry);
334326
} finally {
335327
close(open);
336328
}
@@ -342,6 +334,19 @@ private LogEvent getLogEvent(int index, IndexEntry indexEntry)
342334
}
343335
}
344336

337+
private LogEvent readLogEvent(int index, IndexEntry indexEntry) throws IOException
338+
{
339+
this.logStore.seek(indexEntry.position);
340+
341+
Long toPosition = this.index.size() > index + 1 ? this.index.get(index + 1).position : this.logStore.length();
342+
343+
try (BoundedInputStream stream = new BoundedInputStream(new InputStreamDataInput(this.logStore),
344+
toPosition - indexEntry.position))
345+
{
346+
return read(stream);
347+
}
348+
}
349+
345350
private void checkChanged()
346351
{
347352
// Make sure the log still exist and is not empty

0 commit comments

Comments
 (0)