|
1 | 1 | /* |
2 | | - * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
60 | 60 | import java.util.regex.PatternSyntaxException; |
61 | 61 | import java.util.stream.Collectors; |
62 | 62 | import java.util.stream.Stream; |
| 63 | +import java.util.zip.Deflater; |
63 | 64 | import java.util.zip.ZipEntry; |
64 | 65 | import java.util.zip.ZipException; |
65 | 66 | import java.util.zip.ZipFile; |
@@ -167,6 +168,7 @@ static class Options { |
167 | 168 | List<PathMatcher> excludes; |
168 | 169 | Path extractDir; |
169 | 170 | LocalDateTime date; |
| 171 | + int compressLevel; |
170 | 172 | } |
171 | 173 |
|
172 | 174 | // Valid --date range |
@@ -438,7 +440,7 @@ private boolean create() throws IOException { |
438 | 440 | Path target = options.jmodFile; |
439 | 441 | Path tempTarget = jmodTempFilePath(target); |
440 | 442 | try { |
441 | | - try (JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget, options.date)) { |
| 443 | + try (JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget, options.date, options.compressLevel)) { |
442 | 444 | jmod.write(jos); |
443 | 445 | } |
444 | 446 | Files.move(tempTarget, target); |
@@ -1024,7 +1026,7 @@ private void updateJmodFile(Path target, Path tempTarget, |
1024 | 1026 | { |
1025 | 1027 |
|
1026 | 1028 | try (JmodFile jf = new JmodFile(target); |
1027 | | - JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget, options.date)) |
| 1029 | + JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget, options.date, options.compressLevel)) |
1028 | 1030 | { |
1029 | 1031 | jf.stream().forEach(e -> { |
1030 | 1032 | try (InputStream in = jf.getInputStream(e.section(), e.name())) { |
@@ -1179,6 +1181,33 @@ public LocalDateTime convert(String value) { |
1179 | 1181 | @Override public String valuePattern() { return "date"; } |
1180 | 1182 | } |
1181 | 1183 |
|
| 1184 | + static class CompLevelConverter implements ValueConverter<Integer> { |
| 1185 | + @Override |
| 1186 | + public Integer convert(String value) { |
| 1187 | + int idx = value.indexOf("-"); |
| 1188 | + int lastIdx = value.lastIndexOf("-"); |
| 1189 | + if (idx == -1 || idx != lastIdx) { |
| 1190 | + throw new CommandException("err.compress.incorrect", value); |
| 1191 | + } |
| 1192 | + if (!value.substring(0, idx).equals("zip")) { |
| 1193 | + throw new CommandException("err.compress.incorrect", value); |
| 1194 | + } |
| 1195 | + try { |
| 1196 | + int level = Integer.parseInt(value.substring(idx + 1)); |
| 1197 | + if (level < 0 || level > 9) { |
| 1198 | + throw new CommandException("err.compress.incorrect", value); |
| 1199 | + } |
| 1200 | + return level; |
| 1201 | + } catch (NumberFormatException x) { |
| 1202 | + throw new CommandException("err.compress.incorrect", value); |
| 1203 | + } |
| 1204 | + } |
| 1205 | + |
| 1206 | + @Override public Class<Integer> valueType() { return Integer.class; } |
| 1207 | + |
| 1208 | + @Override public String valuePattern() { return "compress"; } |
| 1209 | + } |
| 1210 | + |
1182 | 1211 | static class WarnIfResolvedReasonConverter |
1183 | 1212 | implements ValueConverter<ModuleResolution> |
1184 | 1213 | { |
@@ -1419,6 +1448,11 @@ private void handleOptions(String[] args) { |
1419 | 1448 | .withRequiredArg() |
1420 | 1449 | .withValuesConvertedBy(new DateConverter()); |
1421 | 1450 |
|
| 1451 | + OptionSpec<Integer> compress |
| 1452 | + = parser.accepts("compress", getMessage("main.opt.compress")) |
| 1453 | + .withRequiredArg() |
| 1454 | + .withValuesConvertedBy(new CompLevelConverter()); |
| 1455 | + |
1422 | 1456 | NonOptionArgumentSpec<String> nonOptions |
1423 | 1457 | = parser.nonOptions(); |
1424 | 1458 |
|
@@ -1488,6 +1522,17 @@ private void handleOptions(String[] args) { |
1488 | 1522 | throw new CommandException("err.modulepath.must.be.specified") |
1489 | 1523 | .showUsage(true); |
1490 | 1524 | } |
| 1525 | + if (opts.has(compress)) { |
| 1526 | + if (!options.mode.equals(Mode.CREATE)) { |
| 1527 | + throw new CommandException("err.compress.wrong.mode") |
| 1528 | + .showUsage(true); |
| 1529 | + } |
| 1530 | + options.compressLevel = getLastElement(opts.valuesOf(compress)); |
| 1531 | + } else { |
| 1532 | + // Default to the default from zlib. Hard-coded here to avoid accidental |
| 1533 | + // compression level change if zlib ever changes the default. |
| 1534 | + options.compressLevel = 6; |
| 1535 | + } |
1491 | 1536 |
|
1492 | 1537 | if (options.mode.equals(Mode.HASH)) { |
1493 | 1538 | if (options.moduleFinder == null || options.modulesToHash == null) |
|
0 commit comments