View Javadoc
1   /*
2    * Copyright 2021 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty5.handler.codec.compression;
18  
19  import io.netty5.util.internal.PlatformDependent;
20  import io.netty5.util.internal.logging.InternalLogger;
21  import io.netty5.util.internal.logging.InternalLoggerFactory;
22  
23  public final class Zstd {
24  
25      private static final InternalLogger logger = InternalLoggerFactory.getInstance(Zstd.class);
26      private static final Throwable cause;
27  
28      static {
29          Throwable t = null;
30  
31          try {
32              Class.forName("com.github.luben.zstd.Zstd", false,
33                  PlatformDependent.getClassLoader(Zstd.class));
34          } catch (ClassNotFoundException e) {
35              t = e;
36              logger.debug(
37                  "zstd-jni not in the classpath; Zstd support will be unavailable.");
38          } catch (Throwable e) {
39              t = e;
40              logger.debug("Failed to load zstd-jni; Zstd support will be unavailable.", t);
41          }
42  
43          cause = t;
44      }
45  
46      /**
47       *
48       * @return true when zstd-jni is in the classpath
49       * and native library is available on this platform and could be loaded
50       */
51      public static boolean isAvailable() {
52          return cause == null;
53      }
54  
55      /**
56       * Throws when zstd support is missing from the classpath or is unavailable on this platform
57       * @throws Throwable a ClassNotFoundException if zstd-jni is missing
58       * or a ExceptionInInitializerError if zstd native lib can't be loaded
59       */
60      public static void ensureAvailability() throws Throwable {
61          if (cause != null) {
62              throw cause;
63          }
64      }
65  
66      /**
67       * Returns {@link Throwable} of unavailability cause
68       */
69      public static Throwable cause() {
70          return cause;
71      }
72  
73      private Zstd() {
74      }
75  }