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