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
The following commit(s) were added to refs/heads/master by this push:
new 7570beb3 Use try-with-resources
7570beb3 is described below
commit 7570beb39134ccf7989789a5c16785fdb363ff12
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Nov 3 15:16:49 2023 -0400
Use try-with-resources
---
.../compress/archivers/LongSymLinkTest.java | 86 ++++++-------
.../commons/compress/archivers/ZipTestCase.java | 135 ++++++++-------------
2 files changed, 89 insertions(+), 132 deletions(-)
diff --git
a/src/test/java/org/apache/commons/compress/archivers/LongSymLinkTest.java
b/src/test/java/org/apache/commons/compress/archivers/LongSymLinkTest.java
index 97b0816d..992e448d 100644
--- a/src/test/java/org/apache/commons/compress/archivers/LongSymLinkTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/LongSymLinkTest.java
@@ -17,7 +17,6 @@
package org.apache.commons.compress.archivers;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -44,9 +43,8 @@ import org.junit.jupiter.params.provider.MethodSource;
/**
* Test that can read various tar file examples.
*
- * Files must be in resources/longsymlink, and there must be a file.txt
containing
- * the list of files in the archives.
-*/
+ * Files must be in resources/longsymlink, and there must be a file.txt
containing the list of files in the archives.
+ */
public class LongSymLinkTest extends AbstractTestCase {
private static final ClassLoader CLASSLOADER =
LongSymLinkTest.class.getClassLoader();
@@ -72,19 +70,18 @@ public class LongSymLinkTest extends AbstractTestCase {
@BeforeAll
public static void setUpFileList() throws Exception {
assertTrue(ARCDIR.exists());
- final File listing= new File(ARCDIR,"files.txt");
+ final File listing = new File(ARCDIR, "files.txt");
assertTrue(listing.canRead(), "files.txt is readable");
- final BufferedReader br = new
BufferedReader(Files.newBufferedReader(listing.toPath()));
- String line;
- while ((line=br.readLine())!=null){
- if (!line.startsWith("#")){
- FILELIST.add(line);
+ try (BufferedReader br = new
BufferedReader(Files.newBufferedReader(listing.toPath()))) {
+ String line;
+ while ((line = br.readLine()) != null) {
+ if (!line.startsWith("#")) {
+ FILELIST.add(line);
+ }
}
}
- br.close();
}
-
@Override
protected String getExpectedString(final ArchiveEntry entry) {
if (entry instanceof TarArchiveEntry) {
@@ -100,49 +97,44 @@ public class LongSymLinkTest extends AbstractTestCase {
@MethodSource("data")
public void testArchive(final File file) throws Exception {
@SuppressWarnings("unchecked") // fileList is of correct type
- final
- ArrayList<String> expected = (ArrayList<String>) FILELIST.clone();
+ final ArrayList<String> expected = (ArrayList<String>)
FILELIST.clone();
final String name = file.getName();
- if ("minotaur.jar".equals(name) || "minotaur-0.jar".equals(name)){
+ if ("minotaur.jar".equals(name) || "minotaur-0.jar".equals(name)) {
expected.add("META-INF/");
expected.add("META-INF/MANIFEST.MF");
}
- final ArchiveInputStream<?> ais = factory.createArchiveInputStream(new
BufferedInputStream(Files.newInputStream(file.toPath())));
- // check if expected type recognized
- if (name.endsWith(".tar")){
- assertTrue(ais instanceof TarArchiveInputStream);
- } else if (name.endsWith(".jar") || name.endsWith(".zip")){
- assertTrue(ais instanceof ZipArchiveInputStream);
- } else if (name.endsWith(".cpio")){
- assertTrue(ais instanceof CpioArchiveInputStream);
- // Hack: cpio does not add trailing "/" to directory names
- for(int i=0; i < expected.size(); i++){
- final String ent = expected.get(i);
- if (ent.endsWith("/")){
- expected.set(i, ent.substring(0, ent.length()-1));
+ try (ArchiveInputStream<?> ais = factory.createArchiveInputStream(new
BufferedInputStream(Files.newInputStream(file.toPath())))) {
+ // check if expected type recognized
+ if (name.endsWith(".tar")) {
+ assertTrue(ais instanceof TarArchiveInputStream);
+ } else if (name.endsWith(".jar") || name.endsWith(".zip")) {
+ assertTrue(ais instanceof ZipArchiveInputStream);
+ } else if (name.endsWith(".cpio")) {
+ assertTrue(ais instanceof CpioArchiveInputStream);
+ // Hack: cpio does not add trailing "/" to directory names
+ for (int i = 0; i < expected.size(); i++) {
+ final String ent = expected.get(i);
+ if (ent.endsWith("/")) {
+ expected.set(i, ent.substring(0, ent.length() - 1));
+ }
}
- }
- } else if (name.endsWith(".ar")){
- assertTrue(ais instanceof ArArchiveInputStream);
- // CPIO does not store directories or directory names
- expected.clear();
- for (final String ent : FILELIST) {
- if (!ent.endsWith("/")) {// not a directory
- final int lastSlash = ent.lastIndexOf('/');
- if (lastSlash >= 0) { // extract path name
- expected.add(ent.substring(lastSlash + 1));
- } else {
- expected.add(ent);
+ } else if (name.endsWith(".ar")) {
+ assertTrue(ais instanceof ArArchiveInputStream);
+ // CPIO does not store directories or directory names
+ expected.clear();
+ for (final String ent : FILELIST) {
+ if (!ent.endsWith("/")) {// not a directory
+ final int lastSlash = ent.lastIndexOf('/');
+ if (lastSlash >= 0) { // extract path name
+ expected.add(ent.substring(lastSlash + 1));
+ } else {
+ expected.add(ent);
+ }
}
}
+ } else {
+ fail("Unexpected file type: " + name);
}
- } else {
- fail("Unexpected file type: "+name);
- }
- try {
- assertDoesNotThrow(() -> checkArchiveContent(ais, expected),
"Error processing " + file.getName());
- } finally {
- ais.close();
}
}
}
diff --git
a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
index d69d2d9c..5b0262d2 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
@@ -88,7 +88,8 @@ public final class ZipTestCase extends AbstractTestCase {
private void assertSameFileContents(final File expectedFile, final File
actualFile) throws IOException {
final int size = (int) Math.max(expectedFile.length(),
actualFile.length());
- try (final ZipFile expected = new ZipFile(expectedFile); final ZipFile
actual = new ZipFile(actualFile)) {
+ try (final ZipFile expected = new ZipFile(expectedFile);
+ final ZipFile actual = new ZipFile(actualFile)) {
final byte[] expectedBuf = new byte[size];
final byte[] actualBuf = new byte[size];
@@ -110,7 +111,7 @@ public final class ZipTestCase extends AbstractTestCase {
assertEquals(expectedElement.getInternalAttributes(),
actualElement.getInternalAttributes());
try (final InputStream actualIs =
actual.getInputStream(actualElement);
- final InputStream expectedIs =
expected.getInputStream(expectedElement)) {
+ final InputStream expectedIs =
expected.getInputStream(expectedElement)) {
IOUtils.readFully(expectedIs, expectedBuf);
IOUtils.readFully(actualIs, actualBuf);
}
@@ -133,8 +134,7 @@ public final class ZipTestCase extends AbstractTestCase {
return result;
}
- private void createArchiveEntry(final String payload, final
ZipArchiveOutputStream zos, final String name)
- throws IOException {
+ private void createArchiveEntry(final String payload, final
ZipArchiveOutputStream zos, final String name) throws IOException {
final ZipArchiveEntry in = new ZipArchiveEntry(name);
zos.putArchiveEntry(in);
@@ -147,8 +147,7 @@ public final class ZipTestCase extends AbstractTestCase {
return zos;
}
- private File createReferenceFile(final Zip64Mode zipMode, final String
prefix)
- throws IOException {
+ private File createReferenceFile(final Zip64Mode zipMode, final String
prefix) throws IOException {
final File reference = createTempFile(prefix, ".zip");
try (final ZipArchiveOutputStream zos = new
ZipArchiveOutputStream(reference)) {
zos.setUseZip64(zipMode);
@@ -167,8 +166,7 @@ public final class ZipTestCase extends AbstractTestCase {
final File directoryToZip = getFilesToZip();
final File outputZipFile = new File(getTempDirFile(), "splitZip.zip");
final long splitSize = 100 * 1024L; /* 100 KB */
- try (final ZipArchiveOutputStream zipArchiveOutputStream = new
ZipArchiveOutputStream(outputZipFile,
- splitSize)) {
+ try (final ZipArchiveOutputStream zipArchiveOutputStream = new
ZipArchiveOutputStream(outputZipFile, splitSize)) {
addFilesToZip(zipArchiveOutputStream, directoryToZip);
}
}
@@ -195,7 +193,7 @@ public final class ZipTestCase extends AbstractTestCase {
outputFile = new File(getTempDirFile(), zipEntry.getName());
try (InputStream inputStream =
zipFile.getInputStream(zipEntry);
- OutputStream outputStream =
Files.newOutputStream(outputFile.toPath())) {
+ OutputStream outputStream =
Files.newOutputStream(outputFile.toPath())) {
buffer = new byte[(int) zipEntry.getSize()];
while ((readLen = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, readLen);
@@ -206,7 +204,7 @@ public final class ZipTestCase extends AbstractTestCase {
return getTempDirFile().listFiles()[0];
}
- private void readStream(final InputStream in, final ArchiveEntry entry,
final Map<String,List<List<Long>>> map) throws IOException {
+ private void readStream(final InputStream in, final ArchiveEntry entry,
final Map<String, List<List<Long>>> map) throws IOException {
final byte[] buf = new byte[4096];
final InputStreamStatistics stats = (InputStreamStatistics) in;
while (in.read(buf) != -1) {
@@ -228,9 +226,8 @@ public final class ZipTestCase extends AbstractTestCase {
final File lastFile = new File(getTempDirFile(), "splitZip.zip");
try (SeekableByteChannel channel =
ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
- InputStream inputStream = Channels.newInputStream(channel);
- ZipArchiveInputStream splitInputStream = new
ZipArchiveInputStream(inputStream,
- UTF_8.toString(), true, false, true)) {
+ InputStream inputStream = Channels.newInputStream(channel);
+ ZipArchiveInputStream splitInputStream = new
ZipArchiveInputStream(inputStream, UTF_8.toString(), true, false, true)) {
ArchiveEntry entry;
final int filesNum = countNonDirectories(directoryToZip);
@@ -242,8 +239,7 @@ public final class ZipTestCase extends AbstractTestCase {
// compare all files one by one
final File fileToCompare = new File(entry.getName());
try (InputStream inputStreamToCompare =
Files.newInputStream(fileToCompare.toPath())) {
- assertArrayEquals(IOUtils.toByteArray(splitInputStream),
- IOUtils.toByteArray(inputStreamToCompare));
+ assertArrayEquals(IOUtils.toByteArray(splitInputStream),
IOUtils.toByteArray(inputStreamToCompare));
}
filesCount++;
}
@@ -257,8 +253,7 @@ public final class ZipTestCase extends AbstractTestCase {
final File directoryToZip = getFilesToZip();
final File outputZipFile = new File(getTempDirFile(), "splitZip.zip");
final long splitSize = 100 * 1024L; /* 100 KB */
- try (final ZipArchiveOutputStream zipArchiveOutputStream = new
ZipArchiveOutputStream(outputZipFile,
- splitSize)) {
+ try (final ZipArchiveOutputStream zipArchiveOutputStream = new
ZipArchiveOutputStream(outputZipFile, splitSize)) {
// create a file that has the same name of one of the created
split segments
final File sameNameFile = new File(getTempDirFile(),
"splitZip.z01");
@@ -268,9 +263,9 @@ public final class ZipTestCase extends AbstractTestCase {
} catch (final Exception e) {
// Ignore:
// java.io.IOException: This archive contains unclosed entries.
- // at
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.finish(ZipArchiveOutputStream.java:563)
- // at
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.close(ZipArchiveOutputStream.java:1119)
- // at
org.apache.commons.compress.archivers.ZipTestCase.buildSplitZipWithSegmentAlreadyExistThrowsException(ZipTestCase.java:715)
+ // at
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.finish(ZipArchiveOutputStream.java:563)
+ // at
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.close(ZipArchiveOutputStream.java:1119)
+ // at
org.apache.commons.compress.archivers.ZipTestCase.buildSplitZipWithSegmentAlreadyExistThrowsException(ZipTestCase.java:715)
}
}
@@ -410,7 +405,6 @@ public final class ZipTestCase extends AbstractTestCase {
final File tmp = createTempFile();
ZipArchiveOutputStream zos = null;
ZipFile zf = null;
- InputStream fis = null;
try {
final File archive = createTempFile("test.", ".zip");
zos = new ZipArchiveOutputStream(archive);
@@ -419,12 +413,11 @@ public final class ZipTestCase extends AbstractTestCase {
in.setSize(tmp.length());
zos.putArchiveEntry(in);
final byte[] b = new byte[(int) tmp.length()];
- fis = Files.newInputStream(tmp.toPath());
- while (fis.read(b) > 0) {
- zos.write(b);
+ try (InputStream fis = Files.newInputStream(tmp.toPath())) {
+ while (fis.read(b) > 0) {
+ zos.write(b);
+ }
}
- fis.close();
- fis = null;
zos.closeArchiveEntry();
zos.close();
zos = null;
@@ -440,9 +433,6 @@ public final class ZipTestCase extends AbstractTestCase {
if (zos != null) {
zos.close();
}
- if (fis != null) {
- fis.close();
- }
}
}
@@ -450,7 +440,6 @@ public final class ZipTestCase extends AbstractTestCase {
public void testFileEntryFromFile() throws Exception {
ZipArchiveOutputStream zos = null;
ZipFile zf = null;
- InputStream fis = null;
final File tmpFile = createTempFile();
try {
final File archive = createTempFile("test.", ".zip");
@@ -458,12 +447,11 @@ public final class ZipTestCase extends AbstractTestCase {
final ZipArchiveEntry in = new ZipArchiveEntry(tmpFile, "foo");
zos.putArchiveEntry(in);
final byte[] b = new byte[(int) tmpFile.length()];
- fis = Files.newInputStream(tmpFile.toPath());
- while (fis.read(b) > 0) {
- zos.write(b);
+ try (InputStream fis = Files.newInputStream(tmpFile.toPath())) {
+ while (fis.read(b) > 0) {
+ zos.write(b);
+ }
}
- fis.close();
- fis = null;
zos.closeArchiveEntry();
zos.close();
zos = null;
@@ -479,22 +467,18 @@ public final class ZipTestCase extends AbstractTestCase {
if (zos != null) {
zos.close();
}
- if (fis != null) {
- fis.close();
- }
}
}
- private void testInputStreamStatistics(final String fileName, final
Map<String, List<Long>> expectedStatistics)
- throws IOException, ArchiveException {
+ private void testInputStreamStatistics(final String fileName, final
Map<String, List<Long>> expectedStatistics) throws IOException,
ArchiveException {
final File input = getFile(fileName);
- final Map<String,List<List<Long>>> actualStatistics = new HashMap<>();
+ final Map<String, List<List<Long>>> actualStatistics = new HashMap<>();
// stream access
try (final InputStream fis = Files.newInputStream(input.toPath());
- final ArchiveInputStream<?> in =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", fis)) {
- for (ArchiveEntry entry; (entry = in.getNextEntry()) != null; ) {
+ final ArchiveInputStream<?> in =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", fis)) {
+ for (ArchiveEntry entry; (entry = in.getNextEntry()) != null;) {
readStream(in, entry, actualStatistics);
}
}
@@ -511,14 +495,12 @@ public final class ZipTestCase extends AbstractTestCase {
}
// compare statistics of stream / file access
- for (final Map.Entry<String,List<List<Long>>> me :
actualStatistics.entrySet()) {
- assertEquals(me.getValue().get(0), me.getValue().get(1),
- "Mismatch of stats for: " + me.getKey());
+ for (final Map.Entry<String, List<List<Long>>> me :
actualStatistics.entrySet()) {
+ assertEquals(me.getValue().get(0), me.getValue().get(1), "Mismatch
of stats for: " + me.getKey());
}
for (final Map.Entry<String, List<Long>> me :
expectedStatistics.entrySet()) {
- assertEquals(me.getValue(),
actualStatistics.get(me.getKey()).get(0),
- "Mismatch of stats with expected value for: " +
me.getKey());
+ assertEquals(me.getValue(),
actualStatistics.get(me.getKey()).get(0), "Mismatch of stats with expected
value for: " + me.getKey());
}
}
@@ -567,11 +549,9 @@ public final class ZipTestCase extends AbstractTestCase {
}
/**
- * Checks if all entries from a nested archive can be read.
- * The archive: OSX_ArchiveWithNestedArchive.zip contains:
- * NestedArchiv.zip and test.xml3.
+ * Checks if all entries from a nested archive can be read. The archive:
OSX_ArchiveWithNestedArchive.zip contains: NestedArchiv.zip and test.xml3.
*
- * The nested archive: NestedArchive.zip contains test1.xml and test2.xml
+ * The nested archive: NestedArchive.zip contains test1.xml and test2.xml
*
* @throws Exception
*/
@@ -611,30 +591,23 @@ public final class ZipTestCase extends AbstractTestCase {
}
/**
- * Test case for being able to skip an entry in an
- * {@link ZipArchiveInputStream} even if the compression method of that
- * entry is unsupported.
+ * Test case for being able to skip an entry in an {@link
ZipArchiveInputStream} even if the compression method of that entry is
unsupported.
*
- * @see <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
- * >COMPRESS-93</a>
+ * @see <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
>COMPRESS-93</a>
*/
@Test
- public void testSkipEntryWithUnsupportedCompressionMethod()
- throws IOException {
+ public void testSkipEntryWithUnsupportedCompressionMethod() throws
IOException {
try (ZipArchiveInputStream zip = new
ZipArchiveInputStream(newInputStream("moby.zip"))) {
final ZipArchiveEntry entry = zip.getNextZipEntry();
assertEquals(ZipMethod.TOKENIZATION.getCode(), entry.getMethod(),
"method");
assertEquals("README", entry.getName());
assertFalse(zip.canReadEntryData(entry));
- assertDoesNotThrow(() -> assertNull(zip.getNextZipEntry()),
- "COMPRESS-93: Unable to skip an unsupported ZIP entry");
+ assertDoesNotThrow(() -> assertNull(zip.getNextZipEntry()),
"COMPRESS-93: Unable to skip an unsupported ZIP entry");
}
}
/**
- * Test case for
- * <a href="https://issues.apache.org/jira/browse/COMPRESS-208"
- * >COMPRESS-208</a>.
+ * Test case for <a
href="https://issues.apache.org/jira/browse/COMPRESS-208" >COMPRESS-208</a>.
*/
@Test
public void testSkipsPK00Prefix() throws Exception {
@@ -648,9 +621,7 @@ public final class ZipTestCase extends AbstractTestCase {
}
/**
- * Test case for
- * <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
- * >COMPRESS-93</a>.
+ * Test case for <a
href="https://issues.apache.org/jira/browse/COMPRESS-93" >COMPRESS-93</a>.
*/
@Test
public void testTokenizationCompressionMethod() throws IOException {
@@ -679,8 +650,8 @@ public final class ZipTestCase extends AbstractTestCase {
}
/**
- * Archives 2 files and unarchives it again. If the file length of result
- * and source is the same, it looks like the operations have worked
+ * Archives 2 files and unarchives it again. If the file length of result
and source is the same, it looks like the operations have worked
+ *
* @throws Exception
*/
@Test
@@ -706,8 +677,7 @@ public final class ZipTestCase extends AbstractTestCase {
final List<File> results = new ArrayList<>();
try (final InputStream fileInputStream =
Files.newInputStream(output.toPath())) {
- try (ArchiveInputStream<ZipArchiveEntry> archiveInputStream =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip",
- fileInputStream)) {
+ try (ArchiveInputStream<ZipArchiveEntry> archiveInputStream =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", fileInputStream)) {
ZipArchiveEntry entry;
while ((entry = archiveInputStream.getNextEntry()) != null) {
final File outfile = new
File(tempResultDir.getCanonicalPath() + "/result/" + entry.getName());
@@ -726,8 +696,8 @@ public final class ZipTestCase extends AbstractTestCase {
}
/**
- * Archives 2 files and unarchives it again. If the file contents of result
- * and source is the same, it looks like the operations have worked
+ * Archives 2 files and unarchives it again. If the file contents of
result and source is the same, it looks like the operations have worked
+ *
* @throws Exception
*/
@Test
@@ -752,8 +722,7 @@ public final class ZipTestCase extends AbstractTestCase {
}
// Unarchive the same
- try (ZipArchiveInputStream inputStream =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip",
- new ByteArrayInputStream(channel.array()))) {
+ try (ZipArchiveInputStream inputStream =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", new
ByteArrayInputStream(channel.array()))) {
ZipArchiveEntry entry;
while ((entry = inputStream.getNextEntry()) != null) {
@@ -772,9 +741,8 @@ public final class ZipTestCase extends AbstractTestCase {
Path archivePath;
ZipArchiveOutputStream zos = null;
ZipFile zf = null;
- InputStream fis = null;
final File tmpFile = createTempFile();
-
+
final Path tmpFilePath = tmpFile.toPath();
try {
final File archiveFile = createTempFile("test.", ".zip");
@@ -784,12 +752,11 @@ public final class ZipTestCase extends AbstractTestCase {
final ZipArchiveEntry in = zos.createArchiveEntry(tmpFilePath,
"foo");
zos.putArchiveEntry(in);
final byte[] b = new byte[(int) tmpFile.length()];
- fis = Files.newInputStream(tmpFile.toPath());
- while (fis.read(b) > 0) {
- zos.write(b);
+ try (InputStream fis = Files.newInputStream(tmpFile.toPath())) {
+ while (fis.read(b) > 0) {
+ zos.write(b);
+ }
}
- fis.close();
- fis = null;
zos.closeArchiveEntry();
zos.close();
zos = null;
@@ -805,14 +772,12 @@ public final class ZipTestCase extends AbstractTestCase {
if (zos != null) {
zos.close();
}
- if (fis != null) {
- fis.close();
- }
}
}
/**
* Simple unarchive test. Asserts nothing.
+ *
* @throws Exception
*/
@Test