Use standard charset name

This commit is contained in:
M66B
2019-10-30 13:38:25 +01:00
parent 6a94e8a5f9
commit 15001deed7
4 changed files with 52 additions and 5 deletions

View File

@@ -57,7 +57,10 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
@@ -68,6 +71,8 @@ class ImageHelper {
private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "image");
private static final int MAX_REDIRECTS = 10;
static Bitmap generateIdenticon(@NonNull String email, int size, int pixels, Context context) {
byte[] hash = getHash(email);
float h = Math.abs(email.hashCode()) % 360;
@@ -346,7 +351,43 @@ class ImageHelper {
}
Bitmap bm;
try (BufferedInputStream is = new BufferedInputStream(new URL(a.source).openStream())) {
HttpURLConnection urlConnection = null;
try {
String url = a.source;
int redirects = 0;
while (true) {
urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.connect();
int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_PERM ||
status == HttpURLConnection.HTTP_MOVED_TEMP) {
redirects++;
if (redirects > MAX_REDIRECTS)
throw new IOException("Too many redirects");
String location = URLDecoder.decode(
urlConnection.getHeaderField("Location"),
StandardCharsets.UTF_8.name());
url = new URL(new URL(url), location).toExternalForm();
Log.i("Redirect #" + redirects + " to " + url);
urlConnection.disconnect();
continue;
}
if (status != HttpURLConnection.HTTP_OK)
throw new IOException("http " + status);
break;
}
BufferedInputStream is = new BufferedInputStream(urlConnection.getInputStream());
Log.i("Probe " + a.source);
is.mark(8192);
BitmapFactory.Options options = new BitmapFactory.Options();
@@ -366,6 +407,9 @@ class ImageHelper {
bm = BitmapFactory.decodeStream(is, null, options);
} else
bm = BitmapFactory.decodeStream(is);
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
if (bm == null)