• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.libraries.mobiledatadownload.file.common.internal;
17 
18 import android.os.Build;
19 import java.io.IOException;
20 import java.util.List;
21 import java.util.Locale;
22 
23 /** Static helpers for commonly-used operations on {@link Exception} classes. */
24 public final class Exceptions {
25 
26   /**
27    * Combines multiple {@code causes} into a single new {@link IOException}. Causes are attached to
28    * the new exception as {@code suppressed}, or if unsupported (SDK <19), the new exception message
29    * simply includes the number of causes.
30    */
combinedIOException(String message, List<IOException> causes)31   public static IOException combinedIOException(String message, List<IOException> causes) {
32     // addSuppressed is available on SDK 19+.
33     if (Build.VERSION.SDK_INT < 19) {
34       String suppressedCount =
35           String.format(Locale.US, " (%d suppressed exceptions)", causes.size());
36       return new IOException(message + suppressedCount);
37     }
38 
39     IOException result = new IOException(message);
40     for (IOException cause : causes) {
41       result.addSuppressed(cause);
42     }
43     return result;
44   }
45 
Exceptions()46   private Exceptions() {}
47 }
48