Skip to content

Commit

Permalink
Fix DM_DEFAULT_ENCODING SpotBugs violations (jenkinsci#6050)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Dec 23, 2021
1 parent a5f13e6 commit 8a5e309
Show file tree
Hide file tree
Showing 29 changed files with 108 additions and 82 deletions.
3 changes: 2 additions & 1 deletion cli/src/main/java/hudson/cli/CLIConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.cli;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
Expand Down Expand Up @@ -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)));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions cli/src/main/java/hudson/cli/PrivateKeyProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -143,7 +143,7 @@ private static String readPemFile(File f) throws IOException{
public static KeyPair loadKey(String pemString, String passwd) throws IOException, GeneralSecurityException {
Iterable<KeyPair> 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) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2279,7 +2280,7 @@ private static class ReadToString extends MasterToSlaveFileCallable<String> {
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());
}
}

Expand Down Expand Up @@ -2336,7 +2337,7 @@ private static class Write extends MasterToSlaveFileCallable<Void> {
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;
Expand Down Expand Up @@ -3611,5 +3612,4 @@ public boolean accept(File file) {
}
private static final long serialVersionUID = 1L;
}

}
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/Proc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/WebAppMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 17 additions & 4 deletions core/src/main/java/hudson/cli/CLICommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/console/AnnotatedLargeText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public ConsoleAnnotator<T> 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
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/hudson/console/ConsoleNote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/AbstractBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/FileParameterValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/model/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -1509,11 +1509,11 @@ public Collection<Fingerprint> 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);
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/hudson/model/UpdateCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/UsageStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/hudson/scm/SCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 +"/>");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions core/src/main/java/hudson/util/BootFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +62,7 @@ protected List<Date> 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) {
Expand All @@ -72,7 +74,7 @@ protected List<Date> loadAttempts(File home) {
}
}
}
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
LOGGER.log(Level.WARNING,"Failed to parse "+f,e);
}
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/util/CompressedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 8a5e309

Please sign in to comment.