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 648d509512d6c8813583a1a59e94300b3d97c791 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Nov 17 09:35:13 2024 -0500 Sort members --- .../commons/compress/compressors/gzip/Extra.java | 170 ++++++++++----------- .../archivers/tar/TarArchiveInputStreamTest.java | 114 +++++++------- .../gzip/GzipCompressorOutputStreamTest.java | 74 ++++----- 3 files changed, 179 insertions(+), 179 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/Extra.java b/src/main/java/org/apache/commons/compress/compressors/gzip/Extra.java index 213c1f14c..706246ad9 100644 --- a/src/main/java/org/apache/commons/compress/compressors/gzip/Extra.java +++ b/src/main/java/org/apache/commons/compress/compressors/gzip/Extra.java @@ -36,9 +36,44 @@ import java.util.stream.Collectors; */ public class Extra { + /** + * The carrier for a subfield in the gzip extra. + */ + public static class SubField { + byte si1; + byte si2; + byte[] payload; + + SubField() { + } + + SubField(byte si1, byte si2, byte[] payload) { + this.si1 = si1; + this.si2 = si2; + this.payload = payload; + } + + /** + * The 2 char iso-8859-1 string made from the si1 and si2 bytes of the sub field + * id. + */ + public String getId() { + return "" + ((char) (si1 & 0xff)) + ((char) (si2 & 0xff)); + } + + /** + * The subfield payload. + */ + public byte[] getPayload() { + return payload; + } + } static final int MAX_SIZE = 0xFFFF; + static final byte[] ZERO_BYTES = new byte[0]; + // -------------- + static Extra fromBytes(byte[] ba) throws IOException { if (ba == null) { return null; @@ -71,46 +106,13 @@ public class Extra { return e; } - - // -------------- - private final List<SubField> fieldsList = new ArrayList<>(); + private int totalSize = 0; public Extra() { } - public boolean isEmpty() { - return fieldsList.isEmpty(); - } - - public void clear() { - fieldsList.clear(); - totalSize = 0; - } - - /** - * The bytes count of this extra payload when encoded. This does not include its - * own 16 bits size. For N sub fields, the total is all subfields payloads + 4N. - */ - public int getEncodedSize() { - return totalSize; - } - - /** - * The count of subfields contained in this extra. - */ - public int getSize() { - return fieldsList.size(); - } - - /** - * @return an unmodifiable copy of the subfields list. - */ - public List<SubField> getFieldsList() { - return Collections.unmodifiableList(fieldsList); - } - /** * Append a subfield by a 2-chars ISO-8859-1 string. The char at index 0 and 1 * are respectiovely si1 and si2 (subfield id 1 and 2). @@ -147,23 +149,44 @@ public class Extra { return this; } - byte[] toBytes() { - if (fieldsList.isEmpty()) { - return ZERO_BYTES; - } + public void clear() { + fieldsList.clear(); + totalSize = 0; + } - byte[] ba = new byte[totalSize]; + /** + * Find the 1st subfield that matches the id. + * + * @return the SubField if found, null otherwise. + */ + public SubField findFirstSubField(String subfieldId) { + return fieldsList.stream().filter(f -> f.getId().equals(subfieldId)).findFirst().orElse(null); + } - int pos = 0; - for (SubField f : fieldsList) { - ba[pos++] = f.si1; - ba[pos++] = f.si2; - ba[pos++] = (byte) (f.payload.length & 0xff); // little endian expected - ba[pos++] = (byte) (f.payload.length >>> 8); - System.arraycopy(f.payload, 0, ba, pos, f.payload.length); - pos += f.payload.length; - } - return ba; + /** + * The bytes count of this extra payload when encoded. This does not include its + * own 16 bits size. For N sub fields, the total is all subfields payloads + 4N. + */ + public int getEncodedSize() { + return totalSize; + } + + /** + * @return an unmodifiable copy of the subfields list. + */ + public List<SubField> getFieldsList() { + return Collections.unmodifiableList(fieldsList); + } + + /** + * The count of subfields contained in this extra. + */ + public int getSize() { + return fieldsList.size(); + } + + public boolean isEmpty() { + return fieldsList.isEmpty(); } /** @@ -175,15 +198,6 @@ public class Extra { return fieldsList.stream().map(SubField::getId).collect(Collectors.toList()); } - /** - * Find the 1st subfield that matches the id. - * - * @return the SubField if found, null otherwise. - */ - public SubField findFirstSubField(String subfieldId) { - return fieldsList.stream().filter(f -> f.getId().equals(subfieldId)).findFirst().orElse(null); - } - /** * Find the subfield at the given index. */ @@ -191,37 +205,23 @@ public class Extra { return fieldsList.get(i); } - /** - * The carrier for a subfield in the gzip extra. - */ - public static class SubField { - byte si1; - byte si2; - byte[] payload; - - SubField() { - } - - SubField(byte si1, byte si2, byte[] payload) { - this.si1 = si1; - this.si2 = si2; - this.payload = payload; + byte[] toBytes() { + if (fieldsList.isEmpty()) { + return ZERO_BYTES; } - /** - * The 2 char iso-8859-1 string made from the si1 and si2 bytes of the sub field - * id. - */ - public String getId() { - return "" + ((char) (si1 & 0xff)) + ((char) (si2 & 0xff)); - } + byte[] ba = new byte[totalSize]; - /** - * The subfield payload. - */ - public byte[] getPayload() { - return payload; + int pos = 0; + for (SubField f : fieldsList) { + ba[pos++] = f.si1; + ba[pos++] = f.si2; + ba[pos++] = (byte) (f.payload.length & 0xff); // little endian expected + ba[pos++] = (byte) (f.payload.length >>> 8); + System.arraycopy(f.payload, 0, ba, pos, f.payload.length); + pos += f.payload.length; } + return ba; } } diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java index 0692b98a8..e8da935c3 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java @@ -135,6 +135,63 @@ public class TarArchiveInputStreamTest extends AbstractTest { } } + private void testCompress666(final int factor, final boolean bufferInputStream, final String localPath) { + final ExecutorService executorService = Executors.newFixedThreadPool(10); + try { + final List<Future<?>> tasks = IntStream.range(0, 200).mapToObj(index -> executorService.submit(() -> { + TarArchiveEntry tarEntry = null; + try (InputStream inputStream = getClass().getResourceAsStream(localPath); + TarArchiveInputStream tarInputStream = new TarArchiveInputStream( + bufferInputStream ? new BufferedInputStream(new GZIPInputStream(inputStream)) : new GZIPInputStream(inputStream), + TarConstants.DEFAULT_RCDSIZE * factor, TarConstants.DEFAULT_RCDSIZE)) { + while ((tarEntry = tarInputStream.getNextEntry()) != null) { + assertNotNull(tarEntry); + } + } catch (final IOException e) { + fail(Objects.toString(tarEntry), e); + } + })).collect(Collectors.toList()); + final List<Exception> list = new ArrayList<>(); + for (final Future<?> future : tasks) { + try { + future.get(); + } catch (final Exception e) { + list.add(e); + } + } + // check: + if (!list.isEmpty()) { + fail(list.get(0)); + } + // or: + // assertTrue(list.isEmpty(), () -> list.size() + " exceptions: " + list.toString()); + } finally { + executorService.shutdownNow(); + } + } + + /** + * Tests https://issues.apache.org/jira/browse/COMPRESS-666 + * + * A factor of 20 is the default. + */ + @ParameterizedTest + @ValueSource(ints = { 1, 2, 4, 8, 16, 20, 32, 64, 128 }) + public void testCompress666Buffered(final int factor) { + testCompress666(factor, true, "/COMPRESS-666/compress-666.tar.gz"); + } + + /** + * Tests https://issues.apache.org/jira/browse/COMPRESS-666 + * + * A factor of 20 is the default. + */ + @ParameterizedTest + @ValueSource(ints = { 1, 2, 4, 8, 16, 20, 32, 64, 128 }) + public void testCompress666Unbuffered(final int factor) { + testCompress666(factor, false, "/COMPRESS-666/compress-666.tar.gz"); + } + @Test public void testDatePriorToEpochInGNUFormat() throws Exception { datePriorToEpoch("preepoch-star.tar"); @@ -449,61 +506,4 @@ public class TarArchiveInputStreamTest extends AbstractTest { assertTrue(tae.isCheckSumOK()); } } - - private void testCompress666(final int factor, final boolean bufferInputStream, final String localPath) { - final ExecutorService executorService = Executors.newFixedThreadPool(10); - try { - final List<Future<?>> tasks = IntStream.range(0, 200).mapToObj(index -> executorService.submit(() -> { - TarArchiveEntry tarEntry = null; - try (InputStream inputStream = getClass().getResourceAsStream(localPath); - TarArchiveInputStream tarInputStream = new TarArchiveInputStream( - bufferInputStream ? new BufferedInputStream(new GZIPInputStream(inputStream)) : new GZIPInputStream(inputStream), - TarConstants.DEFAULT_RCDSIZE * factor, TarConstants.DEFAULT_RCDSIZE)) { - while ((tarEntry = tarInputStream.getNextEntry()) != null) { - assertNotNull(tarEntry); - } - } catch (final IOException e) { - fail(Objects.toString(tarEntry), e); - } - })).collect(Collectors.toList()); - final List<Exception> list = new ArrayList<>(); - for (final Future<?> future : tasks) { - try { - future.get(); - } catch (final Exception e) { - list.add(e); - } - } - // check: - if (!list.isEmpty()) { - fail(list.get(0)); - } - // or: - // assertTrue(list.isEmpty(), () -> list.size() + " exceptions: " + list.toString()); - } finally { - executorService.shutdownNow(); - } - } - - /** - * Tests https://issues.apache.org/jira/browse/COMPRESS-666 - * - * A factor of 20 is the default. - */ - @ParameterizedTest - @ValueSource(ints = { 1, 2, 4, 8, 16, 20, 32, 64, 128 }) - public void testCompress666Buffered(final int factor) { - testCompress666(factor, true, "/COMPRESS-666/compress-666.tar.gz"); - } - - /** - * Tests https://issues.apache.org/jira/browse/COMPRESS-666 - * - * A factor of 20 is the default. - */ - @ParameterizedTest - @ValueSource(ints = { 1, 2, 4, 8, 16, 20, 32, 64, 128 }) - public void testCompress666Unbuffered(final int factor) { - testCompress666(factor, false, "/COMPRESS-666/compress-666.tar.gz"); - } } diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java index 349fda86f..9189e28e7 100644 --- a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStreamTest.java @@ -87,43 +87,6 @@ public class GzipCompressorOutputStreamTest { testChineseFileName(EXPECTED_FILE_NAME, EXPECTED_FILE_NAME, StandardCharsets.UTF_8); } - private void testFileName(final String expected, final String sourceFile) throws IOException { - final Path tempSourceFile = Files.createTempFile(sourceFile, sourceFile); - Files.write(tempSourceFile, "<text>Hello World!</text>".getBytes(StandardCharsets.ISO_8859_1)); - final Path targetFile = Files.createTempFile("test", ".gz"); - final GzipParameters parameters = new GzipParameters(); - parameters.setFilename(sourceFile); - assertEquals(parameters.getFilename(), parameters.getFileName()); - parameters.setFileName(sourceFile); - assertEquals(parameters.getFilename(), parameters.getFileName()); - try (OutputStream fos = Files.newOutputStream(targetFile); - GzipCompressorOutputStream gos = new GzipCompressorOutputStream(fos, parameters)) { - Files.copy(tempSourceFile, gos); - } - try (GzipCompressorInputStream gis = new GzipCompressorInputStream(Files.newInputStream(targetFile))) { - assertEquals(expected, gis.getMetaData().getFileName()); - assertEquals(expected, gis.getMetaData().getFilename()); - } - } - - @Test - public void testFileNameAscii() throws IOException { - testFileName("ASCII.xml", "ASCII.xml"); - } - - /** - * Tests COMPRESS-638. Use {@link GzipParameters#setFileNameCharset(Charset)} if you want non-ISO-8859-1 characters. - * - * GZip RFC requires ISO 8859-1 (LATIN-1). - * - * @throws IOException When the test fails. - */ - @Test - public void testFileNameChinesePercentEncoded() throws IOException { - // "Test Chinese name" - testFileName("??????.xml", EXPECTED_FILE_NAME); - } - /** * Tests the gzip extra header containing subfields. * @@ -188,4 +151,41 @@ public class GzipCompressorOutputStreamTest { } } + private void testFileName(final String expected, final String sourceFile) throws IOException { + final Path tempSourceFile = Files.createTempFile(sourceFile, sourceFile); + Files.write(tempSourceFile, "<text>Hello World!</text>".getBytes(StandardCharsets.ISO_8859_1)); + final Path targetFile = Files.createTempFile("test", ".gz"); + final GzipParameters parameters = new GzipParameters(); + parameters.setFilename(sourceFile); + assertEquals(parameters.getFilename(), parameters.getFileName()); + parameters.setFileName(sourceFile); + assertEquals(parameters.getFilename(), parameters.getFileName()); + try (OutputStream fos = Files.newOutputStream(targetFile); + GzipCompressorOutputStream gos = new GzipCompressorOutputStream(fos, parameters)) { + Files.copy(tempSourceFile, gos); + } + try (GzipCompressorInputStream gis = new GzipCompressorInputStream(Files.newInputStream(targetFile))) { + assertEquals(expected, gis.getMetaData().getFileName()); + assertEquals(expected, gis.getMetaData().getFilename()); + } + } + + @Test + public void testFileNameAscii() throws IOException { + testFileName("ASCII.xml", "ASCII.xml"); + } + + /** + * Tests COMPRESS-638. Use {@link GzipParameters#setFileNameCharset(Charset)} if you want non-ISO-8859-1 characters. + * + * GZip RFC requires ISO 8859-1 (LATIN-1). + * + * @throws IOException When the test fails. + */ + @Test + public void testFileNameChinesePercentEncoded() throws IOException { + // "Test Chinese name" + testFileName("??????.xml", EXPECTED_FILE_NAME); + } + }