]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8327 Support access input file's contents through VFS 1339/head
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 27 Oct 2016 10:10:31 +0000 (12:10 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 27 Oct 2016 14:06:49 +0000 (16:06 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java

index dafc0c8ad790bac3425e8690f8e006cbcb7493f7..450dd704416baaacacab602ea85b944fad3a988a 100644 (file)
 package org.sonar.api.batch.fs;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.Charset;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import javax.annotation.CheckForNull;
 import org.sonar.api.batch.fs.internal.DefaultInputFile;
@@ -99,6 +102,25 @@ public interface InputFile extends InputPath {
    */
   Type type();
 
+  /**
+   * Creates a stream of the file's contents. Depending on the runtime context, the source might be a file in a physical or virtual filesystem.
+   * Typically, it won't be buffered. <b>The stream must be closed by the caller</b>.
+   * Note that there is a default implementation.
+   * @since 6.2
+   */
+  default InputStream inputStream() throws IOException {
+    return Files.newInputStream(path());
+  }
+
+  /**
+   * Fetches the entire contents of the file, decoding with the {@link #charset}.
+   * Note that there is a default implementation.
+   * @since 6.2
+   */
+  default String contents() throws IOException {
+    return new String(Files.readAllBytes(path()), charset());
+  }
+
   /**
    * Status regarding previous analysis
    */
index da9ead4cba783cf6cfbf3409696109a1e5f74c2c..5fd96320fec09f7ddc63835500ab10751cdcd9db 100644 (file)
  */
 package org.sonar.api.batch.fs.internal;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.stream.Collectors;
+
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
@@ -58,6 +65,28 @@ public class DefaultInputFileTest {
     assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1);
   }
 
+  @Test
+  public void test_content() throws IOException {
+    Path baseDir = temp.newFolder().toPath();
+    Path testFile = baseDir.resolve("src").resolve("Foo.php");
+    Files.createDirectories(testFile.getParent());
+    Files.write(testFile, "test string".getBytes(StandardCharsets.UTF_8));
+    DefaultInputFile inputFile = new DefaultInputFile("ABCDE", "src/Foo.php")
+      .setModuleBaseDir(baseDir)
+      .setLines(42)
+      .setLanguage("php")
+      .setStatus(InputFile.Status.ADDED)
+      .setType(InputFile.Type.TEST)
+      .setCharset(StandardCharsets.ISO_8859_1);
+
+    assertThat(inputFile.contents()).isEqualTo("test string");
+    try (InputStream inputStream = inputFile.inputStream()) {
+      String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining());
+      assertThat(result).isEqualTo("test string");
+    }
+
+  }
+
   @Test
   public void test_equals_and_hashcode() throws Exception {
     DefaultInputFile f1 = new DefaultInputFile("ABCDE", "src/Foo.php");