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.IOException; 022import java.io.InputStream; 023import java.nio.file.Files; 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.File, 040 * java.util.jar.JarOutputStream) 041 */ 042 @Override 043 public void unpack(final File file, final JarOutputStream out) throws IOException { 044 if (file == null || out == null) { 045 throw new IllegalArgumentException("Must specify both input and output streams"); 046 } 047 final int size = (int) file.length(); 048 final int bufferSize = size > 0 && size < DEFAULT_BUFFER_SIZE ? size : DEFAULT_BUFFER_SIZE; 049 try (final InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()), bufferSize)) { 050 unpack(in, out); 051 } 052 } 053 054 /* 055 * (non-Javadoc) 056 * 057 * @see org.apache.commons.compress.java.util.jar.Pack200.Unpacker#unpack(java.io.InputStream, 058 * java.util.jar.JarOutputStream) 059 */ 060 @Override 061 public void unpack(final InputStream in, final JarOutputStream out) throws IOException { 062 if (in == null || out == null) { 063 throw new IllegalArgumentException("Must specify both input and output streams"); 064 } 065 completed(0); 066 try { 067 new Archive(in, out).unpack(); 068 } catch (final Pack200Exception e) { 069 throw new IOException("Failed to unpack Jar:" + e); 070 } 071 completed(1); 072 } 073}