That fuck shit the fascists are using
1package org.tm.archive.util;
2
3import androidx.annotation.NonNull;
4import androidx.core.text.HtmlCompat;
5
6import java.io.ByteArrayOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.nio.charset.Charset;
10import java.nio.charset.StandardCharsets;
11import java.util.Objects;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import okhttp3.MediaType;
16import okhttp3.ResponseBody;
17
18public final class OkHttpUtil {
19
20 private static final Pattern CHARSET_PATTERN = Pattern.compile("charset=[\"']?([a-zA-Z0-9\\\\-]+)[\"']?");
21
22 private OkHttpUtil() {}
23
24 public static byte[] readAsBytes(@NonNull InputStream bodyStream, long sizeLimit) throws IOException {
25 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
26
27 byte[] buffer = new byte[(int) ByteUnit.KILOBYTES.toBytes(32)];
28 int readLength = 0;
29 int totalLength = 0;
30
31 while ((readLength = bodyStream.read(buffer)) >= 0) {
32 if (totalLength + readLength > sizeLimit) {
33 throw new IOException("Exceeded maximum size during read!");
34 }
35
36 outputStream.write(buffer, 0, readLength);
37 totalLength += readLength;
38 }
39
40 return outputStream.toByteArray();
41 }
42 public static String readAsString(@NonNull ResponseBody body, long sizeLimit) throws IOException {
43 if (body.contentLength() > sizeLimit) {
44 throw new IOException("Content-Length exceeded maximum size!");
45 }
46
47 byte[] data = readAsBytes(body.byteStream(), sizeLimit);
48 MediaType contentType = body.contentType();
49 Charset charset = contentType != null ? contentType.charset(null) : null;
50
51 charset = charset == null ? getHtmlCharset(new String(data)) : charset;
52
53 return new String(data, Objects.requireNonNull(charset));
54 }
55
56 private static @NonNull Charset getHtmlCharset(String html) {
57 Matcher charsetMatcher = CHARSET_PATTERN.matcher(html);
58 if (charsetMatcher.find() && charsetMatcher.groupCount() > 0) {
59 try {
60 return Objects.requireNonNull(Charset.forName(fromDoubleEncoded(charsetMatcher.group(1))));
61 } catch (Exception ignored) {}
62 }
63 return StandardCharsets.UTF_8;
64 }
65
66 private static @NonNull String fromDoubleEncoded(@NonNull String html) {
67 return HtmlCompat.fromHtml(HtmlCompat.fromHtml(html, 0).toString(), 0).toString();
68 }
69}