Skip to content

Commit

Permalink
Fix DM_DEFAULT_ENCODING SpotBugs violations (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Mar 9, 2022
1 parent a38b111 commit 05cad26
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
13 changes: 7 additions & 6 deletions src/main/java/org/kohsuke/file_leak_detector/AgentMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand All @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/kohsuke/file_leak_detector/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions src/spotbugs/spotbugs-excludes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
this section.
- If it is not a false positive, fix the bug, then remove the exclusion from this section.
-->
<Match>
<Confidence value="1"/>
<Or>
<And>
<Bug pattern="DM_DEFAULT_ENCODING"/>
<Or>
<Class name="org.kohsuke.file_leak_detector.AgentMain"/>
<Class name="org.kohsuke.file_leak_detector.AgentMain$3$1"/>
<Class name="org.kohsuke.file_leak_detector.Listener"/>
</Or>
</And>
</Or>
</Match>
<Match>
<Confidence value="2"/>
<Or>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 05cad26

Please sign in to comment.