Skip to content

Commit 3be6a7b

Browse files
committed
Make it really old Android compatible - again
Base64 is not available on such Androids too
1 parent c106d24 commit 3be6a7b

File tree

7 files changed

+90
-18
lines changed

7 files changed

+90
-18
lines changed

checkstyle/checkstyle.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
<module name="AvoidStarImport"/>
101101
<module name="IllegalImport"> <!-- defaults to sun.* packages -->
102102
<property name="illegalClasses" value="
103+
java.nio.charset.StandardCharsets,
104+
java.util.Base64,
103105
org.jetbrains.annotations.Nullable,
104106
org.jetbrains.annotations.NotNull,
105107
androidx.annotation.Nullable,

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampCommentsExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.extractor.services.bandcamp.extractors;
22

33
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;
4+
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
45

56
import com.grack.nanojson.JsonArray;
67
import com.grack.nanojson.JsonObject;
@@ -21,7 +22,6 @@
2122
import org.schabi.newpipe.extractor.utils.JsonUtils;
2223

2324
import java.io.IOException;
24-
import java.nio.charset.StandardCharsets;
2525
import java.util.Collections;
2626
import java.util.List;
2727

@@ -107,7 +107,7 @@ private JsonObject fetchReviewsData(final String trackId, final String token)
107107
.value("token", token)
108108
.value("count", 7)
109109
.array("exclude_fan_ids").end()
110-
.end().done().getBytes(StandardCharsets.UTF_8)).responseBody());
110+
.end().done().getBytes(UTF_8)).responseBody());
111111
} catch (final IOException | ReCaptchaException e) {
112112
throw new ParsingException("Could not fetch reviews", e);
113113
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.schabi.newpipe.extractor.utils.Utils;
2020

2121
import java.io.IOException;
22-
import java.nio.charset.StandardCharsets;
2322
import java.time.DateTimeException;
2423
import java.time.ZonedDateTime;
2524
import java.time.format.DateTimeFormatter;
@@ -31,6 +30,7 @@
3130
import javax.annotation.Nonnull;
3231
import javax.annotation.Nullable;
3332

33+
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
3434
import static org.schabi.newpipe.extractor.Image.HEIGHT_UNKNOWN;
3535
import static org.schabi.newpipe.extractor.Image.WIDTH_UNKNOWN;
3636
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
@@ -125,7 +125,7 @@ public static JsonObject getArtistDetails(final String id) throws ParsingExcepti
125125
.value("band_id", id)
126126
.end()
127127
.done()
128-
.getBytes(StandardCharsets.UTF_8)).responseBody());
128+
.getBytes(UTF_8)).responseBody());
129129
} catch (final IOException | ReCaptchaException | JsonParserException e) {
130130
throw new ParsingException("Could not download band details", e);
131131
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import javax.annotation.Nonnull;
2020
import java.io.IOException;
21-
import java.nio.charset.StandardCharsets;
2221
import java.util.Collections;
2322

23+
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
2424
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;
2525

2626
public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem> {
@@ -45,7 +45,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
4545
json = JsonParser.object().from(getDownloader().postWithContentTypeJson(
4646
FEATURED_API_URL,
4747
Collections.emptyMap(),
48-
"{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8))
48+
"{\"platform\":\"\",\"version\":0}".getBytes(UTF_8))
4949
.responseBody());
5050
} catch (final JsonParserException e) {
5151
throw new ParsingException("Could not parse Bandcamp featured API response", e);

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
import org.schabi.newpipe.extractor.utils.Utils;
1919

2020
import java.io.IOException;
21-
import java.nio.charset.StandardCharsets;
21+
import java.io.UnsupportedEncodingException;
2222

2323
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
2424
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
2525
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
2626
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
27+
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
2728

2829
import javax.annotation.Nonnull;
2930

@@ -130,11 +131,11 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
130131
}
131132
} else {
132133
try {
133-
json = JsonParser.object().from(new String(page.getBody(), StandardCharsets.UTF_8));
134+
json = JsonParser.object().from(new String(page.getBody(), UTF_8));
134135
isReply = true;
135136
total = json.getArray(CHILDREN).size();
136137
collectRepliesFrom(collector, json);
137-
} catch (final JsonParserException e) {
138+
} catch (final JsonParserException | UnsupportedEncodingException e) {
138139
throw new ParsingException(
139140
"Could not parse json data for nested comments info", e);
140141
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
import javax.annotation.Nonnull;
1919
import javax.annotation.Nullable;
20-
import java.nio.charset.StandardCharsets;
20+
import java.io.UnsupportedEncodingException;
2121
import java.util.List;
2222
import java.util.Objects;
2323

2424
import static org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeCommentsExtractor.CHILDREN;
2525
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getAvatarsFromOwnerAccountOrVideoChannelObject;
2626
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseDateFrom;
27+
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
2728

2829
public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
2930
@Nonnull
@@ -130,8 +131,13 @@ public Page getReplies() throws ParsingException {
130131
// is also structured like a JsonObject.
131132
final JsonObject pageContent = new JsonObject();
132133
pageContent.put(CHILDREN, children);
133-
return new Page(repliesUrl, threadId,
134-
JsonWriter.string(pageContent).getBytes(StandardCharsets.UTF_8));
134+
try {
135+
return new Page(repliesUrl, threadId,
136+
JsonWriter.string(pageContent).getBytes(UTF_8));
137+
} catch (final UnsupportedEncodingException e) {
138+
throw new ParsingException(
139+
"Could not parse data", e);
140+
}
135141
}
136142
return new Page(repliesUrl, threadId);
137143
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/ProtoBuilder.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
55
import java.net.URLEncoder;
6-
import java.nio.charset.StandardCharsets;
7-
import java.util.Base64;
6+
import java.io.UnsupportedEncodingException;
7+
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
88

99
public class ProtoBuilder {
1010
ByteArrayOutputStream byteBuffer;
@@ -18,8 +18,12 @@ public byte[] toBytes() {
1818
}
1919

2020
public String toUrlencodedBase64() {
21-
final String b64 = Base64.getUrlEncoder().encodeToString(toBytes());
22-
return URLEncoder.encode(b64, StandardCharsets.UTF_8);
21+
try {
22+
final String b64 = encodeUrl(toBytes());
23+
return URLEncoder.encode(b64, UTF_8);
24+
} catch (final IOException e) {
25+
throw new RuntimeException(e);
26+
}
2327
}
2428

2529
private void writeVarint(final long val) {
@@ -56,8 +60,12 @@ public void varint(final int field, final long val) {
5660
}
5761

5862
public void string(final int field, final String string) {
59-
final byte[] strBts = string.getBytes(StandardCharsets.UTF_8);
60-
bytes(field, strBts);
63+
try {
64+
final byte[] strBts = string.getBytes(UTF_8);
65+
bytes(field, strBts);
66+
} catch (final IOException e) {
67+
throw new RuntimeException(e);
68+
}
6169
}
6270

6371
public void bytes(final int field, final byte[] bytes) {
@@ -69,4 +77,59 @@ public void bytes(final int field, final byte[] bytes) {
6977
throw new RuntimeException(e);
7078
}
7179
}
80+
81+
private static final byte[] MAP = new byte[] {
82+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
83+
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
84+
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
85+
'5', '6', '7', '8', '9', '+', '/'
86+
};
87+
88+
private static final byte[] URL_MAP = new byte[] {
89+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
90+
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
91+
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
92+
'5', '6', '7', '8', '9', '-', '_'
93+
};
94+
95+
private static String encode(final byte[] in) {
96+
return encode(in, MAP);
97+
}
98+
99+
private static String encodeUrl(final byte[] in) {
100+
return encode(in, URL_MAP);
101+
}
102+
103+
private static String encode(final byte[] in, final byte[] map) {
104+
final int length = (in.length + 2) / 3 * 4;
105+
final byte[] out = new byte[length];
106+
int index = 0;
107+
final int end = in.length - in.length % 3;
108+
for (int i = 0; i < end; i += 3) {
109+
out[index++] = map[(in[i] & 0xff) >> 2];
110+
out[index++] = map[((in[i] & 0x03) << 4) | ((in[i + 1] & 0xff) >> 4)];
111+
out[index++] = map[((in[i + 1] & 0x0f) << 2) | ((in[i + 2] & 0xff) >> 6)];
112+
out[index++] = map[(in[i + 2] & 0x3f)];
113+
}
114+
switch (in.length % 3) {
115+
case 1:
116+
out[index++] = map[(in[end] & 0xff) >> 2];
117+
out[index++] = map[(in[end] & 0x03) << 4];
118+
out[index++] = '=';
119+
out[index++] = '=';
120+
break;
121+
case 2:
122+
out[index++] = map[(in[end] & 0xff) >> 2];
123+
out[index++] = map[((in[end] & 0x03) << 4) | ((in[end + 1] & 0xff) >> 4)];
124+
out[index++] = map[((in[end + 1] & 0x0f) << 2)];
125+
out[index++] = '=';
126+
break;
127+
}
128+
try {
129+
return new String(out, "US-ASCII");
130+
} catch (final UnsupportedEncodingException e) {
131+
throw new RuntimeException(e);
132+
}
133+
}
134+
72135
}

0 commit comments

Comments
 (0)