Use standard charset constants whenever possible

This commit is contained in:
Daniel Gredler
2018-08-27 15:10:53 -04:00
parent cdde1c773c
commit 0a89e31cde
6 changed files with 18 additions and 20 deletions
@@ -19,6 +19,7 @@ import com.threerings.getdown.util.Config;
import com.threerings.getdown.util.ProgressObserver; import com.threerings.getdown.util.ProgressObserver;
import static com.threerings.getdown.Log.log; import static com.threerings.getdown.Log.log;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Manages the <code>digest.txt</code> file and the computing and processing of digests for an * Manages the <code>digest.txt</code> file and the computing and processing of digests for an
@@ -111,7 +112,7 @@ public class Digest
} }
// finally compute and append the digest for the file contents // finally compute and append the digest for the file contents
MessageDigest md = getMessageDigest(version); MessageDigest md = getMessageDigest(version);
byte[] contents = data.toString().getBytes("UTF-8"); byte[] contents = data.toString().getBytes(UTF_8);
String filename = digestFile(version); String filename = digestFile(version);
pout.println(filename + " = " + StringUtil.hexlate(md.digest(contents))); pout.println(filename + " = " + StringUtil.hexlate(md.digest(contents)));
} }
@@ -168,7 +169,7 @@ public class Digest
// we've reached the end, validate our contents // we've reached the end, validate our contents
MessageDigest md = getMessageDigest(version); MessageDigest md = getMessageDigest(version);
byte[] contents = data.toString().getBytes("UTF-8"); byte[] contents = data.toString().getBytes(UTF_8);
String hash = StringUtil.hexlate(md.digest(contents)); String hash = StringUtil.hexlate(md.digest(contents));
if (!hash.equals(_metaDigest)) { if (!hash.equals(_metaDigest)) {
String err = MessageUtil.tcompose("m.invalid_digest_file", _metaDigest, hash); String err = MessageUtil.tcompose("m.invalid_digest_file", _metaDigest, hash);
@@ -23,6 +23,8 @@ import com.threerings.getdown.data.Digest;
import com.threerings.getdown.data.Resource; import com.threerings.getdown.data.Resource;
import com.threerings.getdown.util.Base64; import com.threerings.getdown.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Handles the generation of the digest.txt file. * Handles the generation of the digest.txt file.
*/ */
@@ -120,7 +122,7 @@ public class Digester
// Write out the signature // Write out the signature
String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT); String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT);
signatureOutput.write(signed.getBytes("utf8")); signatureOutput.write(signed.getBytes(UTF_8));
} }
} }
} }
@@ -45,6 +45,8 @@ import java.io.*;
import java.util.*; import java.util.*;
import java.util.jar.*; import java.util.jar.*;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* JarDiff is able to create a jar file containing the delta between two jar files (old and new). * JarDiff is able to create a jar file containing the delta between two jar files (old and new).
* The delta jar file can then be applied to the old jar file to reconstruct the new jar file. * The delta jar file can then be applied to the old jar file to reconstruct the new jar file.
@@ -229,7 +231,7 @@ public class JarDiff implements JarDiffCodes
} }
jos.putNextEntry(new JarEntry(INDEX_NAME)); jos.putNextEntry(new JarEntry(INDEX_NAME));
byte[] bytes = writer.toString().getBytes("UTF-8"); byte[] bytes = writer.toString().getBytes(UTF_8);
jos.write(bytes, 0, bytes.length); jos.write(bytes, 0, bytes.length);
} }
@@ -27,6 +27,8 @@ import java.util.jar.JarOutputStream;
import com.threerings.getdown.util.ProgressObserver; import com.threerings.getdown.util.ProgressObserver;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Applies a jardiff patch to a jar file. * Applies a jardiff patch to a jar file.
*/ */
@@ -178,7 +180,7 @@ public class JarDiffPatcher implements JarDiffCodes
} }
LineNumberReader indexReader = LineNumberReader indexReader =
new LineNumberReader(new InputStreamReader(is, "UTF-8")); new LineNumberReader(new InputStreamReader(is, UTF_8));
String line = indexReader.readLine(); String line = indexReader.readLine();
if (line == null || !line.equals(VERSION_HEADER)) { if (line == null || !line.equals(VERSION_HEADER)) {
throw new IOException("jardiff.error.badheader: " + line); throw new IOException("jardiff.error.badheader: " + line);
@@ -16,7 +16,7 @@
package com.threerings.getdown.util; package com.threerings.getdown.util;
import java.io.UnsupportedEncodingException; import static java.nio.charset.StandardCharsets.US_ASCII;
/** /**
* Utilities for encoding and decoding the Base64 representation of * Utilities for encoding and decoding the Base64 representation of
@@ -452,12 +452,7 @@ public class Base64 {
* adheres to RFC 2045. * adheres to RFC 2045.
*/ */
public static String encodeToString(byte[] input, int flags) { public static String encodeToString(byte[] input, int flags) {
try { return new String(encode(input, flags), US_ASCII);
return new String(encode(input, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
} }
/** /**
@@ -473,12 +468,7 @@ public class Base64 {
* adheres to RFC 2045. * adheres to RFC 2045.
*/ */
public static String encodeToString(byte[] input, int offset, int len, int flags) { public static String encodeToString(byte[] input, int offset, int len, int flags) {
try { return new String(encode(input, offset, len, flags), US_ASCII);
return new String(encode(input, offset, len, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
} }
/** /**
@@ -10,10 +10,11 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLDecoder; import java.net.URLDecoder;
import com.threerings.getdown.data.SysProps; import com.threerings.getdown.data.SysProps;
import com.threerings.getdown.util.Base64; import com.threerings.getdown.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ConnectionUtil public class ConnectionUtil
{ {
/** /**
@@ -49,7 +50,7 @@ public class ConnectionUtil
// Remove any percent-encoding in the username/password // Remove any percent-encoding in the username/password
userInfo = URLDecoder.decode(userInfo, "UTF-8"); userInfo = URLDecoder.decode(userInfo, "UTF-8");
// Now base64 encode the auth info and make it a single line // Now base64 encode the auth info and make it a single line
String encoded = Base64.encodeToString(userInfo.getBytes("UTF-8"), Base64.DEFAULT). String encoded = Base64.encodeToString(userInfo.getBytes(UTF_8), Base64.DEFAULT).
replaceAll("\\n","").replaceAll("\\r", ""); replaceAll("\\n","").replaceAll("\\r", "");
conn.setRequestProperty("Authorization", "Basic " + encoded); conn.setRequestProperty("Authorization", "Basic " + encoded);
} }