Pack200PackerAdapter.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.IOException;
  19. import java.io.OutputStream;
  20. import java.util.jar.JarFile;
  21. import java.util.jar.JarInputStream;

  22. import org.apache.commons.compress.java.util.jar.Pack200.Packer;
  23. import org.apache.commons.compress.utils.ParsingUtils;

  24. /**
  25.  * This class provides the binding between the standard Pack200 interface and the internal interface for (un)packing. As this uses generics for the SortedMap,
  26.  * 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.
  27.  */
  28. public class Pack200PackerAdapter extends Pack200Adapter implements Packer {

  29.     private final PackingOptions options = new PackingOptions();

  30.     @Override
  31.     protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
  32.         super.firePropertyChange(propertyName, oldValue, newValue);
  33.         if (newValue != null && !newValue.equals(oldValue)) {
  34.             if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) {
  35.                 final String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length());
  36.                 options.addClassAttributeAction(attributeName, (String) newValue);
  37.             } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) {
  38.                 final String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length());
  39.                 options.addCodeAttributeAction(attributeName, (String) newValue);
  40.             } else if (propertyName.equals(DEFLATE_HINT)) {
  41.                 options.setDeflateHint((String) newValue);
  42.             } else if (propertyName.equals(EFFORT)) {
  43.                 options.setEffort(ParsingUtils.parseIntValue((String) newValue));
  44.             } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) {
  45.                 final String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length());
  46.                 options.addFieldAttributeAction(attributeName, (String) newValue);
  47.             } else if (propertyName.equals(KEEP_FILE_ORDER)) {
  48.                 options.setKeepFileOrder(Boolean.parseBoolean((String) newValue));
  49.             } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) {
  50.                 final String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length());
  51.                 options.addMethodAttributeAction(attributeName, (String) newValue);
  52.             } else if (propertyName.equals(MODIFICATION_TIME)) {
  53.                 options.setModificationTime((String) newValue);
  54.             } else if (propertyName.startsWith(PASS_FILE_PFX)) {
  55.                 if (oldValue != null && !oldValue.equals("")) {
  56.                     options.removePassFile((String) oldValue);
  57.                 }
  58.                 options.addPassFile((String) newValue);
  59.             } else if (propertyName.equals(SEGMENT_LIMIT)) {
  60.                 options.setSegmentLimit(ParsingUtils.parseLongValue((String) newValue));
  61.             } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) {
  62.                 options.setUnknownAttributeAction((String) newValue);
  63.             }
  64.         }
  65.     }

  66.     @Override
  67.     public void pack(final JarFile file, final OutputStream out) throws IOException {
  68.         if (file == null || out == null) {
  69.             throw new IllegalArgumentException("Must specify both input and output streams");
  70.         }
  71.         completed(0);
  72.         try {
  73.             new Archive(file, out, options).pack();
  74.         } catch (final Pack200Exception e) {
  75.             throw new IOException("Failed to pack Jar:" + e);
  76.         }
  77.         completed(1);
  78.     }

  79.     @Override
  80.     public void pack(final JarInputStream in, final OutputStream out) throws IOException {
  81.         if (in == null || out == null) {
  82.             throw new IllegalArgumentException("Must specify both input and output streams");
  83.         }
  84.         completed(0);
  85.         final PackingOptions options = new PackingOptions();

  86.         try {
  87.             new Archive(in, out, options).pack();
  88.         } catch (final Pack200Exception e) {
  89.             throw new IOException("Failed to pack Jar:" + e);
  90.         }
  91.         completed(1);
  92.         in.close();
  93.     }

  94. }