Skip to content

Commit 9cdf634

Browse files
committed
fix(android): Use the builtin Charsets instead of the StandardCharsets to restore Android API 14 compatibility
1 parent 4af8a07 commit 9cdf634

File tree

6 files changed

+18
-17
lines changed

6 files changed

+18
-17
lines changed

google-http-client/src/main/java/com/google/api/client/util/StringUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.google.api.client.util;
1616

1717
import java.io.UnsupportedEncodingException;
18-
import java.nio.charset.StandardCharsets;
18+
import com.google.api.client.util.Charsets;
1919

2020
/**
2121
* Utilities for strings.
@@ -48,7 +48,7 @@ public static byte[] getBytesUtf8(String string) {
4848
if (string == null) {
4949
return null;
5050
}
51-
return string.getBytes(StandardCharsets.UTF_8);
51+
return string.getBytes(Charsets.UTF_8);
5252
}
5353

5454
/**
@@ -66,7 +66,7 @@ public static String newStringUtf8(byte[] bytes) {
6666
if (bytes == null) {
6767
return null;
6868
}
69-
return new String(bytes, StandardCharsets.UTF_8);
69+
return new String(bytes, Charsets.UTF_8);
7070
}
7171

7272
private StringUtils() {}

google-http-client/src/main/java/com/google/api/client/util/escape/CharEscapers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
package com.google.api.client.util.escape;
1717

18+
import com.google.api.client.util.Charsets;
1819
import java.io.UnsupportedEncodingException;
1920
import java.net.URLDecoder;
20-
import java.nio.charset.StandardCharsets;
2121

2222
/**
2323
* Utility functions for encoding and decoding URIs.
@@ -118,7 +118,7 @@ public static String escapeUriConformant(String value) {
118118
*/
119119
public static String decodeUri(String uri) {
120120
try {
121-
return URLDecoder.decode(uri, StandardCharsets.UTF_8.name());
121+
return URLDecoder.decode(uri, Charsets.UTF_8.name());
122122
} catch (UnsupportedEncodingException e) {
123123
// UTF-8 encoding guaranteed to be supported by JVM
124124
throw new RuntimeException(e);
@@ -140,7 +140,7 @@ public static String decodeUriPath(String path) {
140140
return null;
141141
}
142142
try {
143-
return URLDecoder.decode(path.replace("+", "%2B"), StandardCharsets.UTF_8.name());
143+
return URLDecoder.decode(path.replace("+", "%2B"), Charsets.UTF_8.name());
144144
} catch (UnsupportedEncodingException e) {
145145
// UTF-8 encoding guaranteed to be supported by JVM
146146
throw new RuntimeException(e);

google-http-client/src/test/java/com/google/api/client/http/ConsumingInputStreamTest.java

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

1919
import static org.junit.Assert.assertEquals;
2020

21+
import com.google.api.client.util.Charsets;
2122
import java.io.IOException;
2223
import java.io.InputStream;
23-
import java.nio.charset.StandardCharsets;
24+
2425
import org.junit.Test;
2526

2627
public class ConsumingInputStreamTest {
2728

2829
@Test
2930
public void testClose_drainsBytesOnClose() throws IOException {
3031
MockInputStream mockInputStream =
31-
new MockInputStream("abc123".getBytes(StandardCharsets.UTF_8));
32+
new MockInputStream("abc123".getBytes(Charsets.UTF_8));
3233
InputStream consumingInputStream = new ConsumingInputStream(mockInputStream);
3334

3435
assertEquals(6, mockInputStream.getBytesToRead());

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.api.client.testing.util.LogRecordingHandler;
2323
import com.google.api.client.testing.util.TestableByteArrayInputStream;
2424
import com.google.api.client.util.Key;
25+
import com.google.api.client.util.Charsets;
2526
import com.google.common.io.ByteStreams;
2627
import java.io.BufferedInputStream;
2728
import java.io.ByteArrayInputStream;
@@ -30,7 +31,6 @@
3031
import java.io.IOException;
3132
import java.io.InputStream;
3233
import java.lang.reflect.Type;
33-
import java.nio.charset.StandardCharsets;
3434
import java.text.NumberFormat;
3535
import java.util.Arrays;
3636
import java.util.Locale;
@@ -632,7 +632,7 @@ public void testGetContent_gzipEncoding_finishReadingWithUppercaseContentEncodin
632632

633633
private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding)
634634
throws IOException {
635-
byte[] dataToCompress = "abcd".getBytes(StandardCharsets.UTF_8);
635+
byte[] dataToCompress = "abcd".getBytes(Charsets.UTF_8);
636636
byte[] mockBytes;
637637
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length);
638638
GZIPOutputStream zipStream = new GZIPOutputStream((byteStream))) {
@@ -713,7 +713,7 @@ public LowLevelHttpRequest buildRequest(String method, String url) throws IOExce
713713
public LowLevelHttpResponse execute() throws IOException {
714714
// have to use gzip here because MockLowLevelHttpResponse.setContent()
715715
// returns BufferedStream by itself, so test always success
716-
byte[] dataToCompress = ERROR_SAMPLE.getBytes(StandardCharsets.UTF_8);
716+
byte[] dataToCompress = ERROR_SAMPLE.getBytes(Charsets.UTF_8);
717717
ByteArrayOutputStream content = new ByteArrayOutputStream(dataToCompress.length);
718718
try (GZIPOutputStream zipStream = new GZIPOutputStream((content))) {
719719
zipStream.write(dataToCompress);

google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpRequestTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import com.google.api.client.http.javanet.NetHttpRequest.OutputWriter;
1010
import com.google.api.client.testing.http.HttpTesting;
1111
import com.google.api.client.testing.http.javanet.MockHttpURLConnection;
12+
import com.google.api.client.util.Charsets;
1213
import com.google.api.client.util.StreamingContent;
1314
import java.io.IOException;
1415
import java.io.InputStream;
1516
import java.io.OutputStream;
1617
import java.net.URL;
17-
import java.nio.charset.StandardCharsets;
1818
import java.util.concurrent.TimeoutException;
1919
import org.junit.Test;
2020

@@ -225,7 +225,7 @@ public void testChunkedLengthNotSet() throws Exception {
225225
connection.setRequestMethod("POST");
226226
NetHttpRequest request = new NetHttpRequest(connection);
227227
HttpContent content =
228-
new ByteArrayContent("text/plain", "sample".getBytes(StandardCharsets.UTF_8));
228+
new ByteArrayContent("text/plain", "sample".getBytes(Charsets.UTF_8));
229229
request.setStreamingContent(content);
230230
request.setContentLength(content.getLength());
231231
request.execute();

google-http-client/src/test/java/com/google/api/client/util/Base64Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package com.google.api.client.util;
1616

17-
import java.nio.charset.StandardCharsets;
17+
import com.google.api.client.util.Charsets;
1818
import junit.framework.TestCase;
1919

2020
/**
@@ -26,19 +26,19 @@ public class Base64Test extends TestCase {
2626

2727
public void test_decodeBase64_withPadding() {
2828
String encoded = "Zm9vOmJhcg==";
29-
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
29+
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), Charsets.UTF_8));
3030
}
3131

3232
public void test_decodeBase64_withoutPadding() {
3333
String encoded = "Zm9vOmJhcg";
34-
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
34+
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), Charsets.UTF_8));
3535
}
3636

3737
public void test_decodeBase64_withTrailingWhitespace() {
3838
// Some internal use cases append extra space characters that apache-commons base64 decoding
3939
// previously handled.
4040
String encoded = "Zm9vOmJhcg==\r\n";
41-
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), StandardCharsets.UTF_8));
41+
assertEquals("foo:bar", new String(Base64.decodeBase64(encoded), Charsets.UTF_8));
4242
}
4343

4444
public void test_decodeBase64_withNullBytes_shouldReturnNull() {

0 commit comments

Comments
 (0)