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 b582dadc697a9eecf8f846e00909b022d3f8692b Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Nov 6 11:15:13 2024 -0500 Add GzipParameters.OS, setOS(OS), getOS() --- src/changes/changes.xml | 1 + .../compress/compressors/gzip/GzipParameters.java | 273 ++++++++++++++++++++- .../commons/compress/compressors/GZipTest.java | 6 + 3 files changed, 277 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6f55dcb4b..089fdadbf 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,7 @@ The <action> type attribute can be add,update,fix,remove. <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.getModificationInstant().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.setModificationInstant(Instant).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.OS, setOS(OS), getOS().</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 72 to 78 #563, #567, #574, #582, #587, #595.</action> <action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump com.github.luben:zstd-jni from 1.5.6-4 to 1.5.6-7 #565, #578, #601.</action> diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java index c7a98e558..f1d4fa8bf 100644 --- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java +++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java @@ -33,6 +33,248 @@ import java.util.zip.Deflater; */ public class GzipParameters { + /** + * The OS type. + * <ul> + * <li>0 - FAT filesystem (MS-DOS, OS/2, NT/Win32)</li> + * <li>1 - Amiga</li> + * <li>2 - VMS (or OpenVMS)</li> + * <li>3 - Unix</li> + * <li>4 - VM/CMS</li> + * <li>5 - Atari TOS</li> + * <li>6 - HPFS filesystem (OS/2, NT)</li> + * <li>7 - Macintosh</li> + * <li>8 - Z-System</li> + * <li>9 - CP/M</li> + * <li>10 - TOPS-20</li> + * <li>11 - NTFS filesystem (NT)</li> + * <li>12 - QDOS</li> + * <li>13 - Acorn RISCOS</li> + * <li>255 - unknown</li> + * </ul> + * + * @see <a href="https://datatracker.ietf.org/doc/html/rfc1952#page-7">RFC 1952: GZIP File Format Specification - OS (Operating System)</a> + * @since 1.28.0 + */ + public enum OS { + + // @formatter:off + /** + * 0: FAT filesystem (MS-DOS, OS/2, NT/Win32). + */ + FAT(OS_FAT), + + /** + * 1: Amiga. + */ + AMIGA(OS_AMIGA), + + /** + * 2: VMS (or OpenVMS). + */ + VMS(OS_VMS), + + /** + * 3: Unix. + */ + UNIX(OS_UNIX), + + /** + * 4: VM/CMS. + */ + VM_CMS(OS_VM_CMS), + + /** + * 5: Atari TOS. + */ + ATARI_TOS(OS_ATARI_TOS), + + /** + * 6: HPFS filesystem (OS/2, NT). + */ + HPFS(OS_HPFS), + + /** + * 7: Macintosh. + */ + MACINTOSH(OS_MACINTOSH), + + /** + * 8: Z-System. + */ + Z_SYSTEM(OS_Z_SYSTEM), + + /** + * 9: CP/M. + */ + CPM(OS_CPM), + + /** + * 10: TOPS-20. + */ + TOPS_20(OS_TOPS_20), + + /** + * 11: NTFS filesystem (NT). + */ + NTFS(OS_NTFS), + + /** + * 12: QDOS. + */ + QDOS(OS_QDOS), + + /** + * 13: Acorn RISCOS. + */ + ACORN_RISCOS(OS_ACORN_RISCOS), + + /** + * 255: unknown. + */ + UNKNOWN(OS_UNKNOWN); + // @formatter:on + + /** + * Gets the {@link OS} matching the given code. + * + * @param code an OS or {@link #UNKNOWN} for no match. + * @return a {@link OS}. + */ + public static OS from(final int code) { + switch (code) { + case OS_ACORN_RISCOS: + return ACORN_RISCOS; + case OS_AMIGA: + return AMIGA; + case OS_ATARI_TOS: + return ATARI_TOS; + case OS_CPM: + return CPM; + case OS_FAT: + return FAT; + case OS_HPFS: + return HPFS; + case OS_MACINTOSH: + return MACINTOSH; + case OS_NTFS: + return NTFS; + case OS_QDOS: + return QDOS; + case OS_TOPS_20: + return TOPS_20; + case OS_UNIX: + return UNIX; + case OS_UNKNOWN: + return UNKNOWN; + case OS_VM_CMS: + return VM_CMS; + case OS_VMS: + return VMS; + case OS_Z_SYSTEM: + return Z_SYSTEM; + default: + return UNKNOWN; + } + } + + private final int type; + + /** + * Constructs a new instance. + * + * @param type the OS type. + */ + OS(final int type) { + this.type = type; + } + + /** + * Gets the OS type. + * + * @return the OS type. + */ + public int type() { + return type; + } + + } + + /** + * 0: FAT. + */ + private static final int OS_FAT = 0; + + /** + * 1: Amiga. + */ + private static final int OS_AMIGA = 1; + + /** + * 2: VMS (or OpenVMS). + */ + private static final int OS_VMS = 2; + + /** + * 3: Unix. + */ + private static final int OS_UNIX = 3; + + /** + * 4: VM/CMS. + */ + private static final int OS_VM_CMS = 4; + + /** + * 5: Atari TOS. + */ + private static final int OS_ATARI_TOS = 5; + + /** + * 6: HPFS filesystem (OS/2, NT). + */ + private static final int OS_HPFS = 6; + + /** + * 7: Macintosh. + */ + private static final int OS_MACINTOSH = 7; + + /** + * 8: Z-System. + */ + private static final int OS_Z_SYSTEM = 8; + + /** + * 9: CP/M. + */ + private static final int OS_CPM = 9; + + /** + * 10: TOPS-20. + */ + private static final int OS_TOPS_20 = 10; + + /** + * 11: NTFS filesystem (NT). + */ + private static final int OS_NTFS = 11; + + /** + * 12: QDOS. + */ + private static final int OS_QDOS = 12; + + /** + * 13: Acorn RISCOS. + */ + private static final int OS_ACORN_RISCOS = 13; + + /** + * 255: unknown. + */ + private static final int OS_UNKNOWN = 255; + private int compressionLevel = Deflater.DEFAULT_COMPRESSION; /** @@ -46,7 +288,7 @@ public class GzipParameters { private long modificationTime; private String fileName; private String comment; - private int operatingSystem = 255; // Unknown OS by default + private OS operatingSystem = OS.UNKNOWN; // Unknown OS by default private int bufferSize = 512; private int deflateStrategy = Deflater.DEFAULT_STRATEGY; @@ -127,14 +369,29 @@ public class GzipParameters { return modificationTime; } + /** + * Gets the OS code type. + * + * @return the OS code type. + */ public int getOperatingSystem() { + return operatingSystem.type; + } + + /** + * Gets the OS type. + * + * @return the OS type. + * @since 1.28.0 + */ + public OS getOS() { return operatingSystem; } /** * Sets size of the buffer used to retrieve compressed data from {@link Deflater} and write to underlying {@link OutputStream}. * - * @param bufferSize the bufferSize to set. Must be a positive value. + * @param bufferSize the bufferSize to set. Must be a positive type. * @since 1.21 */ public void setBufferSize(final int bufferSize) { @@ -242,6 +499,16 @@ public class GzipParameters { * @param operatingSystem the code of the operating system */ public void setOperatingSystem(final int operatingSystem) { - this.operatingSystem = operatingSystem; + this.operatingSystem = OS.from(operatingSystem); + } + + /** + * Sets the operating system on which the compression took place. + * + * @param os operating system, null maps to {@link OS#UNKNOWN}. + * @since 1.28.0 + */ + public void setOS(final OS os) { + this.operatingSystem = os != null ? os : OS.UNKNOWN; } } diff --git a/src/test/java/org/apache/commons/compress/compressors/GZipTest.java b/src/test/java/org/apache/commons/compress/compressors/GZipTest.java index 15aa24178..5d8ccbea5 100644 --- a/src/test/java/org/apache/commons/compress/compressors/GZipTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/GZipTest.java @@ -207,7 +207,12 @@ public final class GZipTest extends AbstractTest { parameters.setModificationInstant(MTIME); assertEquals(MTIME.getEpochSecond(), parameters.getModificationTime()); assertEquals(MTIME, parameters.getModificationInstant()); + parameters.setOS(GzipParameters.OS.Z_SYSTEM); + assertEquals(GzipParameters.OS.Z_SYSTEM, parameters.getOS()); + parameters.setOS(null); + assertEquals(GzipParameters.OS.UNKNOWN, parameters.getOS()); parameters.setOperatingSystem(13); + assertEquals(GzipParameters.OS.ACORN_RISCOS, parameters.getOS()); parameters.setFilename("test3.xml"); assertEquals(parameters.getFilename(), parameters.getFileName()); parameters.setFileName("test3.xml"); @@ -223,6 +228,7 @@ public final class GZipTest extends AbstractTest { assertEquals(Deflater.BEST_COMPRESSION, readParams.getCompressionLevel()); assertEquals(123456000, readParams.getModificationTime()); assertEquals(13, readParams.getOperatingSystem()); + assertEquals(GzipParameters.OS.ACORN_RISCOS, readParams.getOS()); assertEquals("test3.xml", readParams.getFileName()); assertEquals("test3.xml", readParams.getFilename()); assertEquals("Umlaute möglich?", readParams.getComment());