BrotliUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.compressors.brotli;

  20. import org.apache.commons.compress.utils.OsgiUtils;

  21. /**
  22.  * Utility code for the Brotli compression format.
  23.  *
  24.  * @ThreadSafe
  25.  * @since 1.14
  26.  */
  27. public class BrotliUtils {

  28.     enum CachedAvailability {
  29.         DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE
  30.     }

  31.     private static volatile CachedAvailability cachedBrotliAvailability;

  32.     static {
  33.         cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
  34.         setCacheBrotliAvailablity(!OsgiUtils.isRunningInOsgiEnvironment());
  35.     }

  36.     // only exists to support unit tests
  37.     static CachedAvailability getCachedBrotliAvailability() {
  38.         return cachedBrotliAvailability;
  39.     }

  40.     private static boolean internalIsBrotliCompressionAvailable() {
  41.         try {
  42.             Class.forName("org.brotli.dec.BrotliInputStream");
  43.             return true;
  44.         } catch (final NoClassDefFoundError | Exception error) { // NOSONAR
  45.             return false;
  46.         }
  47.     }

  48.     /**
  49.      * Are the classes required to support Brotli compression available?
  50.      *
  51.      * @return true if the classes required to support Brotli compression are available
  52.      */
  53.     public static boolean isBrotliCompressionAvailable() {
  54.         final CachedAvailability cachedResult = cachedBrotliAvailability;
  55.         if (cachedResult != CachedAvailability.DONT_CACHE) {
  56.             return cachedResult == CachedAvailability.CACHED_AVAILABLE;
  57.         }
  58.         return internalIsBrotliCompressionAvailable();
  59.     }

  60.     /**
  61.      * Whether to cache the result of the Brotli for Java check.
  62.      *
  63.      * <p>
  64.      * This defaults to {@code false} in an OSGi environment and {@code true} otherwise.
  65.      * </p>
  66.      *
  67.      * @param doCache whether to cache the result
  68.      */
  69.     public static void setCacheBrotliAvailablity(final boolean doCache) {
  70.         if (!doCache) {
  71.             cachedBrotliAvailability = CachedAvailability.DONT_CACHE;
  72.         } else if (cachedBrotliAvailability == CachedAvailability.DONT_CACHE) {
  73.             final boolean hasBrotli = internalIsBrotliCompressionAvailable();
  74.             cachedBrotliAvailability = hasBrotli ? CachedAvailability.CACHED_AVAILABLE : CachedAvailability.CACHED_UNAVAILABLE;
  75.         }
  76.     }

  77.     /** Private constructor to prevent instantiation of this utility class. */
  78.     private BrotliUtils() {
  79.     }
  80. }