aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2017-04-12 11:08:12 +0200
committerdbmeneses <duarte.meneses@sonarsource.com>2017-04-12 15:02:55 +0200
commitd2e84d6611de4066cfddc69b6d29d9c09993b6b2 (patch)
tree38150c6d779a199a0e9215d9db6b56fc60a9cd87 /sonar-scanner-engine
parentdf9e95d9da11cafb5d894b589a80c614b654a9b1 (diff)
downloadsonarqube-d2e84d6611de4066cfddc69b6d29d9c09993b6b2.tar.gz
sonarqube-d2e84d6611de4066cfddc69b6d29d9c09993b6b2.zip
Fix NPE in charset detection
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) {