查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.netty.handler.codec.compression;
18  
19  import com.aayushatharva.brotli4j.Brotli4jLoader;
20  import io.netty.util.internal.PlatformDependent;
21  import io.netty.util.internal.logging.InternalLogger;
22  import io.netty.util.internal.logging.InternalLoggerFactory;
23  
24  public final class Brotli {
25  
26      private static final InternalLogger logger = InternalLoggerFactory.getInstance(Brotli.class);
27      private static final ClassNotFoundException CNFE;
28      private static Throwable cause;
29  
30      static {
31          ClassNotFoundException cnfe = null;
32  
33          try {
34              Class.forName("com.aayushatharva.brotli4j.Brotli4jLoader", false,
35                  PlatformDependent.getClassLoader(Brotli.class));
36          } catch (ClassNotFoundException t) {
37              cnfe = t;
38              logger.debug(
39                  "brotli4j not in the classpath; Brotli support will be unavailable.");
40          }
41  
42          CNFE = cnfe;
43  
44          // If in the classpath, try to load the native library and initialize brotli4j.
45          if (cnfe == null) {
46              cause = Brotli4jLoader.getUnavailabilityCause();
47              if (cause != null) {
48                  logger.debug("Failed to load brotli4j; Brotli support will be unavailable.", cause);
49              }
50          }
51      }
52  
53      /**
54       *
55       * @return true when brotli4j is in the classpath
56       * and native library is available on this platform and could be loaded
57       */
58      public static boolean isAvailable() {
59          return CNFE == null && Brotli4jLoader.isAvailable();
60      }
61  
62      /**
63       * Throws when brotli support is missing from the classpath or is unavailable on this platform
64       * @throws Throwable a ClassNotFoundException if brotli4j is missing
65       * or a UnsatisfiedLinkError if brotli4j native lib can't be loaded
66       */
67      public static void ensureAvailability() throws Throwable {
68          if (CNFE != null) {
69              throw CNFE;
70          }
71          Brotli4jLoader.ensureAvailability();
72      }
73  
74      /**
75       * Returns {@link Throwable} of unavailability cause
76       */
77      public static Throwable cause() {
78          return cause;
79      }
80  
81      private Brotli() {
82      }
83  }