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.sshd;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.util.concurrent.Atomics;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.TimeUtil;
@@ -51,13 +53,13 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
public abstract class BaseCommand implements Command {
private static final Logger log = LoggerFactory.getLogger(BaseCommand.class);
public static final String ENC = "UTF-8";
public static final Charset ENC = UTF_8;
private static final int PRIVATE_STATUS = 1 << 30;
static final int STATUS_CANCEL = PRIVATE_STATUS | 1;
@@ -309,14 +311,7 @@ public abstract class BaseCommand implements Command {
/** Wrap the supplied output stream in a UTF-8 encoded PrintWriter. */
protected static PrintWriter toPrintWriter(final OutputStream o) {
try {
return new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, ENC)));
} catch (UnsupportedEncodingException e) {
// Our default encoding is required by the specifications for the
// runtime APIs, this should never, ever happen.
//
throw new RuntimeException("JVM lacks " + ENC + " encoding", e);
}
return new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, ENC)));
}
private int handleError(final Throwable e) {

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.sshd;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Preconditions;
@@ -193,7 +194,7 @@ class DatabasePubKeyAuth implements PublickeyAuthenticator {
}
try {
byte[] bin = Base64.decodeBase64(line.getBytes("ISO-8859-1"));
byte[] bin = Base64.decodeBase64(line.getBytes(ISO_8859_1));
keys.add(new Buffer(bin).getRawPublicKey());
} catch (RuntimeException | SshException e) {
logBadKey(path, line, e);

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.sshd;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.util.concurrent.Atomics;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.CurrentUser;
@@ -102,7 +104,7 @@ public final class SuExec extends BaseCommand {
if (!msg.endsWith("\n")) {
msg += "\n";
}
err.write(msg.getBytes("UTF-8"));
err.write(msg.getBytes(UTF_8));
err.flush();
onExit(1);
}

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.sshd.commands;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.gerrit.common.data.GlobalCapability;
@@ -33,7 +35,6 @@ import org.kohsuke.args4j.Option;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@@ -82,14 +83,14 @@ final class CreateAccountCommand extends SshCommand {
}
}
private String readSshKey() throws UnsupportedEncodingException, IOException {
private String readSshKey() throws IOException {
if (sshKey == null) {
return null;
}
if ("-".equals(sshKey)) {
sshKey = "";
BufferedReader br =
new BufferedReader(new InputStreamReader(in, "UTF-8"));
new BufferedReader(new InputStreamReader(in, UTF_8));
String line;
while ((line = br.readLine()) != null) {
sshKey += line + "\n";

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.sshd.commands;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.common.Version;
import com.google.gerrit.reviewdb.server.ReviewDb;
@@ -32,7 +34,6 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
@@ -65,11 +66,10 @@ public class QueryShell {
@Inject
QueryShell(final SchemaFactory<ReviewDb> dbFactory,
@Assisted final InputStream in, @Assisted final OutputStream out)
throws UnsupportedEncodingException {
@Assisted final InputStream in, @Assisted final OutputStream out) {
this.dbFactory = dbFactory;
this.in = new BufferedReader(new InputStreamReader(in, "UTF-8"));
this.out = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
this.in = new BufferedReader(new InputStreamReader(in, UTF_8));
this.out = new PrintWriter(new OutputStreamWriter(out, UTF_8));
}
public void setOutputFormat(OutputFormat fmt) {

View File

@@ -22,6 +22,8 @@
*/
package com.google.gerrit.sshd.commands;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.server.tools.ToolsCatalog;
import com.google.gerrit.server.tools.ToolsCatalog.Entry;
import com.google.gerrit.sshd.BaseCommand;
@@ -188,7 +190,7 @@ final class ScpCommand extends BaseCommand {
}
}
out.write("E\n".getBytes("UTF-8"));
out.write("E\n".getBytes(UTF_8));
out.flush();
readAck();
}
@@ -210,7 +212,7 @@ final class ScpCommand extends BaseCommand {
buf.append(" ");
buf.append(dir.getName());
buf.append("\n");
out.write(buf.toString().getBytes("UTF-8"));
out.write(buf.toString().getBytes(UTF_8));
out.flush();
}

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.sshd.commands;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Strings;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.errors.EmailException;
@@ -226,7 +228,7 @@ final class SetAccountCommand extends SshCommand {
in.raw = new RawInput() {
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(sshKey.getBytes("UTF-8"));
return new ByteArrayInputStream(sshKey.getBytes(UTF_8));
}
@Override
@@ -312,7 +314,7 @@ final class SetAccountCommand extends SshCommand {
if (idx >= 0) {
StringBuilder sshKey = new StringBuilder();
BufferedReader br =
new BufferedReader(new InputStreamReader(in, "UTF-8"));
new BufferedReader(new InputStreamReader(in, UTF_8));
String line;
while ((line = br.readLine()) != null) {
sshKey.append(line)

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.sshd.commands;
import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.common.EventListener;
import com.google.gerrit.common.EventSource;
@@ -125,7 +126,7 @@ final class StreamEvents extends BaseCommand {
if (!msg.endsWith("\n")) {
msg += "\n";
}
err.write(msg.getBytes("UTF-8"));
err.write(msg.getBytes(UTF_8));
err.flush();
onExit(1);
return;