Consistently use character encoding constants

Use the java.nio.charset.StandardCharsets.{ISO_8859_1,UTF_8} constants'
name() methods instead of hard-coding the strings.

Where possible, use method variants that take a Charset rather than
a String. This removes the need to catch UnsupportedEncodingException
in some cases.

Change-Id: I4ac1ba0a753de715e1f38ce631842f527b9e127c
This commit is contained in:
David Pursehouse
2015-10-08 15:46:47 +09:00
parent 852022af90
commit 19c63fa311
49 changed files with 170 additions and 148 deletions

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.extensions.restapi;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
@@ -23,7 +25,6 @@ import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
/**
@@ -187,7 +188,7 @@ public abstract class BinaryResult implements Closeable {
try {
Charset cs = enc != null
? Charset.forName(enc)
: StandardCharsets.UTF_8;
: UTF_8;
return cs.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
@@ -226,9 +227,9 @@ public abstract class BinaryResult implements Closeable {
private final String str;
StringResult(String str) {
super(str.getBytes(StandardCharsets.UTF_8));
super(str.getBytes(UTF_8));
setContentType("text/plain");
setCharacterEncoding("UTF-8");
setCharacterEncoding(UTF_8.name());
this.str = str;
}

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.extensions.restapi;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
@@ -40,7 +42,7 @@ public final class Url {
public static String encode(String component) {
if (component != null) {
try {
return URLEncoder.encode(component, "UTF-8");
return URLEncoder.encode(component, UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("JVM must support UTF-8", e);
}
@@ -52,7 +54,7 @@ public final class Url {
public static String decode(String str) {
if (str != null) {
try {
return URLDecoder.decode(str, "UTF-8");
return URLDecoder.decode(str, UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("JVM must support UTF-8", e);
}