You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/09/04 12:18:05 UTC
[commons-compress] branch master updated (df7172f4 -> 8d05bddb)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
from df7172f4 Bump github/codeql-action from 2.21.4 to 2.21.5 (#409)
new 43354810 Add testPaxTimeFieldsForInvalidValues
new 2ca85910 Javadoc
new 5e04c84f Javadoc
new bff667e5 Comment empty block
new 89391cb0 Comment
new 331de296 Use try-with-resources
new 9cf07fa5 Bullet-proof test
new 7e384b59 Bullet-proof test
new fa4f1c48 Use try-with-resources
new a54b1f3f Use try-with-resources
new 3b2adb49 Use try-with-resources
new 4aa4b1ab Comment empty block
new 2e4fdff7 Use try-with-resources
new c284c522 Use try-with-resources
new b220635b Use try-with-resources
new 58390f0d Use try-with-resources
new 35584738 Use try-with-resources
new 66f0b3be Use try-with-resources
new 8d05bddb Use try-with-resources in tests
The 19 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../compress/archivers/dump/DumpArchiveUtil.java | 29 +-
.../compress/archivers/tar/TarArchiveEntry.java | 20 +-
.../archivers/tar/TarArchiveOutputStream.java | 40 +--
.../lzma/LZMACompressorOutputStream.java | 1 +
.../harmony/archive/internal/nls/Messages.java | 2 +-
.../archivers/sevenz/AES256SHA256DecoderTest.java | 27 +-
.../compress/archivers/sevenz/SevenZFileTest.java | 3 +
.../archivers/sevenz/SevenZOutputFileTest.java | 4 +-
.../archivers/tar/TarArchiveEntryTest.java | 24 ++
.../harmony/pack200/tests/PackingOptionsTest.java | 313 +++++++++------------
.../harmony/unpack200/tests/ArchiveTest.java | 49 ++--
.../harmony/unpack200/tests/SegmentTest.java | 47 ++--
.../BoundedSeekableByteChannelInputStreamTest.java | 4 +-
.../utils/ChecksumCalculatingInputStreamTest.java | 45 +--
.../utils/ChecksumVerifyingInputStreamTest.java | 31 +-
.../utils/FixedLengthBlockOutputStreamTest.java | 40 +--
.../MultiReadOnlySeekableByteChannelTest.java | 35 ++-
.../ZipSplitReadOnlySeekableByteChannelTest.java | 41 +--
18 files changed, 388 insertions(+), 367 deletions(-)
[commons-compress] 03/19: Javadoc
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 5e04c84f9aa3b53b9bf53108cb2e0e307e97a4d4
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:14:36 2023 -0400
Javadoc
---
.../archivers/tar/TarArchiveOutputStream.java | 40 +++++++++++++---------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
index f1cc96af..7b4ebe34 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
@@ -115,7 +115,9 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
/**
* Indicates if putArchiveEntry has been called without closeArchiveEntry
*/
+
private boolean haveUnclosedEntry;
+
/**
* indicates if this archive is finished
*/
@@ -126,13 +128,16 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
private final CountingOutputStream countingOut;
private final ZipEncoding zipEncoding;
- // the provided encoding (for unit tests)
+
+ /**
+ * The provided encoding (for unit tests).
+ */
final String encoding;
private boolean addPaxHeadersForNonAsciiNames;
/**
- * Constructor for TarArchiveOutputStream.
+ * Constructs a new instance.
*
* <p>Uses a block size of 512 bytes.</p>
*
@@ -143,7 +148,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Constructor for TarArchiveOutputStream.
+ * Constructs a new instance.
*
* @param os the output stream to use
* @param blockSize the block size to use. Must be a multiple of 512 bytes.
@@ -153,7 +158,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Constructor for TarArchiveOutputStream.
+ * Constructs a new instance.
*
* @param os the output stream to use
* @param blockSize the block size to use
@@ -169,7 +174,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
/**
- * Constructor for TarArchiveOutputStream.
+ * Constructs a new instance.
*
* @param os the output stream to use
* @param blockSize the block size to use . Must be a multiple of 512 bytes.
@@ -191,7 +196,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Constructor for TarArchiveOutputStream.
+ * Constructs a new instance.
*
* @param os the output stream to use
* @param blockSize the block size to use. Must be a multiple of 512 bytes.
@@ -220,7 +225,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Constructor for TarArchiveOutputStream.
+ * Constructs a new instance.
*
* <p>Uses a block size of 512 bytes.</p>
*
@@ -325,7 +330,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Close an entry. This method MUST be called for all file entries that contain data. The reason
+ * Closes an entry. This method MUST be called for all file entries that contain data. The reason
* is that we must buffer data written to the stream in order to satisfy the buffer's record
* based writes. Thus, there may be data fragments still being assembled that must be written to
* the output stream before this entry is closed and the next entry written.
@@ -429,7 +434,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Ends the TAR archive without closing the underlying OutputStream.
+ * Finishes the TAR archive without closing the underlying OutputStream.
*
* An archive consists of a series of file entries terminated by an
* end-of-archive entry, which consists of two 512 blocks of zero bytes.
@@ -470,7 +475,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Get the record size being used by this stream's TarBuffer.
+ * Gets the record size being used by this stream's TarBuffer.
*
* @return The TarBuffer record size.
* @deprecated
@@ -543,7 +548,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Put an entry on the output stream. This writes the entry's header record and positions the
+ * Puts an entry on the output stream. This writes the entry's header record and positions the
* output stream for writing the contents of the entry. Once this method is called, the stream
* is ready for calls to write() to write the entry's contents. Once the contents are written,
* closeArchiveEntry() <B>MUST</B> be called to ensure that all buffered data is completely
@@ -623,7 +628,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Whether to add a PAX extension header for non-ASCII file names.
+ * Sets whether to add a PAX extension header for non-ASCII file names.
*
* @param b whether to add a PAX extension header for non-ASCII file names.
* @since 1.4
@@ -633,7 +638,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Set the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_STAR(1) or
+ * Sets the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_STAR(1) or
* BIGNUMBER_POSIX(2). This specifies the treatment of big files (sizes >
* TarConstants.MAXSIZE) and other numeric values too big to fit into a traditional tar header.
* Default is BIGNUMBER_ERROR.
@@ -646,7 +651,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Set the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1), LONGFILE_GNU(2) or
+ * Sets the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1), LONGFILE_GNU(2) or
* LONGFILE_POSIX(3). This specifies the treatment of long file names (names >=
* TarConstants.NAMELEN). Default is LONGFILE_ERROR.
*
@@ -657,6 +662,9 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
+ * Tests whether the character could lead to problems when used inside a TarArchiveEntry name
+ * for a PAX header.
+ *
* @return true if the character could lead to problems when used inside a TarArchiveEntry name
* for a PAX header.
*/
@@ -714,7 +722,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Write an EOF (end of archive) record to the tar archive. An EOF record consists of a record
+ * Writes an EOF (end of archive) record to the tar archive. An EOF record consists of a record
* of all zeros.
*/
private void writeEOFRecord() throws IOException {
@@ -746,7 +754,7 @@ public class TarArchiveOutputStream extends ArchiveOutputStream {
}
/**
- * Write an archive record to the archive.
+ * Writes an archive record to the archive.
*
* @param record The record data to write to the archive.
* @throws IOException on error
[commons-compress] 11/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 3b2adb490b9ed8386b5510139ca1dc8c3001a308
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:42:23 2023 -0400
Use try-with-resources
---
.../compress/utils/FixedLengthBlockOutputStreamTest.java | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
index d0c188b6..4c40bb62 100644
--- a/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
@@ -315,18 +315,21 @@ public class FixedLengthBlockOutputStreamTest {
@Test
public void testWriteFailsAfterFLClosedThrowsException() {
assertThrowsExactly(ClosedChannelException.class, () -> {
- final FixedLengthBlockOutputStream out = newClosedFLBOS();
- out.write(1);
+ try (FixedLengthBlockOutputStream out = newClosedFLBOS()) {
+ out.write(1);
+ }
}, "expected Closed Channel Exception");
assertThrowsExactly(ClosedChannelException.class, () -> {
- final FixedLengthBlockOutputStream out = newClosedFLBOS();
- out.write(new byte[]{0, 1, 2, 3});
+ try (FixedLengthBlockOutputStream out = newClosedFLBOS()) {
+ out.write(new byte[] { 0, 1, 2, 3 });
+ }
}, "expected Closed Channel Exception");
assertThrowsExactly(ClosedChannelException.class, () -> {
- final FixedLengthBlockOutputStream out = newClosedFLBOS();
- out.write(ByteBuffer.wrap(new byte[]{0, 1, 2, 3}));
+ try (FixedLengthBlockOutputStream out = newClosedFLBOS()) {
+ out.write(ByteBuffer.wrap(new byte[] { 0, 1, 2, 3 }));
+ }
}, "expected Closed Channel Exception");
}
[commons-compress] 19/19: Use try-with-resources in tests
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 8d05bddb3d689c913fa5c5acd01671ade19de9d6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Sep 4 08:17:51 2023 -0400
Use try-with-resources in tests
---
.../harmony/pack200/tests/PackingOptionsTest.java | 313 +++++++++------------
1 file changed, 130 insertions(+), 183 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java b/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
index 1f4aad23..8a7107b4 100644
--- a/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
+++ b/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
@@ -64,33 +64,25 @@ public class PackingOptionsTest {
final JarEntry entry2 = jarFile2.getJarEntry(name);
assertNotNull(entry2, "Missing Entry: " + name);
// assertEquals(entry.getTime(), entry2.getTime());
- if (!name.equals("META-INF/MANIFEST.MF")) { // Manifests aren't
- // necessarily
- // byte-for-byte
- // identical
-
+ if (!name.equals("META-INF/MANIFEST.MF")) {
+ // Manifests aren't necessarily byte-for-byte identical
final InputStream ours = jarFile.getInputStream(entry);
final InputStream expected = jarFile2.getInputStream(entry2);
- final BufferedReader reader1 = new BufferedReader(
- new InputStreamReader(ours));
- final BufferedReader reader2 = new BufferedReader(
- new InputStreamReader(expected));
- String line1 = reader1.readLine();
- String line2 = reader2.readLine();
- int i = 1;
- while (line1 != null || line2 != null) {
- assertEquals(line2, line1, "Unpacked files differ for " + name);
- line1 = reader1.readLine();
- line2 = reader2.readLine();
- i++;
+ try (BufferedReader reader1 = new BufferedReader(new InputStreamReader(ours));
+ final BufferedReader reader2 = new BufferedReader(new InputStreamReader(expected))) {
+ String line1 = reader1.readLine();
+ String line2 = reader2.readLine();
+ int i = 1;
+ while (line1 != null || line2 != null) {
+ assertEquals(line2, line1, "Unpacked files differ for " + name + " at line " + i);
+ line1 = reader1.readLine();
+ line2 = reader2.readLine();
+ i++;
+ }
}
- reader1.close();
- reader2.close();
}
}
- jarFile.close();
- jarFile2.close();
}
private void compareJarEntries(final JarFile jarFile, final JarFile jarFile2) {
@@ -122,8 +114,7 @@ public class PackingOptionsTest {
@Test
public void testE0() throws Pack200Exception, IOException,
URISyntaxException {
- final File f1 = new File(Archive.class.getResource(
- "/pack200/jndi.jar").toURI());
+ final File f1 = new File(Archive.class.getResource("/pack200/jndi.jar").toURI());
in = new JarFile(f1);
file = File.createTempFile("jndiE0", ".pack");
file.deleteOnExit();
@@ -135,8 +126,9 @@ public class PackingOptionsTest {
archive.pack();
in.close();
out.close();
- compareFiles(new JarFile(f1), new JarFile(file));
-
+ try (JarFile jf1 = new JarFile(f1); JarFile jf2 = new JarFile(file)) {
+ compareFiles(jf1, jf2);
+ }
}
@Test
@@ -274,8 +266,7 @@ public class PackingOptionsTest {
options.setLogFile(logFile.getPath());
assertEquals(logFile.getPath(), options.getLogFile());
- in = new JarFile(new File(Archive.class.getResource(
- "/pack200/hw.jar").toURI()));
+ in = new JarFile(new File(Archive.class.getResource("/pack200/hw.jar").toURI()));
file = File.createTempFile("helloworld", ".pack.gz");
file.deleteOnExit();
out = new FileOutputStream(file);
@@ -284,13 +275,12 @@ public class PackingOptionsTest {
out.close();
// log file should be empty
- FileReader reader = new FileReader(logFile);
- assertFalse(reader.ready());
- reader.close();
+ try (FileReader reader = new FileReader(logFile)) {
+ assertFalse(reader.ready());
+ }
options.setVerbose(true);
- in = new JarFile(new File(Archive.class.getResource(
- "/pack200/hw.jar").toURI()));
+ in = new JarFile(new File(Archive.class.getResource("/pack200/hw.jar").toURI()));
file = File.createTempFile("helloworld", ".pack.gz");
file.deleteOnExit();
out = new FileOutputStream(file);
@@ -299,9 +289,9 @@ public class PackingOptionsTest {
out.close();
// log file should not be empty
- reader = new FileReader(logFile);
- assertTrue(reader.ready());
- reader.close();
+ try (FileReader reader = new FileReader(logFile)) {
+ assertTrue(reader.ready());
+ }
}
@Test
@@ -412,116 +402,88 @@ public class PackingOptionsTest {
@Test
public void testNewAttributes() throws Exception {
- in = new JarFile(
- new File(
- Archive.class
- .getResource(
- "/pack200/jndiWithUnknownAttributes.jar")
- .toURI()));
file = File.createTempFile("unknown", ".pack");
- file.deleteOnExit();
- out = new FileOutputStream(file);
- final PackingOptions options = new PackingOptions();
- options.addClassAttributeAction("Pack200", "I");
- final Archive ar = new Archive(in, out, options);
- ar.pack();
- in.close();
- out.close();
+ try (FileOutputStream out = new FileOutputStream(file);
+ JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI()))) {
+ file.deleteOnExit();
+ final PackingOptions options = new PackingOptions();
+ options.addClassAttributeAction("Pack200", "I");
+ final Archive ar = new Archive(in, out, options);
+ ar.pack();
+ }
// unpack and check this was done right
- final InputStream in2 = new FileInputStream(file);
final File file2 = File.createTempFile("unknown", ".jar");
file2.deleteOnExit();
- final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
- final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(
- in2, out2);
- u2archive.unpack();
-
- // compare with original
- final File compareFile = new File(Archive.class.getResource(
- "/pack200/jndiWithUnknownAttributes.jar").toURI());
- final JarFile jarFile = new JarFile(file2);
-
- final JarFile jarFile2 = new JarFile(compareFile);
- assertEquals(jarFile2.size(), jarFile.size());
- compareJarEntries(jarFile, jarFile2);
+ try (InputStream in2 = new FileInputStream(file); JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2))) {
+ final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
+ u2archive.unpack();
+
+ // compare with original
+ final File compareFile = new File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI());
+ try (JarFile jarFile = new JarFile(file2); JarFile jarFile2 = new JarFile(compareFile)) {
+ assertEquals(jarFile2.size(), jarFile.size());
+ compareJarEntries(jarFile, jarFile2);
// compareFiles(jarFile, jarFile2);
+ }
+ }
}
@Test
public void testNewAttributes2() throws Exception {
- in = new JarFile(
- new File(
- Archive.class
- .getResource(
- "/pack200/p200WithUnknownAttributes.jar")
- .toURI()));
file = File.createTempFile("unknown", ".pack");
file.deleteOnExit();
- out = new FileOutputStream(file);
- final PackingOptions options = new PackingOptions();
- options.addFieldAttributeAction("Pack200", "I");
- options.addMethodAttributeAction("Pack200", "I");
- options.addCodeAttributeAction("Pack200", "I");
- final Archive ar = new Archive(in, out, options);
- ar.pack();
- in.close();
- out.close();
-
+ try (FileOutputStream out = new FileOutputStream(file);
+ JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/p200WithUnknownAttributes.jar").toURI()))) {
+ final PackingOptions options = new PackingOptions();
+ options.addFieldAttributeAction("Pack200", "I");
+ options.addMethodAttributeAction("Pack200", "I");
+ options.addCodeAttributeAction("Pack200", "I");
+ final Archive ar = new Archive(in, out, options);
+ ar.pack();
+ }
// unpack and check this was done right
- final InputStream in2 = new FileInputStream(file);
final File file2 = File.createTempFile("unknown", ".jar");
file2.deleteOnExit();
- final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
- final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(
- in2, out2);
- u2archive.unpack();
+ try (InputStream in2 = new FileInputStream(file); JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2))) {
+ final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
+ u2archive.unpack();
+ }
// compare with original
- final File compareFile = new File(Archive.class.getResource(
- "/pack200/p200WithUnknownAttributes.jar").toURI());
- final JarFile jarFile = new JarFile(file2);
-
- final JarFile jarFile2 = new JarFile(compareFile);
- assertEquals(jarFile2.size(), jarFile.size());
- compareJarEntries(jarFile, jarFile2);
+ final File compareFile = new File(Archive.class.getResource("/pack200/p200WithUnknownAttributes.jar").toURI());
+ try (JarFile jarFile = new JarFile(file2); JarFile jarFile2 = new JarFile(compareFile)) {
+ assertEquals(jarFile2.size(), jarFile.size());
+ compareJarEntries(jarFile, jarFile2);
+ }
}
@Test
public void testPassAttributes() throws Exception {
- in = new JarFile(
- new File(
- Archive.class
- .getResource(
- "/pack200/jndiWithUnknownAttributes.jar")
- .toURI()));
file = File.createTempFile("unknown", ".pack");
file.deleteOnExit();
- out = new FileOutputStream(file);
- final PackingOptions options = new PackingOptions();
- options.addClassAttributeAction("Pack200", "pass");
- final Archive ar = new Archive(in, out, options);
- ar.pack();
- in.close();
- out.close();
+ try (FileOutputStream out = new FileOutputStream(file);
+ JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI()))) {
+ final PackingOptions options = new PackingOptions();
+ options.addClassAttributeAction("Pack200", "pass");
+ final Archive ar = new Archive(in, out, options);
+ ar.pack();
+ }
// now unpack
- final InputStream in2 = new FileInputStream(file);
final File file2 = File.createTempFile("unknown", ".jar");
file2.deleteOnExit();
- final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
- final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(
- in2, out2);
- u2archive.unpack();
+ try (InputStream in2 = new FileInputStream(file); JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2))) {
+ final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
+ u2archive.unpack();
+ }
// compare with original
- final File compareFile = new File(Archive.class.getResource(
- "/pack200/jndiWithUnknownAttributes.jar").toURI());
- final JarFile jarFile = new JarFile(file2);
-
- final JarFile jarFile2 = new JarFile(compareFile);
- assertEquals(jarFile2.size(), jarFile.size());
- compareJarEntries(jarFile, jarFile2);
+ final File compareFile = new File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI());
+ try (JarFile jarFile = new JarFile(file2); JarFile jarFile2 = new JarFile(compareFile)) {
+ assertEquals(jarFile2.size(), jarFile.size());
+ compareJarEntries(jarFile, jarFile2);
+ }
}
@Test
@@ -580,32 +542,30 @@ public class PackingOptionsTest {
"If more files are passed then the pack file should be larger");
// now unpack
- final InputStream in2 = new FileInputStream(file);
final File file3 = File.createTempFile("sql", ".jar");
file3.deleteOnExit();
+ final InputStream in2 = new FileInputStream(file);
final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file3));
org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(
in2, out2);
u2archive.unpack();
- final File compareFile = new File(Archive.class.getResource(
- "/pack200/sqlUnpacked.jar").toURI());
- JarFile jarFile = new JarFile(file3);
-
- JarFile jarFile2 = new JarFile(compareFile);
- // Check that both jars have the same entries
- compareJarEntries(jarFile, jarFile2);
-
+ final File compareFile = new File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI());
+ try (JarFile jarFile = new JarFile(file3);
+ JarFile jarFile2 = new JarFile(compareFile)) {
+ // Check that both jars have the same entries
+ compareJarEntries(jarFile, jarFile2);
+ }
// now unpack the file with lots of passed files
- final InputStream in3 = new FileInputStream(file2);
final File file4 = File.createTempFile("sql", ".jar");
file4.deleteOnExit();
- final JarOutputStream out3 = new JarOutputStream(new FileOutputStream(file4));
- u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(in3, out3);
- u2archive.unpack();
- jarFile = new JarFile(file4);
- jarFile2 = new JarFile(compareFile);
- compareJarEntries(jarFile, jarFile2);
+ try (InputStream in3 = new FileInputStream(file2); JarOutputStream out3 = new JarOutputStream(new FileOutputStream(file4))) {
+ u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(in3, out3);
+ u2archive.unpack();
+ }
+ try (JarFile jarFile = new JarFile(file4); JarFile jarFile2 = new JarFile(compareFile)) {
+ compareJarEntries(jarFile, jarFile2);
+ }
}
// public void testE0again() throws IOException, Pack200Exception,
@@ -627,77 +587,64 @@ public class PackingOptionsTest {
@Test
public void testSegmentLimits() throws IOException, Pack200Exception,
URISyntaxException {
- in = new JarFile(new File(Archive.class.getResource(
- "/pack200/hw.jar").toURI()));
file = File.createTempFile("helloworld", ".pack.gz");
file.deleteOnExit();
- out = new FileOutputStream(file);
- PackingOptions options = new PackingOptions();
- options.setSegmentLimit(0);
- Archive archive = new Archive(in, out, options);
- archive.pack();
- in.close();
- out.close();
+ try (JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/hw.jar").toURI()));
+ FileOutputStream out = new FileOutputStream(file)) {
+ PackingOptions options = new PackingOptions();
+ options.setSegmentLimit(0);
+ Archive archive = new Archive(in, out, options);
+ archive.pack();
+ }
- in = new JarFile(new File(Archive.class.getResource(
- "/pack200/hw.jar").toURI()));
file = File.createTempFile("helloworld", ".pack.gz");
file.deleteOnExit();
- out = new FileOutputStream(file);
- options = new PackingOptions();
- options.setSegmentLimit(-1);
- archive = new Archive(in, out, options);
- archive.pack();
- in.close();
- out.close();
+ try (JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/hw.jar").toURI()));
+ FileOutputStream out = new FileOutputStream(file)) {
+ PackingOptions options = new PackingOptions();
+ options.setSegmentLimit(-1);
+ Archive archive = new Archive(in, out, options);
+ archive.pack();
+ }
- in = new JarFile(new File(Archive.class.getResource(
- "/pack200/hw.jar").toURI()));
file = File.createTempFile("helloworld", ".pack.gz");
file.deleteOnExit();
- out = new FileOutputStream(file);
- options = new PackingOptions();
- options.setSegmentLimit(5000);
- archive = new Archive(in, out, options);
- archive.pack();
- in.close();
- out.close();
+ try (JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/hw.jar").toURI()));
+ FileOutputStream out = new FileOutputStream(file)) {
+ PackingOptions options = new PackingOptions();
+ options.setSegmentLimit(5000);
+ Archive archive = new Archive(in, out, options);
+ archive.pack();
+ }
}
@Test
public void testStripDebug() throws IOException, Pack200Exception,
URISyntaxException {
- in = new JarFile(new File(Archive.class.getResource(
- "/pack200/sqlUnpacked.jar").toURI()));
file = File.createTempFile("sql", ".pack");
file.deleteOnExit();
- out = new FileOutputStream(file);
- final PackingOptions options = new PackingOptions();
- options.setGzip(false);
- options.setStripDebug(true);
- final Archive archive = new Archive(in, out, options);
- archive.pack();
- in.close();
- out.close();
+ try (JarFile in = new JarFile(new File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI()));
+ FileOutputStream out = new FileOutputStream(file)) {
+ final PackingOptions options = new PackingOptions();
+ options.setGzip(false);
+ options.setStripDebug(true);
+ final Archive archive = new Archive(in, out, options);
+ archive.pack();
+ }
// now unpack
- final InputStream in2 = new FileInputStream(file);
final File file2 = File.createTempFile("sqloutNoDebug", ".jar");
file2.deleteOnExit();
- final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
- final org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(
- in2, out2);
- u2archive.unpack();
-
- final File compareFile = new File(Archive.class.getResource(
- "/pack200/sqlUnpackedNoDebug.jar")
- .toURI());
- final JarFile jarFile = new JarFile(file2);
- assertTrue(file2.length() < 250000);
-
- final JarFile jarFile2 = new JarFile(compareFile);
+ try (InputStream in2 = new FileInputStream(file); final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2))) {
+ org.apache.commons.compress.harmony.unpack200.Archive u2archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
+ u2archive.unpack();
+ }
- compareFiles(jarFile, jarFile2);
+ final File compareFile = new File(Archive.class.getResource("/pack200/sqlUnpackedNoDebug.jar").toURI());
+ try (JarFile jarFile = new JarFile(file2); JarFile jarFile2 = new JarFile(compareFile)) {
+ assertTrue(file2.length() < 250000);
+ compareFiles(jarFile, jarFile2);
+ }
}
}
[commons-compress] 14/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit c284c522d5a47ce4e7858ad8ff32acb5947cab7a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:54:06 2023 -0400
Use try-with-resources
---
.../MultiReadOnlySeekableByteChannelTest.java | 26 ++++++++++++++--------
1 file changed, 17 insertions(+), 9 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java b/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java
index e6318086..b3faaeaf 100644
--- a/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java
@@ -82,15 +82,17 @@ public class MultiReadOnlySeekableByteChannelTest {
}
@Test
- public void cantPositionToANegativePosition() {
- final SeekableByteChannel s = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(makeEmpty(), makeEmpty());
- assertThrows(IllegalArgumentException.class, () -> s.position(-1));
+ public void cantPositionToANegativePosition() throws IOException {
+ try (SeekableByteChannel s = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(makeEmpty(), makeEmpty())) {
+ assertThrows(IllegalArgumentException.class, () -> s.position(-1));
+ }
}
@Test
- public void cantTruncate() {
- final SeekableByteChannel s = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(makeEmpty(), makeEmpty());
- assertThrows(NonWritableChannelException.class, () -> s.truncate(1));
+ public void cantTruncate() throws IOException {
+ try (SeekableByteChannel s = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(makeEmpty(), makeEmpty())) {
+ assertThrows(NonWritableChannelException.class, () -> s.truncate(1));
+ }
}
@Test
@@ -102,9 +104,13 @@ public class MultiReadOnlySeekableByteChannelTest {
private void check(final byte[] expected) throws IOException {
for (int channelSize = 1; channelSize <= expected.length; channelSize++) {
// Sanity check that all operations work for SeekableInMemoryByteChannel
- check(expected, makeSingle(expected));
+ try (SeekableByteChannel single = makeSingle(expected)) {
+ check(expected, single);
+ }
// Checks against our MultiReadOnlySeekableByteChannel instance
- check(expected, makeMulti(grouped(expected, channelSize)));
+ try (SeekableByteChannel multi = makeMulti(grouped(expected, channelSize))) {
+ check(expected, multi);
+ }
}
}
@@ -345,7 +351,9 @@ public class MultiReadOnlySeekableByteChannelTest {
@Test
public void twoEmptyChannelsConcatenateAsEmptyChannel() throws IOException {
- checkEmpty(MultiReadOnlySeekableByteChannel.forSeekableByteChannels(makeEmpty(), makeEmpty()));
+ try (SeekableByteChannel channel = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(makeEmpty(), makeEmpty())) {
+ checkEmpty(channel);
+ }
}
@Test
[commons-compress] 06/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 331de2967a2d74eee7d78e463ff51449d1b9287b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:20:13 2023 -0400
Use try-with-resources
---
.../archivers/sevenz/AES256SHA256DecoderTest.java | 27 ++++++++++------------
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256DecoderTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256DecoderTest.java
index beb25e7e..783d6385 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256DecoderTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256DecoderTest.java
@@ -28,7 +28,6 @@ import java.io.ObjectInputStream;
import org.junit.jupiter.api.Test;
-
/**
* Unit tests for class {@link AES256SHA256Decoder}.
*
@@ -36,23 +35,21 @@ import org.junit.jupiter.api.Test;
**/
public class AES256SHA256DecoderTest {
-
@Test
- public void testDecodeWithNonEmptyString() {
+ public void testDecodeWithNonEmptyString() throws IOException {
final AES256SHA256Decoder aES256SHA256Decoder = new AES256SHA256Decoder();
- final BufferedInputStream bufferedInputStream = new BufferedInputStream(null, 3138);
- final Coder coder = new Coder();
- final byte[] byteArray = new byte[8];
- byteArray[1] = (byte) (-72);
- coder.properties = byteArray;
- final InputStream inputStream = aES256SHA256Decoder.decode("x", bufferedInputStream, 3138, coder, coder.properties,
- Integer.MAX_VALUE);
-
- final IOException e = assertThrows(IOException.class, () -> new ObjectInputStream(inputStream), "Expecting exception: IOException");
- assertEquals("Salt size + IV size too long in x", e.getMessage());
- assertEquals("org.apache.commons.compress.archivers.sevenz.AES256SHA256Decoder$1", e.getStackTrace()[0].getClassName());
+ try (BufferedInputStream bufferedInputStream = new BufferedInputStream(null, 3138)) {
+ final Coder coder = new Coder();
+ final byte[] byteArray = new byte[8];
+ byteArray[1] = (byte) (-72);
+ coder.properties = byteArray;
+ try (InputStream inputStream = aES256SHA256Decoder.decode("x", bufferedInputStream, 3138, coder, coder.properties, Integer.MAX_VALUE)) {
+ final IOException e = assertThrows(IOException.class, () -> new ObjectInputStream(inputStream), "Expecting exception: IOException");
+ assertEquals("Salt size + IV size too long in x", e.getMessage());
+ assertEquals("org.apache.commons.compress.archivers.sevenz.AES256SHA256Decoder$1", e.getStackTrace()[0].getClassName());
+ }
+ }
}
-
}
[commons-compress] 07/19: Bullet-proof test
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 9cf07fa59dc7c46350e4d14a6cc691268ec7398a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:22:40 2023 -0400
Bullet-proof test
---
.../org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index dac2ed80..da19907b 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -565,6 +565,7 @@ public class SevenZFileTest extends AbstractTestCase {
}
}
+ assertNotNull(testTxtEntry, "testTxtEntry");
final byte[] contents = new byte[(int) testTxtEntry.getSize()];
int numberOfReads = 10;
while (numberOfReads-- > 0) {
[commons-compress] 08/19: Bullet-proof test
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 7e384b59cf324326db6ac71f4579d5f5f205acdb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:23:22 2023 -0400
Bullet-proof test
---
.../org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index da19907b..bc719e96 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -688,6 +688,7 @@ public class SevenZFileTest extends AbstractTestCase {
}
// jump backwards
+ assertNotNull(testTxtEntry, "testTxtEntry");
final byte[] contents = new byte[(int) testTxtEntry.getSize()];
try (InputStream inputStream = sevenZFile.getInputStream(testTxtEntry)) {
off = 0;
@@ -751,6 +752,7 @@ public class SevenZFileTest extends AbstractTestCase {
sevenZFile.read(contents);
// and then read a file after the first entry using random access
+ assertNotNull(testTxtEntry, "testTxtEntry");
contents = new byte[(int) testTxtEntry.getSize()];
int numberOfReads = 10;
while (numberOfReads-- > 0) {
[commons-compress] 15/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit b220635b298c6b1cca5fbe63bd9a1766583eda74
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:55:34 2023 -0400
Use try-with-resources
---
.../utils/ChecksumVerifyingInputStreamTest.java | 31 +++++++++++++---------
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java
index f09fa135..f9bedb39 100644
--- a/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/ChecksumVerifyingInputStreamTest.java
@@ -35,21 +35,24 @@ import org.junit.jupiter.api.Test;
public class ChecksumVerifyingInputStreamTest {
@Test
- public void testReadTakingByteArrayThrowsIOException() {
+ public void testReadTakingByteArrayThrowsIOException() throws IOException {
final Adler32 adler32 = new Adler32();
final byte[] byteArray = new byte[3];
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
- final ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(adler32, byteArrayInputStream, (-1859L), (byte) (-68));
- assertThrows(IOException.class, () -> checksumVerifyingInputStream.read(byteArray));
+ try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(adler32, byteArrayInputStream, (-1859L),
+ (byte) (-68))) {
+ assertThrows(IOException.class, () -> checksumVerifyingInputStream.read(byteArray));
+ }
}
@Test
- public void testReadTakingNoArgumentsThrowsIOException() {
+ public void testReadTakingNoArgumentsThrowsIOException() throws IOException {
final CRC32 cRC32_ = new CRC32();
final byte[] byteArray = new byte[9];
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
- final ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(cRC32_, byteArrayInputStream, (byte)1, (byte)1);
- assertThrows(IOException.class, () -> checksumVerifyingInputStream.read());
+ try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(cRC32_, byteArrayInputStream, (byte) 1, (byte) 1)) {
+ assertThrows(IOException.class, () -> checksumVerifyingInputStream.read());
+ }
}
@Test
@@ -57,13 +60,15 @@ public class ChecksumVerifyingInputStreamTest {
final CRC32 cRC32_ = new CRC32();
final byte[] byteArray = new byte[4];
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
- final ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(cRC32_, byteArrayInputStream, (byte)33, 2303L);
- final int intOne = checksumVerifyingInputStream.read(byteArray);
- final long skipReturnValue = checksumVerifyingInputStream.skip((byte)1);
- assertEquals(558161692L, cRC32_.getValue());
- assertEquals(0, byteArrayInputStream.available());
- assertArrayEquals(new byte[] {(byte)0, (byte)0, (byte)0, (byte)0}, byteArray);
- assertEquals(0L, skipReturnValue);
+ try (ChecksumVerifyingInputStream checksumVerifyingInputStream = new ChecksumVerifyingInputStream(cRC32_, byteArrayInputStream, (byte) 33, 2303L)) {
+ @SuppressWarnings("unused")
+ final int intOne = checksumVerifyingInputStream.read(byteArray);
+ final long skipReturnValue = checksumVerifyingInputStream.skip((byte) 1);
+ assertEquals(558161692L, cRC32_.getValue());
+ assertEquals(0, byteArrayInputStream.available());
+ assertArrayEquals(new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0 }, byteArray);
+ assertEquals(0L, skipReturnValue);
+ }
}
}
\ No newline at end of file
[commons-compress] 04/19: Comment empty block
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit bff667e5b68d8724e878722f6b128476cda303f0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:15:38 2023 -0400
Comment empty block
---
.../commons/compress/compressors/lzma/LZMACompressorOutputStream.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java
index 0556ba8b..12a8238e 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java
@@ -63,6 +63,7 @@ public class LZMACompressorOutputStream extends CompressorOutputStream {
*/
@Override
public void flush() throws IOException {
+ // noop
}
/** {@inheritDoc} */
[commons-compress] 18/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 66f0b3be1a34b3b1220d74b1d4bd380576c50674
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 18:02:17 2023 -0400
Use try-with-resources
---
.../harmony/unpack200/tests/SegmentTest.java | 47 ++++++++++------------
1 file changed, 21 insertions(+), 26 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
index 3ffff9cd..ad50a24f 100644
--- a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
+++ b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
@@ -63,8 +63,7 @@ public class SegmentTest {
@Test
public void testHelloWorld() throws Exception {
- in = Segment.class
- .getResourceAsStream("/pack200/HelloWorld.pack");
+ in = Segment.class.getResourceAsStream("/pack200/HelloWorld.pack");
file = File.createTempFile("hello", "world.jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -72,34 +71,30 @@ public class SegmentTest {
segment.unpack(in, out);
out.close();
out = null;
- final JarFile jarFile = new JarFile(file);
-
- final JarEntry entry = jarFile
- .getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
- assertNotNull(entry);
- final InputStream ours = jarFile.getInputStream(entry);
+ try (JarFile jarFile = new JarFile(file)) {
+ final JarEntry entry = jarFile.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
+ assertNotNull(entry);
+ final InputStream ours = jarFile.getInputStream(entry);
- final JarFile jarFile2 = new JarFile(new File(Segment.class.getResource(
- "/pack200/hw.jar").toURI()));
- final JarEntry entry2 = jarFile2
- .getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
- assertNotNull(entry2);
+ final JarFile jarFile2 = new JarFile(new File(Segment.class.getResource("/pack200/hw.jar").toURI()));
+ final JarEntry entry2 = jarFile2.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
+ assertNotNull(entry2);
- final InputStream expected = jarFile2.getInputStream(entry2);
+ final InputStream expected = jarFile2.getInputStream(entry2);
- final BufferedReader reader1 = new BufferedReader(new InputStreamReader(ours));
- final BufferedReader reader2 = new BufferedReader(new InputStreamReader(expected));
- String line1 = reader1.readLine();
- String line2 = reader2.readLine();
- int i = 1;
- while (line1 != null || line2 != null) {
- assertEquals(line2, line1, "Unpacked class files differ");
- line1 = reader1.readLine();
- line2 = reader2.readLine();
- i++;
+ try (BufferedReader reader1 = new BufferedReader(new InputStreamReader(ours));
+ final BufferedReader reader2 = new BufferedReader(new InputStreamReader(expected))) {
+ String line1 = reader1.readLine();
+ String line2 = reader2.readLine();
+ int i = 1;
+ while (line1 != null || line2 != null) {
+ assertEquals(line2, line1, "Unpacked class files differ ar line " + i);
+ line1 = reader1.readLine();
+ line2 = reader2.readLine();
+ i++;
+ }
+ }
}
- reader1.close();
- reader2.close();
}
@Test
[commons-compress] 13/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 2e4fdff7a1d559e2deed24bb0b6f66e84a451673
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:49:13 2023 -0400
Use try-with-resources
---
.../MultiReadOnlySeekableByteChannelTest.java | 9 ++---
.../ZipSplitReadOnlySeekableByteChannelTest.java | 41 +++++++++++++---------
2 files changed, 29 insertions(+), 21 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java b/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java
index 1549b76d..e6318086 100644
--- a/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/MultiReadOnlySeekableByteChannelTest.java
@@ -224,10 +224,11 @@ public class MultiReadOnlySeekableByteChannelTest {
}
@Test
- public void forSeekableByteChannelsReturnsIdentityForSingleElement() {
- final SeekableByteChannel e = makeEmpty();
- final SeekableByteChannel m = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(e);
- assertSame(e, m);
+ public void forSeekableByteChannelsReturnsIdentityForSingleElement() throws IOException {
+ try (SeekableByteChannel e = makeEmpty();
+ SeekableByteChannel m = MultiReadOnlySeekableByteChannel.forSeekableByteChannels(e)) {
+ assertSame(e, m);
+ }
}
@Test
diff --git a/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java b/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java
index b032ed7f..9b090565 100644
--- a/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java
@@ -82,11 +82,13 @@ public class ZipSplitReadOnlySeekableByteChannelTest {
list.add(firstFile);
list.add(secondFile);
- SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forFiles(lastFile, list);
- assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forFiles(lastFile, list)) {
+ assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ }
- channel = ZipSplitReadOnlySeekableByteChannel.forFiles(firstFile, secondFile, lastFile);
- assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forFiles(firstFile, secondFile, lastFile)) {
+ assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ }
}
@Test
@@ -123,9 +125,10 @@ public class ZipSplitReadOnlySeekableByteChannelTest {
@Test
public void forOrderedSeekableByteChannelsReturnsIdentityForSingleElement() throws IOException {
- final SeekableByteChannel emptyChannel = new SeekableInMemoryByteChannel(ByteUtils.EMPTY_BYTE_ARRAY);
- final SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(emptyChannel);
- assertSame(emptyChannel, channel);
+ try (SeekableByteChannel emptyChannel = new SeekableInMemoryByteChannel(ByteUtils.EMPTY_BYTE_ARRAY);
+ final SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(emptyChannel)) {
+ assertSame(emptyChannel, channel);
+ }
}
@Test
@@ -148,11 +151,13 @@ public class ZipSplitReadOnlySeekableByteChannelTest {
list.add(firstFile);
list.add(secondFile);
- SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(lastFile, list);
- assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(lastFile, list)) {
+ assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ }
- channel = ZipSplitReadOnlySeekableByteChannel.forPaths(firstFile, secondFile, lastFile);
- assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(firstFile, secondFile, lastFile)) {
+ assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ }
}
@Test
@@ -189,13 +194,15 @@ public class ZipSplitReadOnlySeekableByteChannelTest {
final int randomDiskNumber = random.nextInt(3);
final int randomOffset = randomDiskNumber < 2 ? random.nextInt(firstFileSize) : random.nextInt(lastFileSize);
- final ZipSplitReadOnlySeekableByteChannel channel = (ZipSplitReadOnlySeekableByteChannel) ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
- channel.position(randomDiskNumber, randomOffset);
- long expectedPosition = randomOffset;
+ try (ZipSplitReadOnlySeekableByteChannel channel = (ZipSplitReadOnlySeekableByteChannel) ZipSplitReadOnlySeekableByteChannel
+ .buildFromLastSplitSegment(lastFile)) {
+ channel.position(randomDiskNumber, randomOffset);
+ long expectedPosition = randomOffset;
- expectedPosition += randomDiskNumber > 0 ? firstFileSize : 0;
- expectedPosition += randomDiskNumber > 1 ? secondFileSize : 0;
+ expectedPosition += randomDiskNumber > 0 ? firstFileSize : 0;
+ expectedPosition += randomDiskNumber > 1 ? secondFileSize : 0;
- assertEquals(expectedPosition, channel.position());
+ assertEquals(expectedPosition, channel.position());
+ }
}
}
[commons-compress] 02/19: Javadoc
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 2ca8591059f8808f334aef78d3084307683e4431
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:08:15 2023 -0400
Javadoc
---
.../compress/archivers/dump/DumpArchiveUtil.java | 29 +++++++++++-----------
1 file changed, 15 insertions(+), 14 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java
index 80cd9358..7f4be5f2 100644
--- a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java
+++ b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java
@@ -28,6 +28,7 @@ import org.apache.commons.compress.utils.ByteUtils;
* Various utilities for dump archives.
*/
class DumpArchiveUtil {
+
/**
* Calculate checksum for buffer.
*
@@ -46,33 +47,33 @@ class DumpArchiveUtil {
}
/**
- * Read 2-byte integer from buffer.
+ * Reads 2-byte integer from buffer.
*
- * @param buffer
- * @param offset
- * @return the 2-byte entry as an int
+ * @param buffer The source buffer.
+ * @param offset Where to start reading.
+ * @return the 2-byte entry as an int.
*/
public static final int convert16(final byte[] buffer, final int offset) {
return (int) ByteUtils.fromLittleEndian(buffer, offset, 2);
}
/**
- * Read 4-byte integer from buffer.
+ * Reads 4-byte integer from buffer.
*
- * @param buffer
- * @param offset
- * @return the 4-byte entry as an int
+ * @param buffer The source buffer.
+ * @param offset Where to start reading.
+ * @return the 4-byte entry as an int.
*/
public static final int convert32(final byte[] buffer, final int offset) {
return (int) ByteUtils.fromLittleEndian(buffer, offset, 4);
}
/**
- * Read 8-byte integer from buffer.
+ * Reads 8-byte integer from buffer.
*
- * @param buffer
- * @param offset
- * @return the 8-byte entry as a long
+ * @param buffer The source buffer.
+ * @param offset Where to start reading.
+ * @return the 8-byte entry as a long.
*/
public static final long convert64(final byte[] buffer, final int offset) {
return ByteUtils.fromLittleEndian(buffer, offset, 8);
@@ -89,7 +90,7 @@ class DumpArchiveUtil {
/**
* Gets the ino associated with this buffer.
*
- * @param buffer
+ * @param buffer The source buffer.
* @return the ino associated with this buffer.
*/
public static final int getIno(final byte[] buffer) {
@@ -99,7 +100,7 @@ class DumpArchiveUtil {
/**
* Verifies that the buffer contains a tape segment header.
*
- * @param buffer
+ * @param buffer The source buffer.
* @return Whether the buffer contains a tape segment header.
*/
public static final boolean verify(final byte[] buffer) {
[commons-compress] 09/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit fa4f1c481b779bd632a3104e84e09d787359df04
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:40:01 2023 -0400
Use try-with-resources
---
.../commons/compress/archivers/sevenz/SevenZOutputFileTest.java | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
index fac919f4..95a558b3 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
@@ -611,7 +611,9 @@ public class SevenZOutputFileTest extends AbstractTestCase {
methods.add(new SevenZMethodConfiguration(SevenZMethod.COPY));
methods.add(new SevenZMethodConfiguration(SevenZMethod.DEFLATE));
methods.add(new SevenZMethodConfiguration(SevenZMethod.BZIP2));
- createAndReadBack(new SeekableInMemoryByteChannel(), methods);
+ try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel()) {
+ createAndReadBack(channel, methods);
+ }
}
@Test
[commons-compress] 16/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 58390f0dd8ff085cdfdcbd3c234988c8af0c1c7d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 17:55:51 2023 -0400
Use try-with-resources
---
.../BoundedSeekableByteChannelInputStreamTest.java | 4 +-
.../utils/ChecksumCalculatingInputStreamTest.java | 45 ++++++++++++----------
2 files changed, 26 insertions(+), 23 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/BoundedSeekableByteChannelInputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/BoundedSeekableByteChannelInputStreamTest.java
index f7e9a6f0..229eed53 100644
--- a/src/test/java/org/apache/commons/compress/utils/BoundedSeekableByteChannelInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/BoundedSeekableByteChannelInputStreamTest.java
@@ -29,8 +29,8 @@ public class BoundedSeekableByteChannelInputStreamTest {
@Test
public void checkRestrictedRead() throws IOException {
byte[] readContent;
- try (BoundedSeekableByteChannelInputStream input = new BoundedSeekableByteChannelInputStream(0, 5,
- new SeekableInMemoryByteChannel("Hello World!".getBytes(UTF_8)))) {
+ try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel("Hello World!".getBytes(UTF_8));
+ BoundedSeekableByteChannelInputStream input = new BoundedSeekableByteChannelInputStream(0, 5, channel)) {
readContent = IOUtils.toByteArray(input);
}
assertEquals(5, readContent.length);
diff --git a/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java
index 25423c79..c67621d2 100644
--- a/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/ChecksumCalculatingInputStreamTest.java
@@ -50,17 +50,17 @@ public class ChecksumCalculatingInputStreamTest {
assertThrows(NullPointerException.class, () -> new ChecksumCalculatingInputStream(null, new ByteArrayInputStream(new byte[1])));
}
-
@Test
public void testReadTakingByteArray() throws IOException {
final Adler32 adler32 = new Adler32();
final byte[] byteArray = new byte[6];
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
- final ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream);
- final int readResult = checksumCalculatingInputStream.read(byteArray);
- assertEquals(6, readResult);
- assertEquals(0, byteArrayInputStream.available());
- assertEquals(393217L, checksumCalculatingInputStream.getValue());
+ try (ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream)) {
+ final int readResult = checksumCalculatingInputStream.read(byteArray);
+ assertEquals(6, readResult);
+ assertEquals(0, byteArrayInputStream.available());
+ assertEquals(393217L, checksumCalculatingInputStream.getValue());
+ }
}
@Test
@@ -69,13 +69,14 @@ public class ChecksumCalculatingInputStreamTest {
final byte[] byteArray = new byte[6];
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
final ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream);
- final BufferedInputStream bufferedInputStream = new BufferedInputStream(checksumCalculatingInputStream);
- final int inputStreamReadResult = bufferedInputStream.read(byteArray, 0, 1);
- final int checkSumCalculationReadResult = checksumCalculatingInputStream.read();
- assertNotEquals(checkSumCalculationReadResult, inputStreamReadResult);
- assertEquals((-1), checkSumCalculationReadResult);
- assertEquals(0, byteArrayInputStream.available());
- assertEquals(393217L, checksumCalculatingInputStream.getValue());
+ try (BufferedInputStream bufferedInputStream = new BufferedInputStream(checksumCalculatingInputStream)) {
+ final int inputStreamReadResult = bufferedInputStream.read(byteArray, 0, 1);
+ final int checkSumCalculationReadResult = checksumCalculatingInputStream.read();
+ assertNotEquals(checkSumCalculationReadResult, inputStreamReadResult);
+ assertEquals((-1), checkSumCalculationReadResult);
+ assertEquals(0, byteArrayInputStream.available());
+ assertEquals(393217L, checksumCalculatingInputStream.getValue());
+ }
}
@Test
@@ -83,20 +84,22 @@ public class ChecksumCalculatingInputStreamTest {
final Adler32 adler32 = new Adler32();
final byte[] byteArray = new byte[6];
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
- final ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream);
- final long skipResult = checksumCalculatingInputStream.skip((byte)0);
- assertEquals(1L, skipResult);
- assertEquals(65537L, checksumCalculatingInputStream.getValue());
+ try (ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream)) {
+ final long skipResult = checksumCalculatingInputStream.skip((byte) 0);
+ assertEquals(1L, skipResult);
+ assertEquals(65537L, checksumCalculatingInputStream.getValue());
+ }
}
@Test
public void testSkipReturningZero() throws IOException {
final Adler32 adler32 = new Adler32();
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY);
- final ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream);
- final long skipResult = checksumCalculatingInputStream.skip(60L);
- assertEquals(0L, skipResult);
- assertEquals(1L, checksumCalculatingInputStream.getValue());
+ try (ChecksumCalculatingInputStream checksumCalculatingInputStream = new ChecksumCalculatingInputStream(adler32, byteArrayInputStream)) {
+ final long skipResult = checksumCalculatingInputStream.skip(60L);
+ assertEquals(0L, skipResult);
+ assertEquals(1L, checksumCalculatingInputStream.getValue());
+ }
}
}
\ No newline at end of file
[commons-compress] 17/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 35584738b78fe7437d919e8efd7bd78e2faabbac
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 17:59:08 2023 -0400
Use try-with-resources
---
.../harmony/unpack200/tests/ArchiveTest.java | 49 +++++++++++-----------
1 file changed, 24 insertions(+), 25 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
index 8f1013bf..4808eb00 100644
--- a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
+++ b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
@@ -83,28 +83,27 @@ public class ArchiveTest {
@Test
public void testDeflateHint() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("sql", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
Archive archive = new Archive(in, out);
archive.setDeflateHint(true);
archive.unpack();
- JarFile jarFile = new JarFile(file);
- assertEquals(ZipEntry.DEFLATED, jarFile.getEntry("bin/test/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.class").getMethod());
-
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
- file = File.createTempFile("sql", ".jar");
- file.deleteOnExit();
- out = new JarOutputStream(new FileOutputStream(file));
- archive = new Archive(in, out);
- archive.setDeflateHint(false);
- archive.unpack();
- jarFile = new JarFile(file);
- assertEquals(ZipEntry.STORED, jarFile.getEntry("bin/test/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.class").getMethod());
-
+ try (JarFile jarFile = new JarFile(file)) {
+ assertEquals(ZipEntry.DEFLATED, jarFile.getEntry("bin/test/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.class").getMethod());
+
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
+ file = File.createTempFile("sql", ".jar");
+ file.deleteOnExit();
+ out = new JarOutputStream(new FileOutputStream(file));
+ archive = new Archive(in, out);
+ archive.setDeflateHint(false);
+ archive.unpack();
+ }
+ try (JarFile jarFile = new JarFile(file)) {
+ assertEquals(ZipEntry.STORED, jarFile.getEntry("bin/test/org/apache/harmony/sql/tests/internal/rowset/CachedRowSetImplTest.class").getMethod());
+ }
}
@Test
@@ -133,9 +132,9 @@ public class ArchiveTest {
archive.unpack();
// log file should be empty
- FileReader reader = new FileReader(logFile);
- assertFalse(reader.ready());
- reader.close();
+ try (FileReader reader = new FileReader(logFile)) {
+ assertFalse(reader.ready());
+ }
// test verbose
in = Archive.class
@@ -151,9 +150,9 @@ public class ArchiveTest {
archive.unpack();
// log file should not be empty
- reader = new FileReader(logFile);
- assertTrue(reader.ready());
- reader.close();
+ try (FileReader reader = new FileReader(logFile)) {
+ assertTrue(reader.ready());
+ }
// test append option
final long length = logFile.length();
@@ -192,9 +191,9 @@ public class ArchiveTest {
archive.unpack();
// log file should be empty
- reader = new FileReader(logFile);
- assertFalse(reader.ready());
- reader.close();
+ try (FileReader reader = new FileReader(logFile)) {
+ assertFalse(reader.ready());
+ }
}
@Test
[commons-compress] 10/19: Use try-with-resources
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit a54b1f3f34a5b1227d01110d14777daa65937a35
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:41:16 2023 -0400
Use try-with-resources
---
.../utils/FixedLengthBlockOutputStreamTest.java | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
index 77cd68b9..d0c188b6 100644
--- a/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
@@ -298,18 +298,18 @@ public class FixedLengthBlockOutputStreamTest {
@Test
- public void testWriteFailsAfterDestClosedThrowsException() {
+ public void testWriteFailsAfterDestClosedThrowsException() throws IOException {
final int blockSize = 2;
- final MockOutputStream mock = new MockOutputStream(blockSize, false);
- final FixedLengthBlockOutputStream out =
- new FixedLengthBlockOutputStream(mock, blockSize);
- assertThrows(IOException.class, () -> {
- out.write(1);
- assertTrue(out.isOpen());
- mock.close();
- out.write(1);
- }, "expected IO Exception");
- assertFalse(out.isOpen());
+ try (MockOutputStream mock = new MockOutputStream(blockSize, false);
+ FixedLengthBlockOutputStream out = new FixedLengthBlockOutputStream(mock, blockSize)) {
+ assertThrows(IOException.class, () -> {
+ out.write(1);
+ assertTrue(out.isOpen());
+ mock.close();
+ out.write(1);
+ }, "expected IO Exception");
+ assertFalse(out.isOpen());
+ }
}
@Test
[commons-compress] 05/19: Comment
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 89391cb0a472df7b74a1ea787ac6a17d6a4a166b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:16:37 2023 -0400
Comment
---
.../apache/commons/compress/harmony/archive/internal/nls/Messages.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/compress/harmony/archive/internal/nls/Messages.java b/src/main/java/org/apache/commons/compress/harmony/archive/internal/nls/Messages.java
index 6c0e7b11..454afc9b 100644
--- a/src/main/java/org/apache/commons/compress/harmony/archive/internal/nls/Messages.java
+++ b/src/main/java/org/apache/commons/compress/harmony/archive/internal/nls/Messages.java
@@ -205,7 +205,7 @@ public class Messages {
static public ResourceBundle setLocale(final Locale locale, final String resource) {
try {
// VM.bootCallerClassLoader() returns null
- final ClassLoader loader = null;// VM.bootCallerClassLoader();
+ final ClassLoader loader = null; // VM.bootCallerClassLoader();
return (ResourceBundle) AccessController.doPrivileged((PrivilegedAction<Object>) () -> ResourceBundle
.getBundle(resource, locale, loader != null ? loader : ClassLoader.getSystemClassLoader()));
} catch (final MissingResourceException e) {
[commons-compress] 12/19: Comment empty block
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 4aa4b1ab29d7585c84b65b7a1337653ec2d420b4
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 12:42:52 2023 -0400
Comment empty block
---
.../commons/compress/utils/FixedLengthBlockOutputStreamTest.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java b/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
index 4c40bb62..598a0b1e 100644
--- a/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStreamTest.java
@@ -232,7 +232,8 @@ public class FixedLengthBlockOutputStreamTest {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
Files.deleteIfExists(tempFile);
- } catch (final IOException e) {
+ } catch (final IOException ignore) {
+ // ignore
}
}));
final int blockSize = 512;
[commons-compress] 01/19: Add testPaxTimeFieldsForInvalidValues
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 43354810a9aa4305bf82318f1884e83a078a621a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 3 08:06:33 2023 -0400
Add testPaxTimeFieldsForInvalidValues
---
.../compress/archivers/tar/TarArchiveEntry.java | 20 ++++++++++++++++--
.../archivers/tar/TarArchiveEntryTest.java | 24 ++++++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
index 1598b87d..60fd03c9 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
@@ -217,6 +217,17 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
@Deprecated
public static final int MILLIS_PER_SECOND = 1000;
+ /**
+ * Regular expression pattern for validating values in pax extended header file time fields.
+ * These fields contain two numeric values (seconds and sub-second values) as per this definition:
+ * https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_05
+ * <p>
+ * Since they are parsed into long values, maximum length of each is the same as Long.MAX_VALUE
+ * which is 19 digits.
+ * </p>
+ */
+ private static final String PAX_EXTENDED_HEADER_FILE_TIMES_PATTERN = "-?\\d{1,19}(?:\\.\\d{1,19})?";
+
private static FileTime fileTimeFromOptionalSeconds(final long seconds) {
if (seconds <= 0) {
return null;
@@ -265,7 +276,12 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
return fileName;
}
- private static Instant parseInstantFromDecimalSeconds(final String value) {
+ private static Instant parseInstantFromDecimalSeconds(final String value) throws IOException {
+ // Validate field values to prevent denial of service attacks with BigDecimal values (see JDK-6560193)
+ if (!value.matches(TarArchiveEntry.PAX_EXTENDED_HEADER_FILE_TIMES_PATTERN)) {
+ throw new IOException("Corrupted PAX header. Time field value is invalid '" + value + "'");
+ }
+
final BigDecimal epochSeconds = new BigDecimal(value);
final long seconds = epochSeconds.longValue();
final long nanos = epochSeconds.remainder(BigDecimal.ONE).movePointRight(9).longValue();
@@ -289,12 +305,12 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
/** The entry's size. */
private long size;
-
/**
* The entry's modification time.
* Corresponds to the POSIX {@code mtime} attribute.
*/
private FileTime mTime;
+
/**
* The entry's status change time.
* Corresponds to the POSIX {@code ctime} attribute.
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
index 48f6d5ce..db3897d4 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
@@ -39,6 +39,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
+import java.util.Random;
import org.apache.commons.compress.AbstractTestCase;
import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
@@ -406,6 +407,29 @@ public class TarArchiveEntryTest implements TarConstants {
t.setSize(0100000000000L);
}
+ @Test
+ public void testPaxTimeFieldsForInvalidValues() {
+ final String[] headerNames = { "LIBARCHIVE.creationtime", "atime", "mtime", "ctime" };
+ // @formatter:off
+ final String[] testValues = {
+ // Generate a number with a very large integer or fractional component
+ new Random().nextLong() + "." + String.join("",
+ Collections.nCopies(15000, String.valueOf(Long.MAX_VALUE))),
+ // These two examples use the exponent notation
+ "9e9999999",
+ "9E9999999"
+ };
+ // @formatter:on
+
+ final TarArchiveEntry entry = new TarArchiveEntry("test.txt");
+ for (String name : headerNames) {
+ for (String value : testValues) {
+ Exception exp = assertThrows(IllegalArgumentException.class, () -> entry.addPaxHeader(name, value));
+ assert(exp.getCause().getMessage().startsWith("Corrupted PAX header. Time field value is invalid"));
+ }
+ }
+ }
+
@Test
public void testTarFileWithFSRoot() throws IOException {
final File f = File.createTempFile("taetest", ".tar");