aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/CharsetDetector.java11
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/CharsetDetectorTest.java1
2 files changed, 7 insertions, 5 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/CharsetDetector.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/CharsetDetector.java
index ae6aa5532da..3586ea9f171 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/CharsetDetector.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/CharsetDetector.java
@@ -33,8 +33,6 @@ import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Set;
-import javax.annotation.CheckForNull;
-
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
@@ -58,13 +56,17 @@ public class CharsetDetector {
return true;
}
- return detectCharset();
+ if (detectCharset()) {
+ return true;
+ }
+
+ detectedCharset = defaultEncoding;
+ return false;
} catch (IOException e) {
throw new IllegalStateException("Unable to read file " + filePath.toAbsolutePath().toString(), e);
}
}
- @CheckForNull
public Charset charset() {
assertRun();
return detectedCharset;
@@ -90,7 +92,6 @@ public class CharsetDetector {
return false;
}
- @CheckForNull
private boolean detectCharset() throws IOException {
stream.mark(BYTES_TO_DECODE);
byte[] buf = new byte[BYTES_TO_DECODE];
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/CharsetDetectorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/CharsetDetectorTest.java
index 167e362201c..5abdb43764a 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/CharsetDetectorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/CharsetDetectorTest.java
@@ -85,6 +85,7 @@ public class CharsetDetectorTest {
CharsetDetector detector = new CharsetDetector(filePath, StandardCharsets.UTF_8);
assertThat(detector.run()).isFalse();
+ assertThat(detector.charset()).isEqualTo(StandardCharsets.UTF_8);
}
private Charset detectCharset(Path file, Charset defaultEncoding) {