From 05cad263ba46884ad339d4237547c381f485a1c6 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 8 Mar 2022 16:46:13 -0800 Subject: [PATCH] Fix `DM_DEFAULT_ENCODING` SpotBugs violations (#79) --- .../org/kohsuke/file_leak_detector/AgentMain.java | 13 +++++++------ .../org/kohsuke/file_leak_detector/Listener.java | 5 +++-- src/spotbugs/spotbugs-excludes.xml | 13 ------------- .../kohsuke/file_leak_detector/TransformerTest.java | 7 +++++-- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/kohsuke/file_leak_detector/AgentMain.java b/src/main/java/org/kohsuke/file_leak_detector/AgentMain.java index 923c8da..5b71f02 100644 --- a/src/main/java/org/kohsuke/file_leak_detector/AgentMain.java +++ b/src/main/java/org/kohsuke/file_leak_detector/AgentMain.java @@ -5,7 +5,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -21,10 +20,12 @@ import java.nio.channels.spi.AbstractInterruptibleChannel; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.AbstractSelector; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -74,7 +75,7 @@ public static void premain(String agentArguments, Instrumentation instrumentatio Listener.THRESHOLD = Integer.parseInt(t.substring(t.indexOf('=')+1)); } else if(t.equals("trace")) { - Listener.TRACE = new PrintWriter(System.err); + Listener.TRACE = new PrintWriter(new OutputStreamWriter(System.err, Charset.defaultCharset())); } else if(t.equals("strong")) { Listener.makeStrong(); @@ -83,10 +84,10 @@ public static void premain(String agentArguments, Instrumentation instrumentatio serverPort = Integer.parseInt(t.substring(t.indexOf('=')+1)); } else if(t.startsWith("trace=")) { - Listener.TRACE = new PrintWriter(new FileOutputStream(t.substring(6))); + Listener.TRACE = new PrintWriter(new OutputStreamWriter(new FileOutputStream(t.substring(6)), StandardCharsets.UTF_8)); } else if(t.startsWith("error=")) { - Listener.ERROR = new PrintWriter(new FileOutputStream(t.substring(6))); + Listener.ERROR = new PrintWriter(new OutputStreamWriter(new FileOutputStream(t.substring(6)), StandardCharsets.UTF_8)); } else if(t.startsWith("listener=")) { ActivityListener.LIST.add((ActivityListener) AgentMain.class.getClassLoader().loadClass(t.substring(9)).newInstance()); @@ -100,7 +101,7 @@ public void run() { }); } else if(t.startsWith("excludes=")) { - try (BufferedReader reader = new BufferedReader(new FileReader(t.substring(9)))) { + try (BufferedReader reader = Files.newBufferedReader(Paths.get(t.substring(9)), StandardCharsets.UTF_8)) { while (true) { String line = reader.readLine(); if(line == null) { @@ -193,7 +194,7 @@ public Object call() throws Exception { @Override public Void call() throws Exception { try { - BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8)); // Read the request line (and ignore it) in.readLine(); diff --git a/src/main/java/org/kohsuke/file_leak_detector/Listener.java b/src/main/java/org/kohsuke/file_leak_detector/Listener.java index 170fcfc..20bb5a0 100644 --- a/src/main/java/org/kohsuke/file_leak_detector/Listener.java +++ b/src/main/java/org/kohsuke/file_leak_detector/Listener.java @@ -18,6 +18,7 @@ import java.nio.channels.SeekableByteChannel; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Path; import java.util.ArrayList; @@ -250,7 +251,7 @@ public void dump(String prefix, PrintWriter ps) { /** * Trace the "too many open files" error here */ - public static PrintWriter ERROR = new PrintWriter(System.err); + public static PrintWriter ERROR = new PrintWriter(new OutputStreamWriter(System.err, Charset.defaultCharset())); /** * Allows to provide stacktrace-lines which cause the element to be excluded @@ -426,7 +427,7 @@ public static synchronized void close(Object _this) { * Dumps all files that are currently open. */ public static synchronized void dump(OutputStream out) { - dump(new OutputStreamWriter(out)); + dump(new OutputStreamWriter(out, Charset.defaultCharset())); } public static synchronized void dump(Writer w) { PrintWriter pw = new PrintWriter(w); diff --git a/src/spotbugs/spotbugs-excludes.xml b/src/spotbugs/spotbugs-excludes.xml index 567e0a2..552dabf 100644 --- a/src/spotbugs/spotbugs-excludes.xml +++ b/src/spotbugs/spotbugs-excludes.xml @@ -17,19 +17,6 @@ this section. - If it is not a false positive, fix the bug, then remove the exclusion from this section. --> - - - - - - - - - - - - - diff --git a/src/test/java/org/kohsuke/file_leak_detector/TransformerTest.java b/src/test/java/org/kohsuke/file_leak_detector/TransformerTest.java index c81dd79..4c4c6b3 100644 --- a/src/test/java/org/kohsuke/file_leak_detector/TransformerTest.java +++ b/src/test/java/org/kohsuke/file_leak_detector/TransformerTest.java @@ -5,6 +5,7 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -50,8 +51,10 @@ public void testInstrumentations() throws Exception { final String errors; ClassReader classReader = new ClassReader(data2); - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - CheckClassAdapter.verify(classReader, false, new PrintWriter(baos)); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.UTF_8); + PrintWriter pw = new PrintWriter(osw)) { + CheckClassAdapter.verify(classReader, false, pw); errors = new String(baos.toByteArray(), StandardCharsets.UTF_8); } assertTrue("Verification failed for " + c + "\n" + errors, errors.isEmpty());