import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
+import okio.ByteString;
+
public class Utils {
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
throw new IOException(throwableOptional);
}
}
- return response.successOrThrow();
+ try {
+ return response.successOrThrow();
+ } catch (Throwable e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ public static ByteString firstNonEmpty(ByteString... strings) {
+ for (final var s : strings) {
+ if (s.size() > 0) {
+ return s;
+ }
+ }
+ return ByteString.EMPTY;
+ }
+
+ @SafeVarargs
+ public static <T> List<T> firstNonEmpty(List<T>... values) {
+ for (final var s : values) {
+ if (!s.isEmpty()) {
+ return s;
+ }
+ }
+ return List.of();
+ }
+
+ public static String firstNonEmpty(String... strings) {
+ for (final var s : strings) {
+ if (!s.isEmpty()) {
+ return s;
+ }
+ }
+ return "";
+ }
+
+ @SafeVarargs
+ public static <T> T firstNonNull(T... values) {
+ for (final var v : values) {
+ if (v != null) {
+ return v;
+ }
+ }
+ return null;
+ }
+
+ public static String nullIfEmpty(String string) {
+ return string == null || string.isEmpty() ? null : string;
+ }
+
+ public static Proxy getHttpsProxy() {
+ final URI uri;
+ try {
+ uri = new URI("https://example");
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ final var proxies = ProxySelector.getDefault().select(uri);
+ if (proxies.isEmpty()) {
+ return Proxy.NO_PROXY;
+ } else {
+ return proxies.getFirst();
+ }
}
}