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; 025 026/** 027 * This class provides the binding between the standard Pack200 interface and the internal interface for (un)packing. As 028 * this uses generics for the SortedMap, this class must be compiled and run on a Java 1.5 system. However, Java 1.5 is 029 * 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 public void pack(final JarFile file, final OutputStream out) throws IOException { 037 if (file == null || out == null) { 038 throw new IllegalArgumentException("Must specify both input and output streams"); 039 } 040 completed(0); 041 try { 042 new org.apache.commons.compress.harmony.pack200.Archive(file, out, options).pack(); 043 } catch (final Pack200Exception e) { 044 throw new IOException("Failed to pack Jar:" + String.valueOf(e)); 045 } 046 completed(1); 047 } 048 049 @Override 050 public void pack(final JarInputStream in, final OutputStream out) throws IOException { 051 if (in == null || out == null) { 052 throw new IllegalArgumentException("Must specify both input and output streams"); 053 } 054 completed(0); 055 final PackingOptions options = new PackingOptions(); 056 057 try { 058 new org.apache.commons.compress.harmony.pack200.Archive(in, out, options).pack(); 059 } catch (final Pack200Exception e) { 060 throw new IOException("Failed to pack Jar:" + String.valueOf(e)); 061 } 062 completed(1); 063 in.close(); 064 } 065 066 @Override 067 protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) { 068 super.firePropertyChange(propertyName, oldValue, newValue); 069 if (newValue != null && !newValue.equals(oldValue)) { 070 if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) { 071 final String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length()); 072 options.addClassAttributeAction(attributeName, (String) newValue); 073 } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) { 074 final String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length()); 075 options.addCodeAttributeAction(attributeName, (String) newValue); 076 } else if (propertyName.equals(DEFLATE_HINT)) { 077 options.setDeflateHint((String) newValue); 078 } else if (propertyName.equals(EFFORT)) { 079 options.setEffort(Integer.parseInt((String) newValue)); 080 } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) { 081 final String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length()); 082 options.addFieldAttributeAction(attributeName, (String) newValue); 083 } else if (propertyName.equals(KEEP_FILE_ORDER)) { 084 options.setKeepFileOrder(Boolean.parseBoolean((String) newValue)); 085 } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) { 086 final String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length()); 087 options.addMethodAttributeAction(attributeName, (String) newValue); 088 } else if (propertyName.equals(MODIFICATION_TIME)) { 089 options.setModificationTime((String) newValue); 090 } else if (propertyName.startsWith(PASS_FILE_PFX)) { 091 if (oldValue != null && !oldValue.equals("")) { 092 options.removePassFile((String) oldValue); 093 } 094 options.addPassFile((String) newValue); 095 } else if (propertyName.equals(SEGMENT_LIMIT)) { 096 options.setSegmentLimit(Long.parseLong((String) newValue)); 097 } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) { 098 options.setUnknownAttributeAction((String) newValue); 099 } 100 } 101 } 102 103}