Skip to content

Commit

Permalink
fix: refactor to use StandardCharsets (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Feb 23, 2021
1 parent fa3966a commit 03ec798
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
Expand Up @@ -14,11 +14,11 @@

package com.google.api.client.http;

import com.google.api.client.util.Charsets;
import com.google.api.client.util.IOUtils;
import com.google.api.client.util.StreamingContent;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Abstract implementation of an HTTP content with typical options.
Expand Down Expand Up @@ -87,13 +87,13 @@ public AbstractHttpContent setMediaType(HttpMediaType mediaType) {
}

/**
* Returns the charset specified in the media type or {@code Charsets#UTF_8} if not specified.
* Returns the charset specified in the media type or ISO_8859_1 if not specified.
*
* @since 1.10
*/
protected final Charset getCharset() {
return mediaType == null || mediaType.getCharsetParameter() == null
? Charsets.ISO_8859_1
? StandardCharsets.ISO_8859_1
: mediaType.getCharsetParameter();
}

Expand Down
Expand Up @@ -14,7 +14,6 @@

package com.google.api.client.http;

import com.google.api.client.util.Charsets;
import com.google.api.client.util.IOUtils;
import com.google.api.client.util.LoggingInputStream;
import com.google.api.client.util.Preconditions;
Expand All @@ -26,6 +25,7 @@
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -506,14 +506,14 @@ public String parseAsString() throws IOException {
}

/**
* Returns the {@link Charset} specified in the Content-Type of this response or the {@code
* "ISO-8859-1"} charset as a default.
* Returns the {@link Charset} specified in the Content-Type of this response or the ISO-8859-1
* charset as a default.
*
* @since 1.10
*/
public Charset getContentCharset() {
return mediaType == null || mediaType.getCharsetParameter() == null
? Charsets.ISO_8859_1
? StandardCharsets.ISO_8859_1
: mediaType.getCharsetParameter();
}
}
Expand Up @@ -15,7 +15,6 @@
package com.google.api.client.http;

import com.google.api.client.util.ArrayValueMap;
import com.google.api.client.util.Charsets;
import com.google.api.client.util.ClassInfo;
import com.google.api.client.util.Data;
import com.google.api.client.util.FieldInfo;
Expand All @@ -34,6 +33,7 @@
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -72,7 +72,10 @@ public class UrlEncodedParser implements ObjectParser {
* @since 1.13
*/
public static final String MEDIA_TYPE =
new HttpMediaType(UrlEncodedParser.CONTENT_TYPE).setCharsetParameter(Charsets.UTF_8).build();
new HttpMediaType(UrlEncodedParser.CONTENT_TYPE)
.setCharsetParameter(StandardCharsets.UTF_8)
.build();

/**
* Parses the given URL-encoded content into the given data object of data key name/value pairs
* using {@link #parse(Reader, Object)}.
Expand Down
Expand Up @@ -14,14 +14,14 @@

package com.google.api.client.json;

import com.google.api.client.util.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Abstract low-level JSON factory.
Expand Down Expand Up @@ -159,7 +159,7 @@ private String toString(Object item, boolean pretty) throws IOException {
*/
private ByteArrayOutputStream toByteStream(Object item, boolean pretty) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
JsonGenerator generator = createJsonGenerator(byteStream, Charsets.UTF_8);
JsonGenerator generator = createJsonGenerator(byteStream, StandardCharsets.UTF_8);
if (pretty) {
generator.enablePrettyPrint();
}
Expand Down
Expand Up @@ -18,13 +18,13 @@
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.util.Beta;
import com.google.api.client.util.Charsets;
import com.google.api.client.util.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -164,7 +164,7 @@ public String getContentAsString() throws IOException {
HttpMediaType mediaType = contentType != null ? new HttpMediaType(contentType) : null;
Charset charset =
mediaType == null || mediaType.getCharsetParameter() == null
? Charsets.ISO_8859_1
? StandardCharsets.ISO_8859_1
: mediaType.getCharsetParameter();
return out.toString(charset.name());
}
Expand Down
Expand Up @@ -15,9 +15,9 @@
package com.google.api.client.http;

import com.google.api.client.json.Json;
import com.google.api.client.util.Charsets;
import com.google.api.client.util.StringUtils;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ public void testRandomContent() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
content.writeTo(out);
String expectedContent = expectedStringBuilder.toString();
assertEquals(expectedContent, out.toString(Charsets.UTF_8.name()));
assertEquals(expectedContent, out.toString(StandardCharsets.UTF_8.name()));
assertEquals(StringUtils.getBytesUtf8(expectedContent).length, content.getLength());
}

Expand Down Expand Up @@ -128,7 +128,7 @@ private void subtestContent(String expectedContent, String boundaryString, Strin
// write to string
ByteArrayOutputStream out = new ByteArrayOutputStream();
content.writeTo(out);
assertEquals(expectedContent, out.toString(Charsets.UTF_8.name()));
assertEquals(expectedContent, out.toString(StandardCharsets.UTF_8.name()));
assertEquals(StringUtils.getBytesUtf8(expectedContent).length, content.getLength());
assertEquals(
boundaryString == null
Expand Down

0 comments on commit 03ec798

Please sign in to comment.