    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.BufferedOutputStream;
    021import java.io.File;
    022import java.io.FileInputStream;
    023import java.io.FileNotFoundException;
    024import java.io.FileOutputStream;
    025import java.io.IOException;
    026import java.io.InputStream;
    027import java.io.OutputStream;
    028import java.util.jar.JarEntry;
    029import java.util.jar.JarInputStream;
    030import java.util.jar.JarOutputStream;
    031import java.util.zip.GZIPInputStream;
    032
    033import org.apache.commons.compress.harmony.pack200.Pack200Exception;
    034
    035/**
    036 * Archive is the main entry point to unpack200. An archive is constructed with either two file names, a pack file and
    037 * an output file name or an input stream and an output streams. Then <code>unpack()</code> is called, to unpack the
    038 * pack200 archive.
    039 */
    040public class Archive {
    041
    042    private InputStream inputStream;
    043
    044    private final JarOutputStream outputStream;
    045
    046    private boolean removePackFile;
    047
    048    private int logLevel = Segment.LOG_LEVEL_STANDARD;
    049
    050    private FileOutputStream logFile;
    051
    052    private boolean overrideDeflateHint;
    053
    054    private boolean deflateHint;
    055
    056    private String inputFileName;
    057
    058    private String outputFileName;
    059
    060    /**
    061     * Creates an Archive with the given input and output file names.
    062     *
    063     * @param inputFile TODO
    064     * @param outputFile TODO
    065     * @throws FileNotFoundException if the input file does not exist
    066     * @throws FileNotFoundException TODO
    067     * @throws IOException TODO
    068     */
    069    public Archive(final String inputFile, final String outputFile) throws FileNotFoundException, IOException {
    070        this.inputFileName = inputFile;
    071        this.outputFileName = outputFile;
    072        inputStream = new FileInputStream(inputFile);
    073        outputStream = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
    074    }
    075
    076    /**
    077     * Creates an Archive with streams for the input and output files. Note: If you use this method then calling
    078     * {@link #setRemovePackFile(boolean)} will have no effect.
    079     *
    080     * @param inputStream TODO
    081     * @param outputStream TODO
    082     * @throws IOException TODO
    083     */
    084    public Archive(final InputStream inputStream, final JarOutputStream outputStream) throws IOException {
    085        this.inputStream = inputStream;
    086        this.outputStream = outputStream;
    087    }
    088
    089    /**
    090     * Unpacks the Archive from the input file to the output file
    091     *
    092     * @throws Pack200Exception TODO
    093     * @throws IOException TODO
    094     */
    095    public void unpack() throws Pack200Exception, IOException {
    096        outputStream.setComment("PACK200");
    097        try {
    098            if (!inputStream.markSupported()) {
    099                inputStream = new BufferedInputStream(inputStream);
    100                if (!inputStream.markSupported()) {
    101                    throw new IllegalStateException();
    102                }
    103            }
    104            inputStream.mark(2);
    105            if (((inputStream.read() & 0xFF) | (inputStream.read() & 0xFF) << 8) == GZIPInputStream.GZIP_MAGIC) {
    106                inputStream.reset();
    107                inputStream = new BufferedInputStream(new GZIPInputStream(inputStream));
    108            } else {
    109                inputStream.reset();
    110            }
    111            inputStream.mark(4);
    112            final int[] magic = {0xCA, 0xFE, 0xD0, 0x0D}; // Magic word for
    113            // pack200
    114            final int word[] = new int[4];
    115            for (int i = 0; i < word.length; i++) {
    116                word[i] = inputStream.read();
    117            }
    118            boolean compressedWithE0 = false;
    119            for (int m = 0; m < magic.length; m++) {
    120                if (word[m] != magic[m]) {
    121                    compressedWithE0 = true;
    122                }
    123            }
    124            inputStream.reset();
    125            if (compressedWithE0) { // The original Jar was not packed, so just
    126                // copy it across
    127                final JarInputStream jarInputStream = new JarInputStream(inputStream);
    128                JarEntry jarEntry;
    129                while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
    130                    outputStream.putNextEntry(jarEntry);
    131                    final byte[] bytes = new byte[16384];
    132                    int bytesRead = jarInputStream.read(bytes);
    133                    while (bytesRead != -1) {
    134                        outputStream.write(bytes, 0, bytesRead);
    135                        bytesRead = jarInputStream.read(bytes);
    136                    }
    137                    outputStream.closeEntry();
    138                }
    139            } else {
    140                int i = 0;
    141                while (available(inputStream)) {
    142                    i++;
    143                    final Segment segment = new Segment();
    144                    segment.setLogLevel(logLevel);
    145                    segment.setLogStream(logFile != null ? (OutputStream) logFile : (OutputStream) System.out);
    146                    segment.setPreRead(false);
    147
    148                    if (i == 1) {
    149                        segment.log(Segment.LOG_LEVEL_VERBOSE,
    150                            "Unpacking from " + inputFileName + " to " + outputFileName);
    151                    }
    152                    segment.log(Segment.LOG_LEVEL_VERBOSE, "Reading segment " + i);
    153                    if (overrideDeflateHint) {
    154                        segment.overrideDeflateHint(deflateHint);
    155                    }
    156                    segment.unpack(inputStream, outputStream);
    157                    outputStream.flush();
    158
    159                    if (inputStream instanceof FileInputStream) {
    160                        inputFileName = ((FileInputStream) inputStream).getFD().toString();
    161                    }
    162                }
    163            }
    164        } finally {
    165            try {
    166                inputStream.close();
    167            } catch (final Exception e) {
    168            }
    169            try {
    170                outputStream.close();
    171            } catch (final Exception e) {
    172            }
    173            if (logFile != null) {
    174                try {
    175                    logFile.close();
    176                } catch (final Exception e) {
    177                }
    178            }
    179        }
    180        if (removePackFile) {
    181            boolean deleted = false;
    182            if (inputFileName != null) {
    183                final File file = new File(inputFileName);
    184                deleted = file.delete();
    185            }
    186            if (!deleted) {
    187                throw new Pack200Exception("Failed to delete the input file.");
    188            }
    189        }
    190    }
    191
    192    private boolean available(final InputStream inputStream) throws IOException {
    193        inputStream.mark(1);
    194        final int check = inputStream.read();
    195        inputStream.reset();
    196        return check != -1;
    197    }
    198
    199    /**
    200     * If removePackFile is set to true, the input file is deleted after unpacking.
    201     *
    202     * @param removePackFile If true, the input file is deleted after unpacking.
    203     */
    204    public void setRemovePackFile(final boolean removePackFile) {
    205        this.removePackFile = removePackFile;
    206    }
    207
    208    public void setVerbose(final boolean verbose) {
    209        if (verbose) {
    210            logLevel = Segment.LOG_LEVEL_VERBOSE;
    211        } else if (logLevel == Segment.LOG_LEVEL_VERBOSE) {
    212            logLevel = Segment.LOG_LEVEL_STANDARD;
    213        }
    214    }
    215
    216    public void setQuiet(final boolean quiet) {
    217        if (quiet) {
    218            logLevel = Segment.LOG_LEVEL_QUIET;
    219        } else if (logLevel == Segment.LOG_LEVEL_QUIET) {
    220            logLevel = Segment.LOG_LEVEL_QUIET;
    221        }
    222    }
    223
    224    public void setLogFile(final String logFileName) throws FileNotFoundException {
    225        this.logFile = new FileOutputStream(logFileName);
    226    }
    227
    228    public void setLogFile(final String logFileName, final boolean append) throws FileNotFoundException {
    229        logFile = new FileOutputStream(logFileName, append);
    230    }
    231
    232    public void setDeflateHint(final boolean deflateHint) {
    233        overrideDeflateHint = true;
    234        this.deflateHint = deflateHint;
    235    }
    236
    237}