what-the-bank/sources/com/airbnb/lottie/network/DefaultLottieFetchResult.java

82 lines
2.6 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.airbnb.lottie.network;
import com.airbnb.lottie.utils.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
/* loaded from: classes.dex */
public class DefaultLottieFetchResult implements LottieFetchResult {
private final HttpURLConnection connection;
public DefaultLottieFetchResult(HttpURLConnection httpURLConnection) {
this.connection = httpURLConnection;
}
@Override // com.airbnb.lottie.network.LottieFetchResult
public boolean isSuccessful() {
return this.connection.getResponseCode() / 100 == 2;
}
@Override // com.airbnb.lottie.network.LottieFetchResult
public InputStream bodyByteStream() throws IOException {
return this.connection.getInputStream();
}
@Override // com.airbnb.lottie.network.LottieFetchResult
public String contentType() {
return this.connection.getContentType();
}
@Override // com.airbnb.lottie.network.LottieFetchResult
public String error() {
try {
if (isSuccessful()) {
return null;
}
StringBuilder sb = new StringBuilder("Unable to fetch ");
sb.append(this.connection.getURL());
sb.append(". Failed with ");
sb.append(this.connection.getResponseCode());
sb.append("\n");
sb.append(getErrorFromConnection(this.connection));
return sb.toString();
} catch (IOException e) {
Logger.warning("get error failed ", e);
return e.getMessage();
}
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public void close() {
this.connection.disconnect();
}
private String getErrorFromConnection(HttpURLConnection httpURLConnection) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()));
StringBuilder sb = new StringBuilder();
while (true) {
try {
String readLine = bufferedReader.readLine();
if (readLine != null) {
sb.append(readLine);
sb.append('\n');
} else {
try {
break;
} catch (Exception unused) {
}
}
} finally {
try {
bufferedReader.close();
} catch (Exception unused2) {
}
}
}
return sb.toString();
}
}