Archive.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.harmony.pack200;

  18. import java.io.BufferedOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.jar.JarEntry;
  24. import java.util.jar.JarFile;
  25. import java.util.jar.JarInputStream;
  26. import java.util.zip.GZIPOutputStream;
  27. import java.util.zip.ZipEntry;

  28. /**
  29.  * Archive is the main entry point to pack200 and represents a packed archive. An archive is constructed with either a JarInputStream and an output stream or a
  30.  * JarFile as input and an OutputStream. Options can be set, then {@code pack()} is called, to pack the Jar file into a pack200 archive.
  31.  */
  32. public class Archive {

  33.     static class PackingFile {

  34.         private final String name;
  35.         private byte[] contents;
  36.         private final long modtime;
  37.         private final boolean deflateHint;
  38.         private final boolean isDirectory;

  39.         PackingFile(final byte[] bytes, final JarEntry jarEntry) {
  40.             name = jarEntry.getName();
  41.             contents = bytes;
  42.             modtime = jarEntry.getTime();
  43.             deflateHint = jarEntry.getMethod() == ZipEntry.DEFLATED;
  44.             isDirectory = jarEntry.isDirectory();
  45.         }

  46.         PackingFile(final String name, final byte[] contents, final long modtime) {
  47.             this.name = name;
  48.             this.contents = contents;
  49.             this.modtime = modtime;
  50.             deflateHint = false;
  51.             isDirectory = false;
  52.         }

  53.         public byte[] getContents() {
  54.             return contents;
  55.         }

  56.         public long getModtime() {
  57.             return modtime;
  58.         }

  59.         public String getName() {
  60.             return name;
  61.         }

  62.         public boolean isDefalteHint() {
  63.             return deflateHint;
  64.         }

  65.         public boolean isDirectory() {
  66.             return isDirectory;
  67.         }

  68.         public void setContents(final byte[] contents) {
  69.             this.contents = contents;
  70.         }

  71.         @Override
  72.         public String toString() {
  73.             return name;
  74.         }
  75.     }

  76.     static class SegmentUnit {

  77.         private final List<Pack200ClassReader> classList;

  78.         private final List<PackingFile> fileList;

  79.         private int byteAmount;

  80.         private int packedByteAmount;

  81.         SegmentUnit(final List<Pack200ClassReader> classes, final List<PackingFile> files) {
  82.             classList = classes;
  83.             fileList = files;
  84.             byteAmount = 0;
  85.             // Calculate the amount of bytes in classes and files before packing
  86.             byteAmount += classList.stream().mapToInt(element -> element.b.length).sum();
  87.             byteAmount += fileList.stream().mapToInt(element -> element.contents.length).sum();
  88.         }

  89.         public void addPackedByteAmount(final int amount) {
  90.             packedByteAmount += amount;
  91.         }

  92.         public int classListSize() {
  93.             return classList.size();
  94.         }

  95.         public int fileListSize() {
  96.             return fileList.size();
  97.         }

  98.         public int getByteAmount() {
  99.             return byteAmount;
  100.         }

  101.         public List<Pack200ClassReader> getClassList() {
  102.             return classList;
  103.         }

  104.         public List<PackingFile> getFileList() {
  105.             return fileList;
  106.         }

  107.         public int getPackedByteAmount() {
  108.             return packedByteAmount;
  109.         }
  110.     }

  111.     private static final byte[] EMPTY_BYTE_ARRAY = {};

  112.     private final JarInputStream jarInputStream;
  113.     private final OutputStream outputStream;
  114.     private JarFile jarFile;

  115.     private long currentSegmentSize;

  116.     private final PackingOptions options;

  117.     /**
  118.      * Creates an Archive with the given input file and a stream for the output
  119.      *
  120.      * @param jarFile      the input file
  121.      * @param outputStream TODO
  122.      * @param options      packing options (if null then defaults are used)
  123.      * @throws IOException If an I/O error occurs.
  124.      */
  125.     public Archive(final JarFile jarFile, OutputStream outputStream, PackingOptions options) throws IOException {
  126.         if (options == null) { // use all defaults
  127.             options = new PackingOptions();
  128.         }
  129.         this.options = options;
  130.         if (options.isGzip()) {
  131.             outputStream = new GZIPOutputStream(outputStream);
  132.         }
  133.         this.outputStream = new BufferedOutputStream(outputStream);
  134.         this.jarFile = jarFile;
  135.         jarInputStream = null;
  136.         PackingUtils.config(options);
  137.     }

  138.     /**
  139.      * Creates an Archive with streams for the input and output.
  140.      *
  141.      * @param inputStream  TODO
  142.      * @param outputStream TODO
  143.      * @param options      packing options (if null then defaults are used)
  144.      * @throws IOException If an I/O error occurs.
  145.      */
  146.     public Archive(final JarInputStream inputStream, OutputStream outputStream, PackingOptions options) throws IOException {
  147.         jarInputStream = inputStream;
  148.         if (options == null) {
  149.             // use all defaults
  150.             options = new PackingOptions();
  151.         }
  152.         this.options = options;
  153.         if (options.isGzip()) {
  154.             outputStream = new GZIPOutputStream(outputStream);
  155.         }
  156.         this.outputStream = new BufferedOutputStream(outputStream);
  157.         PackingUtils.config(options);
  158.     }

  159.     private boolean addJarEntry(final PackingFile packingFile, final List<Pack200ClassReader> javaClasses, final List<PackingFile> files) {
  160.         final long segmentLimit = options.getSegmentLimit();
  161.         if (segmentLimit != -1 && segmentLimit != 0) {
  162.             // -1 is a special case where only one segment is created and
  163.             // 0 is a special case where one segment is created for each file
  164.             // except for files in "META-INF"
  165.             final long packedSize = estimateSize(packingFile);
  166.             if (packedSize + currentSegmentSize > segmentLimit && currentSegmentSize > 0) {
  167.                 // don't add this JarEntry to the current segment
  168.                 return false;
  169.             }
  170.             // do add this JarEntry
  171.             currentSegmentSize += packedSize;
  172.         }

  173.         final String name = packingFile.getName();
  174.         if (name.endsWith(".class") && !options.isPassFile(name)) {
  175.             final Pack200ClassReader classParser = new Pack200ClassReader(packingFile.contents);
  176.             classParser.setFileName(name);
  177.             javaClasses.add(classParser);
  178.             packingFile.contents = EMPTY_BYTE_ARRAY;
  179.         }
  180.         files.add(packingFile);
  181.         return true;
  182.     }

  183.     private void doNormalPack() throws IOException, Pack200Exception {
  184.         PackingUtils.log("Start to perform a normal packing");
  185.         List<PackingFile> packingFileList;
  186.         if (jarInputStream != null) {
  187.             packingFileList = PackingUtils.getPackingFileListFromJar(jarInputStream, options.isKeepFileOrder());
  188.         } else {
  189.             packingFileList = PackingUtils.getPackingFileListFromJar(jarFile, options.isKeepFileOrder());
  190.         }

  191.         final List<SegmentUnit> segmentUnitList = splitIntoSegments(packingFileList);
  192.         int previousByteAmount = 0;
  193.         int packedByteAmount = 0;

  194.         final int segmentSize = segmentUnitList.size();
  195.         SegmentUnit segmentUnit;
  196.         for (int index = 0; index < segmentSize; index++) {
  197.             segmentUnit = segmentUnitList.get(index);
  198.             new Segment().pack(segmentUnit, outputStream, options);
  199.             previousByteAmount += segmentUnit.getByteAmount();
  200.             packedByteAmount += segmentUnit.getPackedByteAmount();
  201.         }

  202.         PackingUtils.log("Total: Packed " + previousByteAmount + " input bytes of " + packingFileList.size() + " files into " + packedByteAmount + " bytes in "
  203.                 + segmentSize + " segments");

  204.         outputStream.close();
  205.     }

  206.     private void doZeroEffortPack() throws IOException {
  207.         PackingUtils.log("Start to perform a zero-effort packing");
  208.         if (jarInputStream != null) {
  209.             PackingUtils.copyThroughJar(jarInputStream, outputStream);
  210.         } else {
  211.             PackingUtils.copyThroughJar(jarFile, outputStream);
  212.         }
  213.     }

  214.     private long estimateSize(final PackingFile packingFile) {
  215.         // The heuristic used here is for compatibility with the RI and should
  216.         // not be changed
  217.         final String name = packingFile.getName();
  218.         if (name.startsWith("META-INF") || name.startsWith("/META-INF")) {
  219.             return 0;
  220.         }
  221.         long fileSize = packingFile.contents.length;
  222.         if (fileSize < 0) {
  223.             fileSize = 0;
  224.         }
  225.         return name.length() + fileSize + 5;
  226.     }

  227.     /**
  228.      * Packs the archive.
  229.      *
  230.      * @throws Pack200Exception TODO
  231.      * @throws IOException      If an I/O error occurs.
  232.      */
  233.     public void pack() throws Pack200Exception, IOException {
  234.         if (0 == options.getEffort()) {
  235.             doZeroEffortPack();
  236.         } else {
  237.             doNormalPack();
  238.         }
  239.     }

  240.     private List<SegmentUnit> splitIntoSegments(final List<PackingFile> packingFileList) {
  241.         final List<SegmentUnit> segmentUnitList = new ArrayList<>();
  242.         List<Pack200ClassReader> classes = new ArrayList<>();
  243.         List<PackingFile> files = new ArrayList<>();
  244.         final long segmentLimit = options.getSegmentLimit();

  245.         final int size = packingFileList.size();
  246.         PackingFile packingFile;
  247.         for (int index = 0; index < size; index++) {
  248.             packingFile = packingFileList.get(index);
  249.             if (!addJarEntry(packingFile, classes, files)) {
  250.                 // not added because segment has reached maximum size
  251.                 segmentUnitList.add(new SegmentUnit(classes, files));
  252.                 classes = new ArrayList<>();
  253.                 files = new ArrayList<>();
  254.                 currentSegmentSize = 0;
  255.                 // add the jar to a new segment
  256.                 addJarEntry(packingFile, classes, files);
  257.                 // ignore the size of first entry for compatibility with RI
  258.                 currentSegmentSize = 0;
  259.             } else if (segmentLimit == 0 && estimateSize(packingFile) > 0) {
  260.                 // create a new segment for each class unless size is 0
  261.                 segmentUnitList.add(new SegmentUnit(classes, files));
  262.                 classes = new ArrayList<>();
  263.                 files = new ArrayList<>();
  264.             }
  265.         }
  266.         // Change for Apache Commons Compress based on Apache Harmony.
  267.         // if (classes.size() > 0 && files.size() > 0) {
  268.         if (classes.size() > 0 || files.size() > 0) {
  269.             segmentUnitList.add(new SegmentUnit(classes, files));
  270.         }
  271.         return segmentUnitList;
  272.     }

  273. }