View Javadoc
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   *   https://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  
20  package org.apache.commons.compress;
21  
22  import java.io.IOException;
23  import java.util.function.Supplier;
24  
25  import org.apache.commons.lang3.function.Suppliers;
26  
27  /**
28   * Signals that a Pack200 Compress exception of some sort has occurred.
29   *
30   * @since 1.28.0
31   */
32  public class CompressException extends IOException {
33  
34      /** Serial. */
35      private static final long serialVersionUID = 1;
36  
37      /**
38       * Checks that the specified object reference is not {@code null} and throws a customized {@link CompressException} if it is. *
39       *
40       * @param <T>             The type of the reference.
41       * @param <E>             The type of the exception.
42       * @param cls             The exception class.
43       * @param obj             The object reference to check for nullity.
44       * @param messageSupplier supplier of the detail message to be used in the event that a {@code ArchiveException} is thrown
45       * @return {@code obj} if not {@code null}.
46       * @throws E if {@code obj} is {@code null}.
47       */
48      protected static <T, E extends Throwable> T requireNonNull(final Class<? super E> cls, final T obj, final Supplier<String> messageSupplier) throws E {
49          if (obj == null) {
50              try {
51                  cls.getConstructor(String.class).newInstance(Suppliers.get(messageSupplier));
52              } catch (ReflectiveOperationException | SecurityException e) {
53                  new CompressException(Suppliers.get(messageSupplier), e);
54              }
55          }
56          return obj;
57      }
58  
59      /**
60       * Constructs an {@code CompressException} with {@code null} as its error detail message.
61       */
62      public CompressException() {
63          // empty
64      }
65  
66      /**
67       * Constructs a new exception with the specified detail message. The cause is not initialized.
68       *
69       * @param message The message (which is saved for later retrieval by the {@link #getMessage()} method).
70       */
71      public CompressException(final String message) {
72          super(message);
73      }
74  
75      /**
76       * Constructs a new exception with the specified detail message and cause.
77       *
78       * @param message The message (which is saved for later retrieval by the {@link #getMessage()} method).
79       * @param cause   The cause (which is saved for later retrieval by the {@link #getCause()} method). A null value indicates that the cause is nonexistent or
80       *                unknown.
81       */
82      public CompressException(final String message, final Throwable cause) {
83          super(message, cause);
84      }
85  
86      /**
87       * Constructs a {@code CompressException} with the specified cause and a detail message.
88       *
89       * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method). (A null value is permitted, and indicates that the cause
90       *              is nonexistent or unknown.)
91       */
92      public CompressException(final Throwable cause) {
93          super(cause);
94      }
95  }