001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.compressors.xz;
020
021import java.util.HashMap;
022import java.util.Map;
023import org.apache.commons.compress.compressors.FileNameUtil;
024
025/**
026 * Utility code for the xz compression format.
027 * @ThreadSafe
028 * @since 1.4
029 */
030public class XZUtils {
031
032    private static final FileNameUtil fileNameUtil;
033
034    /**
035     * XZ Header Magic Bytes begin a XZ file.
036     *
037     * <p>This is a copy of {@code org.tukaani.xz.XZ.HEADER_MAGIC} in
038     * XZ for Java version 1.5.</p>
039     */
040    private static final byte[] HEADER_MAGIC = {
041        (byte) 0xFD, '7', 'z', 'X', 'Z', '\0'
042    };
043
044    static enum CachedAvailability {
045        DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE
046    }
047
048    private static volatile CachedAvailability cachedXZAvailability;
049
050    static {
051        Map<String, String> uncompressSuffix = new HashMap<String, String>();
052        uncompressSuffix.put(".txz", ".tar");
053        uncompressSuffix.put(".xz", "");
054        uncompressSuffix.put("-xz", "");
055        fileNameUtil = new FileNameUtil(uncompressSuffix, ".xz");
056        cachedXZAvailability = CachedAvailability.DONT_CACHE;
057        try {
058            Class.forName("org.osgi.framework.BundleEvent");
059        } catch (Exception ex) {
060            setCacheXZAvailablity(true);
061        }
062    }
063
064    /** Private constructor to prevent instantiation of this utility class. */
065    private XZUtils() {
066    }
067
068    /**
069     * Checks if the signature matches what is expected for a .xz file.
070     *
071     * <p>This is more or less a copy of the version found in {@link
072     * XZCompressorInputStream} but doesn't depend on the presence of
073     * XZ for Java.</p>
074     *
075     * @param   signature     the bytes to check
076     * @param   length        the number of bytes to check
077     * @return  true if signature matches the .xz magic bytes, false otherwise
078     * @since 1.9
079     */
080    public static boolean matches(byte[] signature, int length) {
081        if (length < HEADER_MAGIC.length) {
082            return false;
083        }
084
085        for (int i = 0; i < HEADER_MAGIC.length; ++i) {
086            if (signature[i] != HEADER_MAGIC[i]) {
087                return false;
088            }
089        }
090
091        return true;
092    }
093
094    /**
095     * Are the classes required to support XZ compression available?
096     * @since 1.5
097     */
098    public static boolean isXZCompressionAvailable() {
099        final CachedAvailability cachedResult = cachedXZAvailability;
100        if (cachedResult != CachedAvailability.DONT_CACHE) {
101            return cachedResult == CachedAvailability.CACHED_AVAILABLE;
102        }
103        return internalIsXZCompressionAvailable();
104    }
105
106    private static boolean internalIsXZCompressionAvailable() {
107        try {
108            XZCompressorInputStream.matches(null, 0);
109            return true;
110        } catch (NoClassDefFoundError error) {
111            return false;
112        }
113    }
114
115    /**
116     * Detects common xz suffixes in the given filename.
117     *
118     * @param filename name of a file
119     * @return {@code true} if the filename has a common xz suffix,
120     *         {@code false} otherwise
121     */
122    public static boolean isCompressedFilename(String filename) {
123        return fileNameUtil.isCompressedFilename(filename);
124    }
125
126    /**
127     * Maps the given name of a xz-compressed file to the name that the
128     * file should have after uncompression. Commonly used file type specific
129     * suffixes like ".txz" are automatically detected and
130     * correctly mapped. For example the name "package.txz" is mapped to
131     * "package.tar". And any filenames with the generic ".xz" suffix
132     * (or any other generic xz suffix) is mapped to a name without that
133     * suffix. If no xz suffix is detected, then the filename is returned
134     * unmapped.
135     *
136     * @param filename name of a file
137     * @return name of the corresponding uncompressed file
138     */
139    public static String getUncompressedFilename(String filename) {
140        return fileNameUtil.getUncompressedFilename(filename);
141    }
142
143    /**
144     * Maps the given filename to the name that the file should have after
145     * compression with xz. Common file types with custom suffixes for
146     * compressed versions are automatically detected and correctly mapped.
147     * For example the name "package.tar" is mapped to "package.txz". If no
148     * custom mapping is applicable, then the default ".xz" suffix is appended
149     * to the filename.
150     *
151     * @param filename name of a file
152     * @return name of the corresponding compressed file
153     */
154    public static String getCompressedFilename(String filename) {
155        return fileNameUtil.getCompressedFilename(filename);
156    }
157
158    /**
159     * Whether to cache the result of the XZ for Java check.
160     *
161     * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p>
162     * @param doCache whether to cache the result
163     * @since 1.9
164     */
165    public static void setCacheXZAvailablity(boolean doCache) {
166        if (!doCache) {
167            cachedXZAvailability = CachedAvailability.DONT_CACHE;
168        } else if (cachedXZAvailability == CachedAvailability.DONT_CACHE) {
169            final boolean hasXz = internalIsXZCompressionAvailable();
170            cachedXZAvailability = hasXz ? CachedAvailability.CACHED_AVAILABLE
171                : CachedAvailability.CACHED_UNAVAILABLE;
172        }
173    }
174
175    // only exists to support unit tests
176    static CachedAvailability getCachedXZAvailability() {
177        return cachedXZAvailability;
178    }
179}