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