提交 c0815b50 编写于 作者: J Josh Soref

Use StandardCharsets

上级 a43efeab
......@@ -25,6 +25,7 @@ package hudson;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
......@@ -97,7 +98,7 @@ public class Main {
// check for authentication info
String auth = new URL(home).getUserInfo();
if(auth != null) auth = "Basic " + new Base64Encoder().encode(auth.getBytes("UTF-8"));
if(auth != null) auth = "Basic " + new Base64Encoder().encode(auth.getBytes(StandardCharsets.UTF_8));
{// check if the home is set correctly
HttpURLConnection con = open(new URL(home));
......@@ -143,7 +144,7 @@ public class Main {
try {
int ret;
try (OutputStream os = Files.newOutputStream(tmpFile.toPath());
Writer w = new OutputStreamWriter(os,"UTF-8")) {
Writer w = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
w.write("<?xml version='1.1' encoding='UTF-8'?>");
w.write("<run><log encoding='hexBinary' content-encoding='"+Charset.defaultCharset().name()+"'>");
w.flush();
......
......@@ -28,6 +28,7 @@ import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPublicKey;
import javax.annotation.Nullable;
......@@ -236,7 +237,7 @@ public final class TcpSlaveAgentListener extends Thread {
DataInputStream in = new DataInputStream(s.getInputStream());
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"UTF-8")),
new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8)),
true); // DEPRECATED: newer protocol shouldn't use PrintWriter but should use DataOutputStream
// peek the first few bytes to determine what to do with this client
......@@ -291,7 +292,7 @@ public final class TcpSlaveAgentListener extends Thread {
*/
private void respondHello(String header, Socket s) throws IOException {
try {
Writer o = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
Writer o = new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8);
//TODO: expose version about minimum supported Remoting version (JENKINS-48766)
if (header.startsWith("GET / ")) {
......@@ -358,11 +359,7 @@ public final class TcpSlaveAgentListener extends Thread {
private final byte[] ping;
public PingAgentProtocol() {
try {
ping = "Ping\n".getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("JLS mandates support for UTF-8 charset", e);
}
ping = "Ping\n".getBytes(StandardCharsets.UTF_8);
}
/**
......@@ -414,9 +411,9 @@ public final class TcpSlaveAgentListener extends Thread {
} else {
LOGGER.log(Level.FINE, "Expected ping response from {0} of {1} got {2}", new Object[]{
socket.getRemoteSocketAddress(),
new String(ping, "UTF-8"),
new String(ping, StandardCharsets.UTF_8),
responseLength > 0 && responseLength <= response.length ?
new String(response, 0, responseLength, "UTF-8") :
new String(response, 0, responseLength, StandardCharsets.UTF_8) :
"bad response length " + responseLength
});
return false;
......
......@@ -37,6 +37,7 @@ import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.channels.ClosedByInterruptException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -99,7 +100,7 @@ public class UDPBroadcastThread extends Thread {
rsp.append("</hudson>");
byte[] response = rsp.toString().getBytes("UTF-8");
byte[] response = rsp.toString().getBytes(StandardCharsets.UTF_8);
mcs.send(new DatagramPacket(response,response.length,sender));
}
} catch (ClosedByInterruptException e) {
......
......@@ -50,6 +50,7 @@ import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -188,7 +189,7 @@ public class Api extends AbstractModelObject {
// simple output allowed
rsp.setContentType("text/plain;charset=UTF-8");
String text = result instanceof CharacterData ? ((CharacterData) result).getText() : result.toString();
o.write(text.getBytes("UTF-8"));
o.write(text.getBytes(StandardCharsets.UTF_8));
return;
}
......
......@@ -31,6 +31,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -244,7 +245,7 @@ public final class DirectoryBrowserSupport implements HttpResponse {
rsp.setContentType("text/plain;charset=UTF-8");
try (OutputStream os = rsp.getOutputStream()) {
for (VirtualFile kid : baseFile.list()) {
os.write(kid.getName().getBytes("UTF-8"));
os.write(kid.getName().getBytes(StandardCharsets.UTF_8));
if (kid.isDirectory()) {
os.write('/');
}
......
......@@ -49,6 +49,7 @@ import java.io.OutputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.DataInputStream;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyFactory;
......@@ -181,7 +182,7 @@ public class UsageStatistics extends PageDecorator implements PersistentDescript
// json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string
try (OutputStream cipheros = new CombinedCipherOutputStream(baos,getKey(),"AES");
OutputStream zipos = new GZIPOutputStream(cipheros);
OutputStreamWriter w = new OutputStreamWriter(zipos, "UTF-8")) {
OutputStreamWriter w = new OutputStreamWriter(zipos, StandardCharsets.UTF_8)) {
o.write(w);
}
......
......@@ -99,6 +99,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
......@@ -1206,7 +1207,7 @@ public abstract class View extends AbstractModelObject implements AccessControll
}
// try to reflect the changes by reloading
try (InputStream in = new BufferedInputStream(new ByteArrayInputStream(out.toString().getBytes("UTF-8")))){
try (InputStream in = new BufferedInputStream(new ByteArrayInputStream(out.toString().getBytes(StandardCharsets.UTF_8)))){
// Do not allow overwriting view name as it might collide with another
// view in same ViewGroup and might not satisfy Jenkins.checkGoodName.
String oldname = name;
......
......@@ -30,6 +30,7 @@ import org.apache.tools.tar.TarEntry;
import java.io.FilterOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* The TarOutputStream writes a UNIX tar archive as an OutputStream.
......@@ -186,7 +187,7 @@ public class TarOutputStream extends FilterOutputStream {
TarEntry longLinkEntry = new TarEntry(TarConstants.GNU_LONGLINK,
TarConstants.LF_GNUTYPE_LONGNAME);
byte[] name = entry.getName().getBytes("UTF-8");
byte[] name = entry.getName().getBytes(StandardCharsets.UTF_8);
longLinkEntry.setSize(name.length + 1);
putNextEntry(longLinkEntry);
write(name);
......
......@@ -24,6 +24,7 @@
package hudson.scheduler;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
......@@ -55,7 +56,7 @@ public abstract class Hash {
public static Hash from(String seed) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(seed.getBytes("UTF-8"));
md5.update(seed.getBytes(StandardCharsets.UTF_8));
byte[] digest = md5.digest();
for (int i=8; i<digest.length; i++)
......@@ -74,8 +75,6 @@ public abstract class Hash {
};
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e); // MD5 is a part of JRE
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // UTF-8 is mandatory
}
}
......
......@@ -24,6 +24,7 @@ import hudson.model.Descriptor;
import hudson.util.MultipartFormDataParser;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletException;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
......@@ -216,7 +217,7 @@ public abstract class CrumbIssuer implements Describable<CrumbIssuer>, Extension
if (text != null) {
try (OutputStream o = rsp.getCompressedOutputStream(req)) {
rsp.setContentType("text/plain;charset=UTF-8");
o.write(text.getBytes("UTF-8"));
o.write(text.getBytes(StandardCharsets.UTF_8));
}
} else {
super.doXml(req, rsp, xpath, wrapper, tree, depth);
......
......@@ -33,6 +33,7 @@ package hudson.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
......@@ -266,7 +267,7 @@ public class ChunkedInputStream extends InputStream {
}
//parse data
String dataString = new String(baos.toByteArray(),"US-ASCII");
String dataString = new String(baos.toByteArray(), StandardCharsets.US_ASCII);
int separator = dataString.indexOf(';');
dataString = (separator > 0)
? dataString.substring(0, separator).trim()
......
......@@ -32,6 +32,7 @@ package hudson.util;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
/**
* Implements HTTP chunking support. Writes are buffered to an internal buffer (2048 default size).
......@@ -96,7 +97,7 @@ public class ChunkedOutputStream extends OutputStream {
*/
protected void flushCache() throws IOException {
if (cachePosition > 0) {
byte[] chunkHeader = (Integer.toHexString(cachePosition) + "\r\n").getBytes("US-ASCII");
byte[] chunkHeader = (Integer.toHexString(cachePosition) + "\r\n").getBytes(StandardCharsets.US_ASCII);
stream.write(chunkHeader, 0, chunkHeader.length);
stream.write(cache, 0, cachePosition);
stream.write(ENDCHUNK, 0, ENDCHUNK.length);
......@@ -115,7 +116,7 @@ public class ChunkedOutputStream extends OutputStream {
* @since 3.0
*/
protected void flushCacheWithAppend(byte[] bufferToAppend, int off, int len) throws IOException {
byte[] chunkHeader = (Integer.toHexString(cachePosition + len) + "\r\n").getBytes("US-ASCII");
byte[] chunkHeader = (Integer.toHexString(cachePosition + len) + "\r\n").getBytes(StandardCharsets.US_ASCII);
stream.write(chunkHeader, 0, chunkHeader.length);
stream.write(cache, 0, cachePosition);
stream.write(bufferToAppend, off, len);
......
......@@ -88,7 +88,7 @@ public class SecretRewriter {
boolean modified = false; // did we actually change anything?
try (PrintWriter out = new PrintWriter(new BufferedWriter(w))) {
try (InputStream fin = Files.newInputStream(f.toPath())) {
BufferedReader r = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
BufferedReader r = new BufferedReader(new InputStreamReader(fin, StandardCharsets.UTF_8));
String line;
StringBuilder buf = new StringBuilder();
......
......@@ -27,6 +27,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
......@@ -50,7 +51,7 @@ public class Service {
final Enumeration<URL> e = classLoader.getResources("META-INF/services/"+type.getName());
while (e.hasMoreElements()) {
URL url = e.nextElement();
try (BufferedReader configFile = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
try (BufferedReader configFile = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = configFile.readLine()) != null) {
line = line.trim();
......@@ -84,7 +85,7 @@ public class Service {
Enumeration<URL> e = cl.getResources("META-INF/services/" + spi.getName());
while(e.hasMoreElements()) {
final URL url = e.nextElement();
try (BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
try (BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = r.readLine()) != null) {
if (line.startsWith("#"))
......
......@@ -18,6 +18,7 @@ package hudson.util.jna;
import com.sun.jna.ptr.IntByReference;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.TreeMap;
import java.util.TreeSet;
......@@ -65,11 +66,7 @@ public class RegistryKey {
* @return String
*/
private static String convertBufferToString(byte[] buf) {
try {
return new String(buf, 0, buf.length - 2, "UTF-16LE");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // impossible
}
return new String(buf, 0, buf.length - 2, StandardCharsets.UTF_16LE);
}
/**
......@@ -128,15 +125,11 @@ public class RegistryKey {
* Writes a String value.
*/
public void setValue(String name, String value) {
try {
byte[] bytes = value.getBytes("UTF-16LE");
int newLength = bytes.length+2; // for 0 padding
byte[] with0 = new byte[newLength];
System.arraycopy(bytes, 0, with0, 0, newLength);
check(Advapi32.INSTANCE.RegSetValueEx(handle, name, 0, WINNT.REG_SZ, with0, with0.length));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] bytes = value.getBytes(StandardCharsets.UTF_16LE);
int newLength = bytes.length+2; // for 0 padding
byte[] with0 = new byte[newLength];
System.arraycopy(bytes, 0, with0, 0, newLength);
check(Advapi32.INSTANCE.RegSetValueEx(handle, name, 0, WINNT.REG_SZ, with0, with0.length));
}
/**
......
......@@ -46,6 +46,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Set;
/**
......@@ -110,7 +111,7 @@ public class PluginSubtypeMarker extends AbstractProcessor {
private void write(TypeElement c) throws IOException {
FileObject f = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
"", "META-INF/services/hudson.Plugin");
try (Writer w = new OutputStreamWriter(f.openOutputStream(), "UTF-8")) {
try (Writer w = new OutputStreamWriter(f.openOutputStream(), StandardCharsets.UTF_8)) {
w.write(c.getQualifiedName().toString());
}
}
......
......@@ -8,6 +8,7 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
......@@ -84,11 +85,7 @@ public class HMACConfidentialKey extends ConfidentialKey {
* While redundant, often convenient.
*/
public String mac(String message) {
try {
return Util.toHexString(mac(message.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return Util.toHexString(mac(message.getBytes(StandardCharsets.UTF_8)));
}
/**
......
......@@ -24,6 +24,7 @@
package jenkins.security;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
......@@ -50,12 +51,10 @@ public class RSADigitalSignatureConfidentialKey extends RSAConfidentialKey {
RSAPrivateKey key = getPrivateKey();
Signature sig = Signature.getInstance(SIGNING_ALGORITHM + "with" + key.getAlgorithm());
sig.initSign(key);
sig.update(msg.getBytes("UTF-8"));
sig.update(msg.getBytes(StandardCharsets.UTF_8));
return hudson.remoting.Base64.encode(sig.sign());
} catch (GeneralSecurityException e) {
throw new SecurityException(e);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // UTF-8 is mandatory
}
}
......
......@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.logging.Level;
......@@ -91,7 +92,7 @@ public class EncryptedSlaveAgentJnlpFile implements HttpResponse {
if(it instanceof SlaveComputer) {
jnlpMac = Util.fromHexString(((SlaveComputer)it).getJnlpMac());
} else {
jnlpMac = JnlpSlaveAgentProtocol.SLAVE_SECRET.mac(slaveName.getBytes("UTF-8"));
jnlpMac = JnlpSlaveAgentProtocol.SLAVE_SECRET.mac(slaveName.getBytes(StandardCharsets.UTF_8));
}
SecretKey key = new SecretKeySpec(jnlpMac, 0, /* export restrictions */ 128 / 8, "AES");
byte[] encrypted;
......
......@@ -3,6 +3,7 @@ package jenkins.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/**
* Filtering {@link OutputStream} that looks for {@link #MARK} in the output stream and notifies the callback.
......@@ -109,10 +110,6 @@ public abstract class MarkFindingOutputStream extends OutputStream {
private static final byte[] MBYTES = toUTF8(MARK);
private static byte[] toUTF8(String s) {
try {
return s.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return s.getBytes(StandardCharsets.UTF_8);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册