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.zstandard; 020 021import org.apache.commons.compress.utils.OsgiUtils; 022 023/** 024 * Utility code for the Zstandard compression format. 025 * @ThreadSafe 026 * @since 1.16 027 */ 028public class ZstdUtils { 029 030 enum CachedAvailability { 031 DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE 032 } 033 034 /** 035 * Zstandard Frame Magic Bytes. 036 */ 037 private static final byte[] ZSTANDARD_FRAME_MAGIC = { 038 (byte) 0x28, (byte) 0xB5, (byte) 0x2F, (byte) 0xFD 039 }; 040 041 /** 042 * Skippable Frame Magic Bytes - the three common bytes. 043 */ 044 private static final byte[] SKIPPABLE_FRAME_MAGIC = { 045 (byte) 0x2A, (byte) 0x4D, (byte) 0x18 046 }; 047 048 private static volatile CachedAvailability cachedZstdAvailability; 049 050 static { 051 cachedZstdAvailability = CachedAvailability.DONT_CACHE; 052 setCacheZstdAvailablity(!OsgiUtils.isRunningInOsgiEnvironment()); 053 } 054 055 // only exists to support unit tests 056 static CachedAvailability getCachedZstdAvailability() { 057 return cachedZstdAvailability; 058 } 059 060 private static boolean internalIsZstdCompressionAvailable() { 061 try { 062 Class.forName("com.github.luben.zstd.ZstdInputStream"); 063 return true; 064 } catch (final NoClassDefFoundError | Exception error) { // NOSONAR 065 return false; 066 } 067 } 068 069 /** 070 * Are the classes required to support Zstandard compression available? 071 * @return true if the classes required to support Zstandard compression are available 072 */ 073 public static boolean isZstdCompressionAvailable() { 074 final CachedAvailability cachedResult = cachedZstdAvailability; 075 if (cachedResult != CachedAvailability.DONT_CACHE) { 076 return cachedResult == CachedAvailability.CACHED_AVAILABLE; 077 } 078 return internalIsZstdCompressionAvailable(); 079 } 080 081 /** 082 * Checks if the signature matches what is expected for a Zstandard file. 083 * 084 * @param signature the bytes to check 085 * @param length the number of bytes to check 086 * @return true if signature matches the Ztstandard or skippable 087 * frame magic bytes, false otherwise 088 */ 089 public static boolean matches(final byte[] signature, final int length) { 090 if (length < ZSTANDARD_FRAME_MAGIC.length) { 091 return false; 092 } 093 094 boolean isZstandard = true; 095 for (int i = 0; i < ZSTANDARD_FRAME_MAGIC.length; ++i) { 096 if (signature[i] != ZSTANDARD_FRAME_MAGIC[i]) { 097 isZstandard = false; 098 break; 099 } 100 } 101 if (isZstandard) { 102 return true; 103 } 104 105 if (0x50 == (signature[0] & 0xF0)) { 106 // skippable frame 107 for (int i = 0; i < SKIPPABLE_FRAME_MAGIC.length; ++i) { 108 if (signature[i + 1] != SKIPPABLE_FRAME_MAGIC[i]) { 109 return false; 110 } 111 } 112 113 return true; 114 } 115 116 return false; 117 } 118 119 /** 120 * Whether to cache the result of the Zstandard for Java check. 121 * 122 * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p> 123 * @param doCache whether to cache the result 124 */ 125 public static void setCacheZstdAvailablity(final boolean doCache) { 126 if (!doCache) { 127 cachedZstdAvailability = CachedAvailability.DONT_CACHE; 128 } else if (cachedZstdAvailability == CachedAvailability.DONT_CACHE) { 129 final boolean hasZstd = internalIsZstdCompressionAvailable(); 130 cachedZstdAvailability = hasZstd ? CachedAvailability.CACHED_AVAILABLE 131 : CachedAvailability.CACHED_UNAVAILABLE; 132 } 133 } 134 135 /** Private constructor to prevent instantiation of this utility class. */ 136 private ZstdUtils() { 137 } 138}