View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.pack200;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.jar.JarFile;
24  import java.util.jar.JarInputStream;
25  
26  import org.apache.commons.compress.java.util.jar.Pack200.Packer;
27  import org.apache.commons.compress.utils.ParsingUtils;
28  
29  /**
30   * This class provides the binding between the standard Pack200 interface and the internal interface for (un)packing. As this uses generics for the SortedMap,
31   * this class must be compiled and run on a Java 1.5 system. However, Java 1.5 is not necessary to use the internal libraries for unpacking.
32   */
33  public class Pack200PackerAdapter extends Pack200Adapter implements Packer {
34  
35      private final PackingOptions options = new PackingOptions();
36  
37      @Override
38      protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
39          super.firePropertyChange(propertyName, oldValue, newValue);
40          if (newValue != null && !newValue.equals(oldValue)) {
41              if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) {
42                  final String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length());
43                  options.addClassAttributeAction(attributeName, (String) newValue);
44              } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) {
45                  final String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length());
46                  options.addCodeAttributeAction(attributeName, (String) newValue);
47              } else if (propertyName.equals(DEFLATE_HINT)) {
48                  options.setDeflateHint((String) newValue);
49              } else if (propertyName.equals(EFFORT)) {
50                  options.setEffort(ParsingUtils.parseIntValue((String) newValue));
51              } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) {
52                  final String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length());
53                  options.addFieldAttributeAction(attributeName, (String) newValue);
54              } else if (propertyName.equals(KEEP_FILE_ORDER)) {
55                  options.setKeepFileOrder(Boolean.parseBoolean((String) newValue));
56              } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) {
57                  final String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length());
58                  options.addMethodAttributeAction(attributeName, (String) newValue);
59              } else if (propertyName.equals(MODIFICATION_TIME)) {
60                  options.setModificationTime((String) newValue);
61              } else if (propertyName.startsWith(PASS_FILE_PFX)) {
62                  if (oldValue != null && !oldValue.equals("")) {
63                      options.removePassFile((String) oldValue);
64                  }
65                  options.addPassFile((String) newValue);
66              } else if (propertyName.equals(SEGMENT_LIMIT)) {
67                  options.setSegmentLimit(ParsingUtils.parseLongValue((String) newValue));
68              } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) {
69                  options.setUnknownAttributeAction((String) newValue);
70              }
71          }
72      }
73  
74      @Override
75      public void pack(final JarFile file, final OutputStream out) throws IOException {
76          if (file == null || out == null) {
77              throw new IllegalArgumentException("Must specify both input and output streams");
78          }
79          completed(0);
80          try {
81              new Archive(file, out, options).pack();
82          } catch (final Pack200Exception e) {
83              throw new IOException("Failed to pack Jar:" + e);
84          }
85          completed(1);
86      }
87  
88      @Override
89      public void pack(final JarInputStream in, final OutputStream out) throws IOException {
90          if (in == null || out == null) {
91              throw new IllegalArgumentException("Must specify both input and output streams");
92          }
93          completed(0);
94          final PackingOptions options = new PackingOptions();
95  
96          try {
97              new Archive(in, out, options).pack();
98          } catch (final Pack200Exception e) {
99              throw new IOException("Failed to pack Jar:" + e);
100         }
101         completed(1);
102         in.close();
103     }
104 
105 }