diff --git a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java index eceff79eff3c..0553dd215de5 100644 --- a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java +++ b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java @@ -1,5 +1,6 @@ package hudson.cli; +import java.nio.charset.StandardCharsets; import java.util.Base64; /** @@ -33,7 +34,7 @@ public CLIConnectionFactory basicAuth(String username, String password) { * Cf. {@code BasicHeaderApiTokenAuthenticator}. */ public CLIConnectionFactory basicAuth(String userInfo) { - return authorization("Basic " + Base64.getEncoder().encodeToString(userInfo.getBytes())); + return authorization("Basic " + Base64.getEncoder().encodeToString(userInfo.getBytes(StandardCharsets.UTF_8))); } /** diff --git a/cli/src/main/java/hudson/cli/PrivateKeyProvider.java b/cli/src/main/java/hudson/cli/PrivateKeyProvider.java index 1ab1eff33049..f283053f9cb5 100644 --- a/cli/src/main/java/hudson/cli/PrivateKeyProvider.java +++ b/cli/src/main/java/hudson/cli/PrivateKeyProvider.java @@ -23,7 +23,6 @@ */ package hudson.cli; -import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.logging.Level.FINE; import java.io.ByteArrayInputStream; @@ -32,6 +31,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Paths; @@ -134,7 +134,7 @@ private static String readPemFile(File f) throws IOException{ DataInputStream dis = new DataInputStream(is)) { byte[] bytes = new byte[(int) f.length()]; dis.readFully(bytes); - return new String(bytes); + return new String(bytes, StandardCharsets.UTF_8); } catch (InvalidPathException e) { throw new IOException(e); } @@ -143,7 +143,7 @@ private static String readPemFile(File f) throws IOException{ public static KeyPair loadKey(String pemString, String passwd) throws IOException, GeneralSecurityException { Iterable itr = SecurityUtils.loadKeyPairIdentities(null, new PathResource(Paths.get("key")), - new ByteArrayInputStream(pemString.getBytes(UTF_8)), + new ByteArrayInputStream(pemString.getBytes(StandardCharsets.UTF_8)), FilePasswordProvider.of(passwd)); long numLoaded = itr == null ? 0 : StreamSupport.stream(itr.spliterator(), false).count(); if (numLoaded <= 0) { diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 16e060f9d23c..cadb8cc257e9 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -86,6 +86,7 @@ import java.net.URI; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.Charset; import java.nio.file.FileSystemException; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -2279,7 +2280,7 @@ private static class ReadToString extends MasterToSlaveFileCallable { private static final long serialVersionUID = 1L; @Override public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { - return new String(Files.readAllBytes(fileToPath(f))); + return new String(Files.readAllBytes(fileToPath(f)), Charset.defaultCharset()); } } @@ -2336,7 +2337,7 @@ private static class Write extends MasterToSlaveFileCallable { public Void invoke(File f, VirtualChannel channel) throws IOException { mkdirs(f.getParentFile()); try (OutputStream fos = Files.newOutputStream(fileToPath(f)); - Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos)) { + Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos, Charset.defaultCharset())) { w.write(content); } return null; @@ -3611,5 +3612,4 @@ public boolean accept(File file) { } private static final long serialVersionUID = 1L; } - } diff --git a/core/src/main/java/hudson/Proc.java b/core/src/main/java/hudson/Proc.java index dd94d2097c39..24cd16552295 100644 --- a/core/src/main/java/hudson/Proc.java +++ b/core/src/main/java/hudson/Proc.java @@ -39,6 +39,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; import java.util.Locale; import java.util.Map; import java.util.concurrent.CancellationException; @@ -354,7 +355,7 @@ public int join() throws InterruptedException, IOException { // } catch (IOException x) { // LOGGER.log(Level.FINE,"stderr termination failed",x); // } - out.write(msg.getBytes()); + out.write(msg.getBytes(Charset.defaultCharset())); out.write('\n'); } return r; diff --git a/core/src/main/java/hudson/WebAppMain.java b/core/src/main/java/hudson/WebAppMain.java index f3becef4932f..b0596a66374d 100644 --- a/core/src/main/java/hudson/WebAppMain.java +++ b/core/src/main/java/hudson/WebAppMain.java @@ -50,6 +50,7 @@ import java.io.OutputStream; import java.net.URL; import java.net.URLClassLoader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.StandardOpenOption; @@ -293,7 +294,7 @@ public void joinInit() throws InterruptedException { */ private void recordBootAttempt(File home) { try (OutputStream o=Files.newOutputStream(BootFailure.getBootFailureFile(home).toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { - o.write((new Date() + System.getProperty("line.separator", "\n")).getBytes()); + o.write((new Date() + System.getProperty("line.separator", "\n")).getBytes(Charset.defaultCharset())); } catch (IOException | InvalidPathException e) { LOGGER.log(WARNING, "Failed to record boot attempts",e); } diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index 198736dc9fca..d27d0b6beff5 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; import java.nio.charset.Charset; @@ -404,7 +405,11 @@ protected void printUsage(PrintStream stderr, CmdLineParser p) { public final String getSingleLineSummary() { ByteArrayOutputStream out = new ByteArrayOutputStream(); getCmdLineParser().printSingleLineUsage(out); - return out.toString(); + try { + return out.toString(getClientCharset().name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** @@ -414,7 +419,11 @@ public final String getSingleLineSummary() { public final String getUsage() { ByteArrayOutputStream out = new ByteArrayOutputStream(); getCmdLineParser().printUsage(out); - return out.toString(); + try { + return out.toString(getClientCharset().name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** @@ -427,7 +436,11 @@ public final String getLongDescription() { printUsageSummary(ps); ps.close(); - return out.toString(); + try { + return out.toString(getClientCharset().name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** @@ -457,7 +470,7 @@ public void setClientCharset(@NonNull Charset encoding) { this.encoding = encoding; } - protected @NonNull Charset getClientCharset() throws IOException, InterruptedException { + protected @NonNull Charset getClientCharset() { if (encoding != null) { return encoding; } diff --git a/core/src/main/java/hudson/console/AnnotatedLargeText.java b/core/src/main/java/hudson/console/AnnotatedLargeText.java index af70227ad1a5..dafff632d305 100644 --- a/core/src/main/java/hudson/console/AnnotatedLargeText.java +++ b/core/src/main/java/hudson/console/AnnotatedLargeText.java @@ -186,7 +186,7 @@ public long writeHtmlTo(long start, Writer w) throws IOException { oos.close(); StaplerResponse rsp = Stapler.getCurrentResponse(); if (rsp!=null) - rsp.setHeader("X-ConsoleAnnotator", new String(Base64.getEncoder().encode(baos.toByteArray()))); + rsp.setHeader("X-ConsoleAnnotator", Base64.getEncoder().encodeToString(baos.toByteArray())); return r; } diff --git a/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java b/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java index 00a84b6f4612..6e70a743d824 100644 --- a/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java +++ b/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java @@ -121,7 +121,7 @@ public ConsoleAnnotator annotate(T context, MarkupText text) { } } catch (IOException | ClassNotFoundException e) { // if we failed to resurrect an annotation, ignore it. - LOGGER.log(Level.FINE, "Failed to resurrect annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest)) + "\"", e); + LOGGER.log(Level.FINE, "Failed to resurrect annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest, Charset.defaultCharset())) + "\"", e); } int bytesUsed = rest - b.available(); // bytes consumed by annotations diff --git a/core/src/main/java/hudson/console/ConsoleNote.java b/core/src/main/java/hudson/console/ConsoleNote.java index ee7896895dec..62a9349c02c0 100644 --- a/core/src/main/java/hudson/console/ConsoleNote.java +++ b/core/src/main/java/hudson/console/ConsoleNote.java @@ -43,6 +43,7 @@ import java.io.OutputStream; import java.io.Serializable; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; @@ -326,12 +327,12 @@ public static void skip(DataInputStream in) throws IOException { * plus a few magic characters. */ @SuppressFBWarnings(value = "MS_PKGPROTECT", justification = "used in several plugins") - public static final byte[] PREAMBLE = PREAMBLE_STR.getBytes(); + public static final byte[] PREAMBLE = PREAMBLE_STR.getBytes(StandardCharsets.UTF_8); /** * Post amble is the ANSI escape sequence that brings back the echo. */ @SuppressFBWarnings(value = "MS_PKGPROTECT", justification = "used in several plugins") - public static final byte[] POSTAMBLE = POSTAMBLE_STR.getBytes(); + public static final byte[] POSTAMBLE = POSTAMBLE_STR.getBytes(StandardCharsets.UTF_8); /** * Locates the preamble in the given buffer. diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 0a7f1cb68323..add476c3cea7 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -65,6 +65,7 @@ import java.io.IOException; import java.io.InterruptedIOException; import java.lang.ref.WeakReference; +import java.nio.channels.ClosedByInterruptException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; @@ -668,7 +669,7 @@ public void defaultCheckout() throws IOException, InterruptedException { } } catch (AbortException e) { listener.error(e.getMessage()); - } catch (InterruptedIOException e) { + } catch (ClosedByInterruptException | InterruptedIOException e) { throw (InterruptedException)new InterruptedException().initCause(e); } catch (IOException e) { // checkout error not yet reported diff --git a/core/src/main/java/hudson/model/FileParameterValue.java b/core/src/main/java/hudson/model/FileParameterValue.java index b3e5c146cd9d..cc9f39faefee 100644 --- a/core/src/main/java/hudson/model/FileParameterValue.java +++ b/core/src/main/java/hudson/model/FileParameterValue.java @@ -36,6 +36,7 @@ import java.io.OutputStream; import java.io.UncheckedIOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.regex.Pattern; import jenkins.util.SystemProperties; @@ -302,7 +303,7 @@ public String getString(String encoding) throws UnsupportedEncodingException { @Override public String getString() { - return new String(get()); + return new String(get(), Charset.defaultCharset()); } @Override diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index e331a9e0c172..33673abfcdc3 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -1509,11 +1509,11 @@ public Collection getBuildFingerprints() { } String message = "No such file: " + logFile; - return new ByteArrayInputStream(charset != null ? message.getBytes(charset) : message.getBytes()); + return new ByteArrayInputStream(charset != null ? message.getBytes(charset) : message.getBytes(Charset.defaultCharset())); } public @NonNull Reader getLogReader() throws IOException { - if (charset==null) return new InputStreamReader(getLogInputStream()); + if (charset==null) return new InputStreamReader(getLogInputStream(),Charset.defaultCharset()); else return new InputStreamReader(getLogInputStream(),charset); } diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 081e4e4cc816..e3b0dcb40f77 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -68,6 +68,7 @@ import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; import java.nio.file.InvalidPathException; @@ -2025,11 +2026,11 @@ private static VerificationResult verifyChecksums(String expectedDigest, String } if (caseSensitive) { - if (MessageDigest.isEqual(expectedDigest.getBytes(), actualDigest.getBytes())) { + if (MessageDigest.isEqual(expectedDigest.getBytes(StandardCharsets.US_ASCII), actualDigest.getBytes(StandardCharsets.US_ASCII))) { return VerificationResult.PASS; } } else { - if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(), actualDigest.toLowerCase().getBytes())) { + if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(StandardCharsets.US_ASCII), actualDigest.toLowerCase().getBytes(StandardCharsets.US_ASCII))) { return VerificationResult.PASS; } } diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index bc292db26c36..a62031bfc406 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -193,7 +193,7 @@ public String getStatData() throws IOException { o.write(w); } - return new String(Base64.getEncoder().encode(baos.toByteArray())); + return Base64.getEncoder().encodeToString(baos.toByteArray()); } catch (Throwable e) { // the exception could be GeneralSecurityException, InvalidParameterException or any other depending on the security provider you have installed LOG.log(Level.INFO, "Usage statistics could not be sent ({0})", e.getMessage()); LOG.log(Level.FINE, "Error sending usage statistics", e); diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 7b94b07e7dd5..1e2fdde83ec8 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -52,8 +52,10 @@ import hudson.security.PermissionScope; import hudson.tasks.Builder; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -739,7 +741,7 @@ protected final boolean createEmptyChangeLog(File changelogFile, BuildListener l * @since 1.568 */ protected final void createEmptyChangeLog(@NonNull File changelogFile, @NonNull TaskListener listener, @NonNull String rootTag) throws IOException { - try (FileWriter w = new FileWriter(changelogFile)) { + try (Writer w = Files.newBufferedWriter(Util.fileToPath(changelogFile), Charset.defaultCharset())) { w.write("<"+rootTag +"/>"); } } diff --git a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java index 229c7974b521..3acab22bf065 100644 --- a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java +++ b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java @@ -31,6 +31,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -100,7 +101,7 @@ public void commence(HttpServletRequest req, HttpServletResponse rsp, Authentica PrintWriter out; try { - out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream())); + out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream(), StandardCharsets.UTF_8)); } catch (IllegalStateException e) { out = rsp.getWriter(); } diff --git a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java index e625759f37fd..a5ec4a26ad72 100644 --- a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java +++ b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java @@ -83,8 +83,8 @@ protected synchronized String issueCrumb(ServletRequest request, String salt) { buffer.append(req.getSession().getId()); } - md.update(buffer.toString().getBytes()); - return Util.toHexString(md.digest(salt.getBytes())); + md.update(buffer.toString().getBytes(StandardCharsets.UTF_8)); + return Util.toHexString(md.digest(salt.getBytes(StandardCharsets.US_ASCII))); } } return null; diff --git a/core/src/main/java/hudson/util/BootFailure.java b/core/src/main/java/hudson/util/BootFailure.java index 23dff92dcd84..0de96ea8bd1b 100644 --- a/core/src/main/java/hudson/util/BootFailure.java +++ b/core/src/main/java/hudson/util/BootFailure.java @@ -4,8 +4,10 @@ import hudson.WebAppMain; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; @@ -60,7 +62,7 @@ protected List loadAttempts(File home) { File f = getBootFailureFile(home); try { if (f.exists()) { - try (BufferedReader failureFileReader = new BufferedReader(new FileReader(f))) { + try (BufferedReader failureFileReader = Files.newBufferedReader(f.toPath(), Charset.defaultCharset())) { String line; DateFormat df = DateFormat.getDateInstance(); while ((line=failureFileReader.readLine())!=null) { @@ -72,7 +74,7 @@ protected List loadAttempts(File home) { } } } - } catch (IOException e) { + } catch (IOException | InvalidPathException e) { LOGGER.log(Level.WARNING,"Failed to parse "+f,e); } } diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 48add2d49f10..648972716144 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -34,6 +34,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; @@ -101,7 +102,9 @@ public InputStream read() throws IOException { /** * Loads the file content as a string. + * @deprecated removed without replacement */ + @Deprecated public String loadAsString() throws IOException { long sizeGuess; if(file.exists()) @@ -115,7 +118,7 @@ public String loadAsString() throws IOException { StringBuilder str = new StringBuilder((int)sizeGuess); try (InputStream is = read(); - Reader r = new InputStreamReader(is)) { + Reader r = new InputStreamReader(is, Charset.defaultCharset())) { char[] buf = new char[8192]; int len; while((len=r.read(buf,0,buf.length))>0) diff --git a/core/src/main/java/hudson/util/ConsistentHash.java b/core/src/main/java/hudson/util/ConsistentHash.java index 76c7c8b872f3..3a2d983f3c0c 100644 --- a/core/src/main/java/hudson/util/ConsistentHash.java +++ b/core/src/main/java/hudson/util/ConsistentHash.java @@ -24,6 +24,7 @@ package hudson.util; import hudson.util.Iterators.DuplicateFilterIterator; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -304,7 +305,7 @@ private synchronized void refreshTable() { private int digest(String s) { try { MessageDigest messageDigest = createMessageDigest(); - messageDigest.update(s.getBytes()); + messageDigest.update(s.getBytes(StandardCharsets.UTF_8)); byte[] digest = messageDigest.digest(); // 16 bytes -> 4 bytes diff --git a/core/src/main/java/hudson/util/IOUtils.java b/core/src/main/java/hudson/util/IOUtils.java index c0b92536ea76..e2eab5dd4514 100644 --- a/core/src/main/java/hudson/util/IOUtils.java +++ b/core/src/main/java/hudson/util/IOUtils.java @@ -17,6 +17,7 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.util.Collection; @@ -148,7 +149,7 @@ public static int mode(File f) throws PosixException { */ public static String readFirstLine(InputStream is, String encoding) throws IOException { try (BufferedReader reader = new BufferedReader( - encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding))) { + encoding == null ? new InputStreamReader(is, Charset.defaultCharset()) : new InputStreamReader(is, encoding))) { return reader.readLine(); } } diff --git a/core/src/main/java/hudson/util/LogTaskListener.java b/core/src/main/java/hudson/util/LogTaskListener.java index 33b2ca2318ad..7d696ada4632 100644 --- a/core/src/main/java/hudson/util/LogTaskListener.java +++ b/core/src/main/java/hudson/util/LogTaskListener.java @@ -31,6 +31,8 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -86,7 +88,12 @@ public void write(int b) throws IOException { @Override public void flush() throws IOException { if (baos.size() > 0) { - LogRecord lr = new LogRecord(level, baos.toString()); + LogRecord lr; + try { + lr = new LogRecord(level, baos.toString(StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } lr.setLoggerName(logger.getName()); lr.setSourceClassName(caller.getClassName()); lr.setSourceMethodName(caller.getMethodName()); diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index 4344cc4f42f9..f9d140f51ce5 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -50,15 +50,17 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.ObjectStreamException; import java.io.RandomAccessFile; import java.io.Serializable; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -983,7 +985,7 @@ class LinuxProcess extends UnixProcess { LinuxProcess(int pid) throws IOException { super(pid); - try (BufferedReader r = new BufferedReader(new FileReader(getFile("status")))) { + try (BufferedReader r = Files.newBufferedReader(Util.fileToPath(getFile("status")), StandardCharsets.UTF_8)) { String line; while((line=r.readLine())!=null) { line=line.toLowerCase(Locale.ENGLISH); @@ -1015,7 +1017,7 @@ public synchronized List getArguments() { for (int i = 0; i < cmdline.length; i++) { byte b = cmdline[i]; if(b==0) { - arguments.add(new String(cmdline,pos,i-pos)); + arguments.add(new String(cmdline,pos,i-pos, StandardCharsets.UTF_8)); pos=i+1; } } @@ -1039,7 +1041,7 @@ public synchronized EnvVars getEnvironmentVariables() { for (int i = 0; i < environ.length; i++) { byte b = environ[i]; if(b==0) { - envVars.addLine(new String(environ,pos,i-pos)); + envVars.addLine(new String(environ,pos,i-pos, StandardCharsets.UTF_8)); pos=i+1; } } @@ -1345,7 +1347,12 @@ private String readLine(int fd, long addr, String prefix) throws IOException { buf.write(ch); addr++; } - String line = buf.toString(); + String line; + try { + line = buf.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } if(LOGGER.isLoggable(FINEST)) LOGGER.finest(prefix+" was "+line); return line; @@ -1616,7 +1623,12 @@ private String readLine(int fd, long addr, String prefix) throws IOException { buf.write(ch); addr++; } - String line = buf.toString(); + String line; + try { + line = buf.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } if(LOGGER.isLoggable(FINEST)) LOGGER.finest(prefix+" was "+line); return line; @@ -1779,13 +1791,16 @@ byte peek() { return getByte(offset); } - @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "TODO needs triage") String readString() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte ch; while(offset < length && (ch = getByte(offset++))!='\0') baos.write(ch); - return baos.toString(); + try { + return baos.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } void skip0() { @@ -2098,7 +2113,11 @@ private void parse(Memory m, NativeLong size, Consumer consumer) { while ((ch = m.getByte(offset++)) != '\0') { baos.write(ch); } - consumer.accept(baos.toString()); + try { + consumer.accept(baos.toString(StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } baos.reset(); } } diff --git a/core/src/main/java/hudson/util/Scrambler.java b/core/src/main/java/hudson/util/Scrambler.java index 374a1e9ba21d..283a80347bad 100644 --- a/core/src/main/java/hudson/util/Scrambler.java +++ b/core/src/main/java/hudson/util/Scrambler.java @@ -40,7 +40,7 @@ public class Scrambler { public static String scramble(String secret) { if(secret==null) return null; - return new String(Base64.getEncoder().encode(secret.getBytes(StandardCharsets.UTF_8))); + return Base64.getEncoder().encodeToString(secret.getBytes(StandardCharsets.UTF_8)); } public static String descramble(String scrambled) { diff --git a/core/src/main/java/hudson/util/Secret.java b/core/src/main/java/hudson/util/Secret.java index 45385af68e0d..6da075b331a5 100644 --- a/core/src/main/java/hudson/util/Secret.java +++ b/core/src/main/java/hudson/util/Secret.java @@ -148,7 +148,7 @@ public String getEncryptedValue() { System.arraycopy(iv, 0, payload, pos, iv.length); pos+=iv.length; System.arraycopy(encrypted, 0, payload, pos, encrypted.length); - return "{"+new String(Base64.getEncoder().encode(payload))+"}"; + return "{" + Base64.getEncoder().encodeToString(payload) + "}"; } catch (GeneralSecurityException e) { throw new Error(e); // impossible } diff --git a/core/src/main/java/jenkins/security/DefaultConfidentialStore.java b/core/src/main/java/jenkins/security/DefaultConfidentialStore.java index d24bc837dd4a..49513a02ef60 100644 --- a/core/src/main/java/jenkins/security/DefaultConfidentialStore.java +++ b/core/src/main/java/jenkins/security/DefaultConfidentialStore.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.security.GeneralSecurityException; @@ -151,5 +152,5 @@ public byte[] randomBytes(int size) { return random; } - private static final byte[] MAGIC = "::::MAGIC::::".getBytes(); + private static final byte[] MAGIC = "::::MAGIC::::".getBytes(StandardCharsets.US_ASCII); } diff --git a/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java b/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java index d0114c85fa8b..8b7aeeefbdf5 100644 --- a/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java +++ b/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java @@ -13,6 +13,7 @@ import hudson.Extension; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.logging.Logger; import jenkins.util.JavaVMArguments; @@ -107,7 +108,7 @@ private static String resolveSymlink(File link) throws IOException { byte[] buf = new byte[r]; m.read(0, buf, 0, r); - return new String(buf); + return new String(buf, StandardCharsets.UTF_8); } throw new IOException("Failed to readlink " + link); diff --git a/core/src/main/java/jenkins/util/JSONSignatureValidator.java b/core/src/main/java/jenkins/util/JSONSignatureValidator.java index 1c932f0dfa22..a6bd08c91a42 100644 --- a/core/src/main/java/jenkins/util/JSONSignatureValidator.java +++ b/core/src/main/java/jenkins/util/JSONSignatureValidator.java @@ -241,7 +241,7 @@ private boolean verifySignature(Signature signature, String providedSignature) { * Utility method supporting both possible digest formats: Base64 and Hex */ private boolean digestMatches(byte[] digest, String providedDigest) { - return providedDigest.equalsIgnoreCase(Hex.encodeHexString(digest)) || providedDigest.equalsIgnoreCase(new String(Base64.getEncoder().encode(digest))); + return providedDigest.equalsIgnoreCase(Hex.encodeHexString(digest)) || providedDigest.equalsIgnoreCase(Base64.getEncoder().encodeToString(digest)); } diff --git a/src/spotbugs/spotbugs-excludes.xml b/src/spotbugs/spotbugs-excludes.xml index 978042fce847..9f5927b041b1 100644 --- a/src/spotbugs/spotbugs-excludes.xml +++ b/src/spotbugs/spotbugs-excludes.xml @@ -51,48 +51,16 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -