001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.harmony.pack200;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import java.util.jar.JarFile;
022import java.util.jar.JarInputStream;
023
024import org.apache.commons.compress.java.util.jar.Pack200.Packer;
025import org.apache.commons.compress.utils.ParsingUtils;
026
027/**
028 * This class provides the binding between the standard Pack200 interface and the internal interface for (un)packing. As this uses generics for the SortedMap,
029 * 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.
030 */
031public class Pack200PackerAdapter extends Pack200Adapter implements Packer {
032
033    private final PackingOptions options = new PackingOptions();
034
035    @Override
036    protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
037        super.firePropertyChange(propertyName, oldValue, newValue);
038        if (newValue != null && !newValue.equals(oldValue)) {
039            if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) {
040                final String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length());
041                options.addClassAttributeAction(attributeName, (String) newValue);
042            } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) {
043                final String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length());
044                options.addCodeAttributeAction(attributeName, (String) newValue);
045            } else if (propertyName.equals(DEFLATE_HINT)) {
046                options.setDeflateHint((String) newValue);
047            } else if (propertyName.equals(EFFORT)) {
048                options.setEffort(ParsingUtils.parseIntValue((String) newValue));
049            } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) {
050                final String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length());
051                options.addFieldAttributeAction(attributeName, (String) newValue);
052            } else if (propertyName.equals(KEEP_FILE_ORDER)) {
053                options.setKeepFileOrder(Boolean.parseBoolean((String) newValue));
054            } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) {
055                final String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length());
056                options.addMethodAttributeAction(attributeName, (String) newValue);
057            } else if (propertyName.equals(MODIFICATION_TIME)) {
058                options.setModificationTime((String) newValue);
059            } else if (propertyName.startsWith(PASS_FILE_PFX)) {
060                if (oldValue != null && !oldValue.equals("")) {
061                    options.removePassFile((String) oldValue);
062                }
063                options.addPassFile((String) newValue);
064            } else if (propertyName.equals(SEGMENT_LIMIT)) {
065                options.setSegmentLimit(ParsingUtils.parseLongValue((String) newValue));
066            } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) {
067                options.setUnknownAttributeAction((String) newValue);
068            }
069        }
070    }
071
072    @Override
073    public void pack(final JarFile file, final OutputStream out) throws IOException {
074        if (file == null || out == null) {
075            throw new IllegalArgumentException("Must specify both input and output streams");
076        }
077        completed(0);
078        try {
079            new Archive(file, out, options).pack();
080        } catch (final Pack200Exception e) {
081            throw new IOException("Failed to pack Jar:" + e);
082        }
083        completed(1);
084    }
085
086    @Override
087    public void pack(final JarInputStream in, final OutputStream out) throws IOException {
088        if (in == null || out == null) {
089            throw new IllegalArgumentException("Must specify both input and output streams");
090        }
091        completed(0);
092        final PackingOptions options = new PackingOptions();
093
094        try {
095            new Archive(in, out, options).pack();
096        } catch (final Pack200Exception e) {
097            throw new IOException("Failed to pack Jar:" + e);
098        }
099        completed(1);
100        in.close();
101    }
102
103}