View Javadoc
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  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.jar.JarFile;
22  import java.util.jar.JarInputStream;
23  
24  import org.apache.commons.compress.java.util.jar.Pack200.Packer;
25  import org.apache.commons.compress.utils.ParsingUtils;
26  
27  /**
28   * This class provides the binding between the standard Pack200 interface and the internal interface for (un)packing. As this uses generics for the SortedMap,
29   * 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.
30   */
31  public class Pack200PackerAdapter extends Pack200Adapter implements Packer {
32  
33      private final PackingOptions options = new PackingOptions();
34  
35      @Override
36      protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
37          super.firePropertyChange(propertyName, oldValue, newValue);
38          if (newValue != null && !newValue.equals(oldValue)) {
39              if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) {
40                  final String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length());
41                  options.addClassAttributeAction(attributeName, (String) newValue);
42              } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) {
43                  final String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length());
44                  options.addCodeAttributeAction(attributeName, (String) newValue);
45              } else if (propertyName.equals(DEFLATE_HINT)) {
46                  options.setDeflateHint((String) newValue);
47              } else if (propertyName.equals(EFFORT)) {
48                  options.setEffort(ParsingUtils.parseIntValue((String) newValue));
49              } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) {
50                  final String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length());
51                  options.addFieldAttributeAction(attributeName, (String) newValue);
52              } else if (propertyName.equals(KEEP_FILE_ORDER)) {
53                  options.setKeepFileOrder(Boolean.parseBoolean((String) newValue));
54              } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) {
55                  final String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length());
56                  options.addMethodAttributeAction(attributeName, (String) newValue);
57              } else if (propertyName.equals(MODIFICATION_TIME)) {
58                  options.setModificationTime((String) newValue);
59              } else if (propertyName.startsWith(PASS_FILE_PFX)) {
60                  if (oldValue != null && !oldValue.equals("")) {
61                      options.removePassFile((String) oldValue);
62                  }
63                  options.addPassFile((String) newValue);
64              } else if (propertyName.equals(SEGMENT_LIMIT)) {
65                  options.setSegmentLimit(ParsingUtils.parseLongValue((String) newValue));
66              } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) {
67                  options.setUnknownAttributeAction((String) newValue);
68              }
69          }
70      }
71  
72      @Override
73      public void pack(final JarFile file, final OutputStream out) throws IOException {
74          if (file == null || out == null) {
75              throw new IllegalArgumentException("Must specify both input and output streams");
76          }
77          completed(0);
78          try {
79              new org.apache.commons.compress.harmony.pack200.Archive(file, out, options).pack();
80          } catch (final Pack200Exception e) {
81              throw new IOException("Failed to pack Jar:" + e);
82          }
83          completed(1);
84      }
85  
86      @Override
87      public void pack(final JarInputStream in, final OutputStream out) throws IOException {
88          if (in == null || out == null) {
89              throw new IllegalArgumentException("Must specify both input and output streams");
90          }
91          completed(0);
92          final PackingOptions options = new PackingOptions();
93  
94          try {
95              new org.apache.commons.compress.harmony.pack200.Archive(in, out, options).pack();
96          } catch (final Pack200Exception e) {
97              throw new IOException("Failed to pack Jar:" + e);
98          }
99          completed(1);
100         in.close();
101     }
102 
103 }