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.brotli; 020 021import org.apache.commons.compress.utils.OsgiUtils; 022 023/** 024 * Utility code for the Brotli compression format. 025 * @ThreadSafe 026 * @since 1.14 027 */ 028public class BrotliUtils { 029 030 enum CachedAvailability { 031 DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE 032 } 033 034 private static volatile CachedAvailability cachedBrotliAvailability; 035 036 static { 037 cachedBrotliAvailability = CachedAvailability.DONT_CACHE; 038 setCacheBrotliAvailablity(!OsgiUtils.isRunningInOsgiEnvironment()); 039 } 040 041 // only exists to support unit tests 042 static CachedAvailability getCachedBrotliAvailability() { 043 return cachedBrotliAvailability; 044 } 045 046 047 private static boolean internalIsBrotliCompressionAvailable() { 048 try { 049 Class.forName("org.brotli.dec.BrotliInputStream"); 050 return true; 051 } catch (final NoClassDefFoundError | Exception error) { // NOSONAR 052 return false; 053 } 054 } 055 056 /** 057 * Are the classes required to support Brotli compression available? 058 * @return true if the classes required to support Brotli compression are available 059 */ 060 public static boolean isBrotliCompressionAvailable() { 061 final CachedAvailability cachedResult = cachedBrotliAvailability; 062 if (cachedResult != CachedAvailability.DONT_CACHE) { 063 return cachedResult == CachedAvailability.CACHED_AVAILABLE; 064 } 065 return internalIsBrotliCompressionAvailable(); 066 } 067 068 /** 069 * Whether to cache the result of the Brotli for Java check. 070 * 071 * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p> 072 * @param doCache whether to cache the result 073 */ 074 public static void setCacheBrotliAvailablity(final boolean doCache) { 075 if (!doCache) { 076 cachedBrotliAvailability = CachedAvailability.DONT_CACHE; 077 } else if (cachedBrotliAvailability == CachedAvailability.DONT_CACHE) { 078 final boolean hasBrotli = internalIsBrotliCompressionAvailable(); 079 cachedBrotliAvailability = hasBrotli ? CachedAvailability.CACHED_AVAILABLE 080 : CachedAvailability.CACHED_UNAVAILABLE; 081 } 082 } 083 084 /** Private constructor to prevent instantiation of this utility class. */ 085 private BrotliUtils() { 086 } 087}