BinaryResult: Use Charset for setCharacterEncoding

Instead of using a String which is then converted to a Charset, use
the Charset directly.

Change-Id: I4e481286d5554b0f550c68992c266c06a9239340
This commit is contained in:
David Pursehouse
2015-10-17 00:02:27 +09:00
parent 746ca2e4d4
commit a1d9f76ea6
5 changed files with 18 additions and 12 deletions

View File

@@ -59,7 +59,7 @@ public abstract class BinaryResult implements Closeable {
}
private String contentType = OCTET_STREAM;
private String characterEncoding;
private Charset characterEncoding;
private long contentLength = -1;
private boolean gzip = true;
private boolean base64 = false;
@@ -67,9 +67,9 @@ public abstract class BinaryResult implements Closeable {
/** @return the MIME type of the result, for HTTP clients. */
public String getContentType() {
String enc = getCharacterEncoding();
Charset enc = getCharacterEncoding();
if (enc != null) {
return contentType + "; charset=" + enc;
return contentType + "; charset=" + enc.name();
}
return contentType;
}
@@ -81,12 +81,18 @@ public abstract class BinaryResult implements Closeable {
}
/** Get the character encoding; null if not known. */
public String getCharacterEncoding() {
public Charset getCharacterEncoding() {
return characterEncoding;
}
/** Set the character set used to encode text data and return {@code this}. */
@Deprecated
public BinaryResult setCharacterEncoding(String encoding) {
return setCharacterEncoding(Charset.forName(encoding));
}
/** Set the character set used to encode text data and return {@code this}. */
public BinaryResult setCharacterEncoding(Charset encoding) {
characterEncoding = encoding;
return this;
}
@@ -184,10 +190,10 @@ public abstract class BinaryResult implements Closeable {
getContentType());
}
private static String decode(byte[] data, String enc) {
private static String decode(byte[] data, Charset enc) {
try {
Charset cs = enc != null
? Charset.forName(enc)
? enc
: UTF_8;
return cs.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)

View File

@@ -669,7 +669,7 @@ public class RestApiServlet extends HttpServlet {
w.flush();
replyBinaryResult(req, res, asBinaryResult(buf)
.setContentType(JSON_TYPE)
.setCharacterEncoding(UTF_8.name()));
.setCharacterEncoding(UTF_8));
}
private static Gson newGson(Multimap<String, String> config,
@@ -791,7 +791,7 @@ public class RestApiServlet extends HttpServlet {
res.setHeader("X-FYI-Content-Type", src.getContentType());
return asBinaryResult(buf)
.setContentType(JSON_TYPE)
.setCharacterEncoding(UTF_8.name());
.setCharacterEncoding(UTF_8);
}
private static BinaryResult stackBase64(HttpServletResponse res,
@@ -819,7 +819,7 @@ public class RestApiServlet extends HttpServlet {
}
res.setHeader("X-FYI-Content-Encoding", "base64");
res.setHeader("X-FYI-Content-Type", src.getContentType());
return b64.setContentType("text/plain").setCharacterEncoding(ISO_8859_1.name());
return b64.setContentType("text/plain").setCharacterEncoding(ISO_8859_1);
}
private static BinaryResult stackGzip(HttpServletResponse res,

View File

@@ -78,7 +78,7 @@ public class ListCaches implements RestReadView<ConfigResource> {
return BinaryResult.create(Joiner.on('\n').join(cacheNames))
.base64()
.setContentType("text/plain")
.setCharacterEncoding(UTF_8.name());
.setCharacterEncoding(UTF_8);
} else {
return cacheNames;
}

View File

@@ -100,7 +100,7 @@ public class GarbageCollect implements RestModifyView<ProjectResource, Input>,
}
}
}.setContentType("text/plain")
.setCharacterEncoding(UTF_8.name())
.setCharacterEncoding(UTF_8)
.disableGzip();
}

View File

@@ -236,7 +236,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
display(buf);
return BinaryResult.create(buf.toByteArray())
.setContentType("text/plain")
.setCharacterEncoding(UTF_8.name());
.setCharacterEncoding(UTF_8);
}
return apply();
}