[tor-commits] [metrics-lib/master] Fix leaking resource in DescriptorReaderImpl.java
karsten at torproject.org
karsten at torproject.org
Fri Aug 23 13:31:16 UTC 2019
commit ce19c16543b410cedf3216ba477537d28cc3c818
Author: fava <fava at libertymail.net>
Date: Thu Aug 22 05:28:44 2019 +0000
Fix leaking resource in DescriptorReaderImpl.java
Fix leaking resource in DescriptorReaderImpl.java using
try-with-resource statement.
This fix is related to analysis on metrics-lib using sonarqube
Implements part of #30544
---
.../descriptor/impl/DescriptorReaderImpl.java | 70 ++++++++++++----------
1 file changed, 37 insertions(+), 33 deletions(-)
diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
index b8eb191..90ef2c6 100644
--- a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
@@ -281,41 +281,45 @@ public class DescriptorReaderImpl implements DescriptorReader {
}
private void readTarball(File file) throws IOException {
- FileInputStream in = new FileInputStream(file);
- if (in.available() <= 0) {
- return;
- }
- TarArchiveInputStream tais;
- if (file.getName().endsWith(".tar.bz2")) {
- tais = new TarArchiveInputStream(new BZip2CompressorInputStream(in));
- } else if (file.getName().endsWith(".tar.xz")) {
- tais = new TarArchiveInputStream(new XZCompressorInputStream(in));
- } else if (file.getName().endsWith(".tar")) {
- tais = new TarArchiveInputStream(in);
- } else {
- return;
- }
- BufferedInputStream bis = new BufferedInputStream(tais);
- TarArchiveEntry tae;
- while ((tae = tais.getNextTarEntry()) != null) {
- if (tae.isDirectory()) {
- continue;
+ try (FileInputStream in = new FileInputStream(file)) {
+ if (in.available() <= 0) {
+ return;
}
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int len;
- byte[] data = new byte[1024];
- while ((len = bis.read(data, 0, 1024)) >= 0) {
- baos.write(data, 0, len);
+ TarArchiveInputStream tais;
+ if (file.getName().endsWith(".tar.bz2")) {
+ tais = new TarArchiveInputStream(new BZip2CompressorInputStream(in));
+ } else if (file.getName().endsWith(".tar.xz")) {
+ tais = new TarArchiveInputStream(new XZCompressorInputStream(in));
+ } else if (file.getName().endsWith(".tar")) {
+ tais = new TarArchiveInputStream(in);
+ } else {
+ return;
}
- byte[] rawDescriptorBytes = baos.toByteArray();
- if (rawDescriptorBytes.length < 1) {
- continue;
- }
- String fileName = tae.getName().substring(
- tae.getName().lastIndexOf("/") + 1);
- for (Descriptor descriptor : this.descriptorParser.parseDescriptors(
- rawDescriptorBytes, file, fileName)) {
- this.descriptorQueue.add(descriptor);
+ try (BufferedInputStream bis = new BufferedInputStream(tais)) {
+ TarArchiveEntry tae;
+ while ((tae = tais.getNextTarEntry()) != null) {
+ if (tae.isDirectory()) {
+ continue;
+ }
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ int len;
+ byte[] data = new byte[1024];
+ while ((len = bis.read(data, 0, 1024)) >= 0) {
+ baos.write(data, 0, len);
+ }
+ byte[] rawDescriptorBytes = baos.toByteArray();
+ if (rawDescriptorBytes.length < 1) {
+ continue;
+ }
+ String fileName = tae.getName().substring(
+ tae.getName().lastIndexOf("/") + 1);
+ for (Descriptor descriptor :
+ this.descriptorParser.parseDescriptors(
+ rawDescriptorBytes, file, fileName)) {
+ this.descriptorQueue.add(descriptor);
+ }
+ }
+ }
}
}
}
More information about the tor-commits
mailing list