Skip to content
Permalink
Browse files
fix: refactor to use StandardCharsets (#1243)
  • Loading branch information
elharo committed Feb 23, 2021
1 parent fa3966a commit 03ec798d7637ff454614415be7b324cd8dc7c77c
@@ -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.
@@ -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();
}

@@ -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;
@@ -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;
@@ -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();
}
}
@@ -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;
@@ -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;
@@ -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)}.
@@ -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.
@@ -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();
}
@@ -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;
@@ -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());
}
@@ -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;

/**
@@ -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());
}

@@ -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

0 comments on commit 03ec798

Please sign in to comment.