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.unpack200; 018 019import java.io.BufferedInputStream; 020import java.io.File; 021import java.io.FileInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.util.jar.JarOutputStream; 025 026import org.apache.commons.compress.harmony.pack200.Pack200Adapter; 027import org.apache.commons.compress.harmony.pack200.Pack200Exception; 028import org.apache.commons.compress.java.util.jar.Pack200.Unpacker; 029 030/** 031 * This class provides the binding between the standard Pack200 interface and the internal interface for (un)packing. As 032 * this uses generics for the SortedMap, this class must be compiled and run on a Java 1.5 system. However, Java 1.5 is 033 * not necessary to use the internal libraries for unpacking. 034 */ 035public class Pack200UnpackerAdapter extends Pack200Adapter implements Unpacker { 036 /* 037 * (non-Javadoc) 038 * 039 * @see org.apache.commons.compress.java.util.jar.Pack200.Unpacker#unpack(java.io.InputStream, 040 * java.util.jar.JarOutputStream) 041 */ 042 @Override 043 public void unpack(final InputStream in, final JarOutputStream out) throws IOException { 044 if (in == null || out == null) { 045 throw new IllegalArgumentException("Must specify both input and output streams"); 046 } 047 completed(0); 048 try { 049 new Archive(in, out).unpack(); 050 } catch (final Pack200Exception e) { 051 throw new IOException("Failed to unpack Jar:" + String.valueOf(e)); 052 } 053 completed(1); 054 in.close(); 055 } 056 057 /* 058 * (non-Javadoc) 059 * 060 * @see org.apache.commons.compress.java.util.jar.Pack200.Unpacker#unpack(java.io.File, 061 * java.util.jar.JarOutputStream) 062 */ 063 @Override 064 public void unpack(final File file, final JarOutputStream out) throws IOException { 065 if (file == null || out == null) { 066 throw new IllegalArgumentException("Must specify both input and output streams"); 067 } 068 final int size = (int) file.length(); 069 final int bufferSize = (size > 0 && size < DEFAULT_BUFFER_SIZE ? size : DEFAULT_BUFFER_SIZE); 070 final InputStream in = new BufferedInputStream(new FileInputStream(file), bufferSize); 071 unpack(in, out); 072 } 073}