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 fcf7e5e1777ac8833a24326e635500e114d514b5 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Nov 12 09:20:28 2023 -0500 Reuse existing CharsetNames.UTF_8 constant --- .../archivers/zip/ZipArchiveInputStream.java | 3 +- .../archivers/zip/ZipArchiveOutputStream.java | 3 +- .../compress/archivers/zip/ZipEncodingHelper.java | 10 ++---- .../commons/compress/archivers/zip/ZipFile.java | 9 ++--- .../harmony/unpack200/bytecode/CPUTF8.java | 4 ++- .../archivers/ArchiveStreamFactoryTest.java | 41 +++++++++++----------- .../archivers/cpio/CpioArchiveInputStreamTest.java | 3 +- .../compress/archivers/cpio/CpioArchiveTest.java | 9 ++--- .../zip/ParallelScatterZipCreatorTest.java | 9 ++--- .../archivers/zip/ZipArchiveInputStreamTest.java | 27 +++++++------- .../compress/archivers/zip/ZipEncodingTest.java | 5 +-- .../zip/ZipFileIgnoringLocalFileHeaderTest.java | 3 +- .../compress/archivers/zip/ZipFileTest.java | 5 +-- .../archivers/zip/ZipMemoryFileSystemTest.java | 3 +- .../CompressorStreamFactoryRoundtripTest.java | 6 ++-- 15 files changed, 75 insertions(+), 65 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index 7b750ad07..846c12587 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -45,6 +45,7 @@ import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream; import org.apache.commons.compress.utils.ArchiveUtils; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.InputStreamStatistics; @@ -364,7 +365,7 @@ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> i * @param inputStream the stream to wrap */ public ZipArchiveInputStream(final InputStream inputStream) { - this(inputStream, ZipEncodingHelper.UTF8); + this(inputStream, CharsetNames.UTF_8); } /** diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java index 9099d5fe7..768e5083e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java @@ -39,6 +39,7 @@ import java.util.zip.ZipException; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; /** @@ -213,7 +214,7 @@ public class ZipArchiveOutputStream extends ArchiveOutputStream<ZipArchiveEntry> /** * default encoding for file names and comment. */ - static final String DEFAULT_ENCODING = ZipEncodingHelper.UTF8; + static final String DEFAULT_ENCODING = CharsetNames.UTF_8; /** * General purpose flag, which indicates that file names are diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java index ce11e4ea6..581e519cd 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java @@ -23,21 +23,17 @@ import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; +import org.apache.commons.compress.utils.CharsetNames; + /** * Static helper functions for robustly encoding file names in ZIP files. */ public abstract class ZipEncodingHelper { - - /** - * name of the encoding UTF-8 - */ - static final String UTF8 = "UTF8"; - /** * the encoding UTF-8 */ - static final ZipEncoding UTF8_ZIP_ENCODING = getZipEncoding(UTF8); + static final ZipEncoding UTF8_ZIP_ENCODING = getZipEncoding(CharsetNames.UTF_8); /** * Instantiates a ZIP encoding. An NIO based character set encoder/decoder will be returned. diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java index ebc0d047a..e4545200e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java @@ -47,6 +47,7 @@ import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream; import org.apache.commons.compress.utils.BoundedArchiveInputStream; import org.apache.commons.compress.utils.BoundedSeekableByteChannelInputStream; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.CountingInputStream; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.InputStreamStatistics; @@ -442,7 +443,7 @@ public class ZipFile implements Closeable { * @throws IOException if an error occurs while reading the file. */ public ZipFile(final File f) throws IOException { - this(f, ZipEncodingHelper.UTF8); + this(f, CharsetNames.UTF_8); } /** @@ -502,7 +503,7 @@ public class ZipFile implements Closeable { * @since 1.22 */ public ZipFile(final Path path) throws IOException { - this(path, ZipEncodingHelper.UTF8); + this(path, CharsetNames.UTF_8); } /** @@ -565,7 +566,7 @@ public class ZipFile implements Closeable { * @since 1.13 */ public ZipFile(final SeekableByteChannel channel) throws IOException { - this(channel, "unknown archive", ZipEncodingHelper.UTF8, true); + this(channel, "unknown archive", CharsetNames.UTF_8, true); } /** @@ -668,7 +669,7 @@ public class ZipFile implements Closeable { * @throws IOException if an error occurs while reading the file. */ public ZipFile(final String name) throws IOException { - this(new File(name).toPath(), ZipEncodingHelper.UTF8); + this(new File(name).toPath(), CharsetNames.UTF_8); } /** diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPUTF8.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPUTF8.java index 346d7576e..f7eae1dd6 100644 --- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPUTF8.java +++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPUTF8.java @@ -20,6 +20,8 @@ import java.io.DataOutputStream; import java.io.IOException; import java.util.Objects; +import org.apache.commons.compress.utils.CharsetNames; + /** * UTF8 constant pool entry, used for storing long Strings. */ @@ -81,7 +83,7 @@ public class CPUTF8 extends ConstantPoolEntry { @Override public String toString() { - return "UTF8: " + utf8; + return CharsetNames.UTF_8 + ":" + utf8; } public String underlyingString() { diff --git a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java index aee3c0832..23adb65a8 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java @@ -39,6 +39,7 @@ import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.io.input.BrokenInputStream; import org.junit.jupiter.api.Test; @@ -73,11 +74,11 @@ public class ArchiveStreamFactoryTest extends AbstractTest { // The different factory types private static final ArchiveStreamFactory FACTORY = ArchiveStreamFactory.DEFAULT; - private static final ArchiveStreamFactory FACTORY_UTF8 = new ArchiveStreamFactory("UTF-8"); + private static final ArchiveStreamFactory FACTORY_UTF8 = new ArchiveStreamFactory(CharsetNames.UTF_8); private static final ArchiveStreamFactory FACTORY_ASCII = new ArchiveStreamFactory("ASCII"); - private static final ArchiveStreamFactory FACTORY_SET_UTF8 = getFactory("UTF-8"); + private static final ArchiveStreamFactory FACTORY_SET_UTF8 = getFactory(CharsetNames.UTF_8); private static final ArchiveStreamFactory FACTORY_SET_ASCII = getFactory("ASCII"); @@ -115,39 +116,39 @@ public class ArchiveStreamFactoryTest extends AbstractTest { static final TestData[] TESTS = { new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, ARJ_DEFAULT, FACTORY, "charsetName"), - new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, "UTF-8", FACTORY_UTF8, "charsetName"), + new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, CharsetNames.UTF_8, FACTORY_UTF8, "charsetName"), new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, "ASCII", FACTORY_ASCII, "charsetName"), - new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, "UTF-8", FACTORY_SET_UTF8, "charsetName"), + new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, CharsetNames.UTF_8, FACTORY_SET_UTF8, "charsetName"), new TestData("bla.arj", ArchiveStreamFactory.ARJ, false, "ASCII", FACTORY_SET_ASCII, "charsetName"), new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, CPIO_DEFAULT, FACTORY, "encoding"), - new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, "UTF-8", FACTORY_UTF8, "encoding"), + new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, CharsetNames.UTF_8, FACTORY_UTF8, "encoding"), new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, "ASCII", FACTORY_ASCII, "encoding"), - new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, "UTF-8", FACTORY_SET_UTF8, "encoding"), + new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, CharsetNames.UTF_8, FACTORY_SET_UTF8, "encoding"), new TestData("bla.cpio", ArchiveStreamFactory.CPIO, true, "ASCII", FACTORY_SET_ASCII, "encoding"), new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, DUMP_DEFAULT, FACTORY, "encoding"), - new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, "UTF-8", FACTORY_UTF8, "encoding"), + new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, CharsetNames.UTF_8, FACTORY_UTF8, "encoding"), new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, "ASCII", FACTORY_ASCII, "encoding"), - new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, "UTF-8", FACTORY_SET_UTF8, "encoding"), + new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, CharsetNames.UTF_8, FACTORY_SET_UTF8, "encoding"), new TestData("bla.dump", ArchiveStreamFactory.DUMP, false, "ASCII", FACTORY_SET_ASCII, "encoding"), new TestData("bla.tar", ArchiveStreamFactory.TAR, true, TAR_DEFAULT, FACTORY, "encoding"), - new TestData("bla.tar", ArchiveStreamFactory.TAR, true, "UTF-8", FACTORY_UTF8, "encoding"), + new TestData("bla.tar", ArchiveStreamFactory.TAR, true, CharsetNames.UTF_8, FACTORY_UTF8, "encoding"), new TestData("bla.tar", ArchiveStreamFactory.TAR, true, "ASCII", FACTORY_ASCII, "encoding"), - new TestData("bla.tar", ArchiveStreamFactory.TAR, true, "UTF-8", FACTORY_SET_UTF8, "encoding"), + new TestData("bla.tar", ArchiveStreamFactory.TAR, true, CharsetNames.UTF_8, FACTORY_SET_UTF8, "encoding"), new TestData("bla.tar", ArchiveStreamFactory.TAR, true, "ASCII", FACTORY_SET_ASCII, "encoding"), new TestData("bla.jar", ArchiveStreamFactory.JAR, true, JAR_DEFAULT, FACTORY, "encoding"), - new TestData("bla.jar", ArchiveStreamFactory.JAR, true, "UTF-8", FACTORY_UTF8, "encoding"), + new TestData("bla.jar", ArchiveStreamFactory.JAR, true, CharsetNames.UTF_8, FACTORY_UTF8, "encoding"), new TestData("bla.jar", ArchiveStreamFactory.JAR, true, "ASCII", FACTORY_ASCII, "encoding"), - new TestData("bla.jar", ArchiveStreamFactory.JAR, true, "UTF-8", FACTORY_SET_UTF8, "encoding"), + new TestData("bla.jar", ArchiveStreamFactory.JAR, true, CharsetNames.UTF_8, FACTORY_SET_UTF8, "encoding"), new TestData("bla.jar", ArchiveStreamFactory.JAR, true, "ASCII", FACTORY_SET_ASCII, "encoding"), new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, ZIP_DEFAULT, FACTORY, "encoding"), - new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, "UTF-8", FACTORY_UTF8, "encoding"), + new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, CharsetNames.UTF_8, FACTORY_UTF8, "encoding"), new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, "ASCII", FACTORY_ASCII, "encoding"), - new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, "UTF-8", FACTORY_SET_UTF8, "encoding"), + new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, CharsetNames.UTF_8, FACTORY_SET_UTF8, "encoding"), new TestData("bla.zip", ArchiveStreamFactory.ZIP, true, "ASCII", FACTORY_SET_ASCII, "encoding"), }; // equals allowing null @@ -304,8 +305,8 @@ public class ArchiveStreamFactoryTest extends AbstractTest { assertNull(fac.getEntryEncoding()); fac = new ArchiveStreamFactory(null); assertNull(fac.getEntryEncoding()); - fac = new ArchiveStreamFactory("UTF-8"); - assertEquals("UTF-8", fac.getEntryEncoding()); + fac = new ArchiveStreamFactory(CharsetNames.UTF_8); + assertEquals(CharsetNames.UTF_8, fac.getEntryEncoding()); } @Test @@ -313,12 +314,12 @@ public class ArchiveStreamFactoryTest extends AbstractTest { public void testEncodingDeprecated() { final ArchiveStreamFactory fac1 = new ArchiveStreamFactory(); assertNull(fac1.getEntryEncoding()); - fac1.setEntryEncoding("UTF-8"); - assertEquals("UTF-8", fac1.getEntryEncoding()); + fac1.setEntryEncoding(CharsetNames.UTF_8); + assertEquals(CharsetNames.UTF_8, fac1.getEntryEncoding()); fac1.setEntryEncoding("US_ASCII"); assertEquals("US_ASCII", fac1.getEntryEncoding()); - final ArchiveStreamFactory fac2 = new ArchiveStreamFactory("UTF-8"); - assertEquals("UTF-8", fac2.getEntryEncoding()); + final ArchiveStreamFactory fac2 = new ArchiveStreamFactory(CharsetNames.UTF_8); + assertEquals(CharsetNames.UTF_8, fac2.getEntryEncoding()); assertThrows(IllegalStateException.class, () -> fac2.setEntryEncoding("US_ASCII"), "Expected IllegalStateException"); } diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java index 6e4324758..ba046a7e1 100644 --- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java @@ -25,6 +25,7 @@ import java.io.InputStream; import org.apache.commons.compress.AbstractTest; import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.junit.jupiter.api.Test; @@ -70,7 +71,7 @@ public class CpioArchiveInputStreamTest extends AbstractTest { @Test public void testCpioUnarchiveMultibyteCharName() throws Exception { int count = 0; - try (final CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("COMPRESS-459.cpio"), "UTF-8")) { + try (final CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("COMPRESS-459.cpio"), CharsetNames.UTF_8)) { CpioArchiveEntry entry; while ((entry = in.getNextEntry()) != null) { diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java index 62cfd6ae0..5295e059b 100644 --- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java @@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.stream.Stream; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -42,22 +43,22 @@ public class CpioArchiveTest { @MethodSource("factory") public void utf18RoundtripTest(final short format) throws Exception { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, format, CpioConstants.BLOCK_SIZE, "UTF-16LE")) { + try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, format, CpioConstants.BLOCK_SIZE, CharsetNames.UTF_16LE)) { final CpioArchiveEntry entry = new CpioArchiveEntry(format, "T\u00e4st.txt", 4); if (format == CpioConstants.FORMAT_NEW_CRC) { entry.setChksum(10); } os.putArchiveEntry(entry); - os.write(new byte[] {1, 2, 3, 4}); + os.write(new byte[] { 1, 2, 3, 4 }); os.closeArchiveEntry(); } baos.close(); try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray()); - CpioArchiveInputStream in = new CpioArchiveInputStream(bin, "UTF-16LE")) { + CpioArchiveInputStream in = new CpioArchiveInputStream(bin, CharsetNames.UTF_16LE)) { final CpioArchiveEntry entry = in.getNextEntry(); assertNotNull(entry); assertEquals("T\u00e4st.txt", entry.getName()); - assertArrayEquals(new byte[] {1, 2, 3, 4}, IOUtils.toByteArray(in)); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, IOUtils.toByteArray(in)); } } } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java index 52d3de42a..aa8b55e21 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ParallelScatterZipCreatorTest.java @@ -49,6 +49,7 @@ import org.apache.commons.compress.AbstractTest; import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore; import org.apache.commons.compress.parallel.InputStreamSupplier; import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -76,7 +77,7 @@ public class ParallelScatterZipCreatorTest extends AbstractTempDirTest { final Map<String, byte[]> entries; final ParallelScatterZipCreator zipCreator; try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result)) { - zos.setEncoding("UTF-8"); + zos.setEncoding(CharsetNames.UTF_8); final ExecutorService es = Executors.newFixedThreadPool(1); final ScatterGatherBackingStoreSupplier supp = () -> new FileBasedScatterGatherBackingStore(createTempFile("parallelscatter", "n1")); @@ -95,7 +96,7 @@ public class ParallelScatterZipCreatorTest extends AbstractTempDirTest { final ParallelScatterZipCreator zipCreator; final Map<String, byte[]> entries; try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result)) { - zos.setEncoding("UTF-8"); + zos.setEncoding(CharsetNames.UTF_8); final ExecutorService es = Executors.newFixedThreadPool(1); final ScatterGatherBackingStoreSupplier supp = () -> new FileBasedScatterGatherBackingStore(createTempFile("parallelscatter", "n1")); @@ -202,7 +203,7 @@ public class ParallelScatterZipCreatorTest extends AbstractTempDirTest { final ParallelScatterZipCreator zipCreator; final Map<String, byte[]> entries; try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result)) { - zos.setEncoding("UTF-8"); + zos.setEncoding(CharsetNames.UTF_8); // Formatter:off final Path dir = Paths.get("target/custom-temp-dir"); @@ -225,7 +226,7 @@ public class ParallelScatterZipCreatorTest extends AbstractTempDirTest { final ParallelScatterZipCreator zipCreator; final Map<String, byte[]> entries; try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result)) { - zos.setEncoding("UTF-8"); + zos.setEncoding(CharsetNames.UTF_8); zipCreator = new ParallelScatterZipCreator(); entries = writeEntries(zipCreator); diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java index b172724a5..f13369234 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java @@ -47,6 +47,7 @@ import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -306,7 +307,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testProperlyReadsStoredEntryWithDataDescriptorWithoutSignature() throws IOException { try (InputStream fs = newInputStream("bla-stored-dd-nosig.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, "UTF-8", true, true)) { + ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, CharsetNames.UTF_8, true, true)) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -322,7 +323,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testProperlyReadsStoredEntryWithDataDescriptorWithSignature() throws IOException { try (InputStream fs = newInputStream("bla-stored-dd.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, "UTF-8", true, true)) { + ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, CharsetNames.UTF_8, true, true)) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -488,10 +489,10 @@ public class ZipArchiveInputStreamTest extends AbstractTest { final File lastFile = getFile("COMPRESS-477/split_zip_created_by_winrar/split_zip_created_by_winrar.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, ZipEncodingHelper.UTF8, true, false, true)) { + ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, CharsetNames.UTF_8, true, false, true)) { final File fileToCompare = getFile("COMPRESS-477/split_zip_created_by_winrar/zip_to_compare_created_by_winrar.zip"); - try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), ZipEncodingHelper.UTF8, + try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), CharsetNames.UTF_8, true, false, true)) { ArchiveEntry entry; @@ -510,10 +511,10 @@ public class ZipArchiveInputStreamTest extends AbstractTest { final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, ZipEncodingHelper.UTF8, true, false, true)) { + ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, CharsetNames.UTF_8, true, false, true)) { final File fileToCompare = getFile("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip.zip"); - try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), ZipEncodingHelper.UTF8, + try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), CharsetNames.UTF_8, true, false, true)) { ArchiveEntry entry; @@ -532,10 +533,10 @@ public class ZipArchiveInputStreamTest extends AbstractTest { final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip_zip64.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); - ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, ZipEncodingHelper.UTF8, true, false, true)) { + ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, CharsetNames.UTF_8, true, false, true)) { final File fileToCompare = getFile("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip_zip64.zip"); - try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), ZipEncodingHelper.UTF8, + try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()), CharsetNames.UTF_8, true, false, true)) { ArchiveEntry entry; @@ -552,7 +553,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testSplitZipCreatedByZipThrowsException() throws IOException { final File zipSplitFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01"); - try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(Files.newInputStream(zipSplitFile.toPath()), ZipEncodingHelper.UTF8, true, false, + try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(Files.newInputStream(zipSplitFile.toPath()), CharsetNames.UTF_8, true, false, true)) { assertThrows(EOFException.class, () -> { @@ -578,7 +579,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testThrowsIfStoredDDIsDifferentFromLengthRead() throws IOException { try (InputStream fs = newInputStream("bla-stored-dd-contradicts-actualsize.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, "UTF-8", true, true)) { + ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, CharsetNames.UTF_8, true, true)) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -591,7 +592,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testThrowsIfStoredDDIsInconsistent() throws IOException { try (InputStream fs = newInputStream("bla-stored-dd-sizes-differ.zip"); - ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, "UTF-8", true, true)) { + ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, CharsetNames.UTF_8, true, true)) { final ZipArchiveEntry e = archive.getNextZipEntry(); assertNotNull(e); assertEquals("test1.xml", e.getName()); @@ -701,7 +702,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @Test public void testZipUsingStoredWithDDAndNoDDSignature() throws IOException { try (InputStream inputStream = forgeZipInputStream(); - ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream, "UTF-8", true, true)) { + ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream, CharsetNames.UTF_8, true, true)) { getAllZipEntries(zipInputStream); } } @@ -718,7 +719,7 @@ public class ZipArchiveInputStreamTest extends AbstractTest { @ValueSource(booleans = { true, false }) public void zipInputStream(final boolean allowStoredEntriesWithDataDescriptor) { try (ZipArchiveInputStream zIn = new ZipArchiveInputStream( - Files.newInputStream(Paths.get("src/test/resources/COMPRESS-647/test.zip")), "UTF-8", false, + Files.newInputStream(Paths.get("src/test/resources/COMPRESS-647/test.zip")), CharsetNames.UTF_8, false, allowStoredEntriesWithDataDescriptor)) { ZipArchiveEntry zae = zIn.getNextEntry(); while (zae != null) { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java index d8f5bea08..df1156a2a 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import org.apache.commons.compress.utils.CharsetNames; import org.hamcrest.core.IsInstanceOf; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -111,9 +112,9 @@ public class ZipEncodingTest { @Test public void testIsUTF8() { - assertTrue(ZipEncodingHelper.isUTF8("UTF-8")); + assertTrue(ZipEncodingHelper.isUTF8(CharsetNames.UTF_8)); assertTrue(ZipEncodingHelper.isUTF8("UTF8")); - Assertions.assertEquals(Charset.defaultCharset().name().equals("UTF-8"), ZipEncodingHelper.isUTF8(null)); + Assertions.assertEquals(Charset.defaultCharset().name().equals(CharsetNames.UTF_8), ZipEncodingHelper.isUTF8(null)); } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java index f8ff381e7..d567e91f6 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java @@ -27,13 +27,14 @@ import java.nio.file.Files; import java.util.Enumeration; import org.apache.commons.compress.AbstractTest; +import org.apache.commons.compress.utils.CharsetNames; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; public class ZipFileIgnoringLocalFileHeaderTest { private static ZipFile openZipWithoutLFH(final String fileName) throws IOException { - return new ZipFile(AbstractTest.getFile(fileName), ZipEncodingHelper.UTF8, true, true); + return new ZipFile(AbstractTest.getFile(fileName), CharsetNames.UTF_8, true, true); } @TempDir diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java index 77714979c..bfadce4de 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java @@ -53,6 +53,7 @@ import java.util.zip.ZipEntry; import org.apache.commons.compress.AbstractTest; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; import org.apache.commons.io.function.IORunnable; @@ -246,7 +247,7 @@ public class ZipFileTest extends AbstractTest { } try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(data)) { - zf = new ZipFile(channel, ZipEncodingHelper.UTF8); + zf = new ZipFile(channel, CharsetNames.UTF_8); final ArrayList<ZipArchiveEntry> l = Collections.list(zf.getEntries()); assertEntryName(l, 0, "AbstractUnicodeExtraField"); assertEntryName(l, 1, "AsiExtraField"); @@ -311,7 +312,7 @@ public class ZipFileTest extends AbstractTest { data = IOUtils.toByteArray(fis); } try (final SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(data)) { - zf = new ZipFile(channel, ZipEncodingHelper.UTF8); + zf = new ZipFile(channel, CharsetNames.UTF_8); final Map<String, byte[]> content = new HashMap<>(); for (final ZipArchiveEntry entry : Collections.list(zf.getEntries())) { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipMemoryFileSystemTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipMemoryFileSystemTest.java index f41a7e87d..479467423 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipMemoryFileSystemTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipMemoryFileSystemTest.java @@ -50,6 +50,7 @@ import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.parallel.InputStreamSupplier; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.io.file.PathUtils; import org.junit.jupiter.api.AfterEach; @@ -214,7 +215,7 @@ public class ZipMemoryFileSystemTest { } } - try (final ZipFile zf = new ZipFile(Files.newByteChannel(target, StandardOpenOption.READ), target.getFileName().toString(), ZipEncodingHelper.UTF8, + try (final ZipFile zf = new ZipFile(Files.newByteChannel(target, StandardOpenOption.READ), target.getFileName().toString(), CharsetNames.UTF_8, true)) { final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next(); assertEquals(8, b_entry.getSize()); diff --git a/src/test/java/org/apache/commons/compress/compressors/CompressorStreamFactoryRoundtripTest.java b/src/test/java/org/apache/commons/compress/compressors/CompressorStreamFactoryRoundtripTest.java index dc86ba5b2..3d06de378 100644 --- a/src/test/java/org/apache/commons/compress/compressors/CompressorStreamFactoryRoundtripTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/CompressorStreamFactoryRoundtripTest.java @@ -18,13 +18,13 @@ */ package org.apache.commons.compress.compressors; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.stream.Stream; +import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -55,7 +55,7 @@ public class CompressorStreamFactoryRoundtripTest { final ByteArrayOutputStream compressedOs = new ByteArrayOutputStream(); final String fixture = "The quick brown fox jumps over the lazy dog"; try (CompressorOutputStream compressorOutputStream = factory.createCompressorOutputStream(compressorName, compressedOs)) { - compressorOutputStream.write(fixture.getBytes(UTF_8)); + compressorOutputStream.write(fixture.getBytes(CharsetNames.UTF_8)); compressorOutputStream.flush(); } final ByteArrayInputStream is = new ByteArrayInputStream(compressedOs.toByteArray()); @@ -65,7 +65,7 @@ public class CompressorStreamFactoryRoundtripTest { compressorInputStream.close(); decompressedOs.flush(); decompressedOs.close(); - assertEquals(fixture, decompressedOs.toString("UTF-8")); + assertEquals(fixture, decompressedOs.toString(CharsetNames.UTF_8)); } }