Use common library to read file content.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 8 Jun 2020 18:05:18 +0000 (21:05 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 8 Jun 2020 18:05:18 +0000 (21:05 +0300)
src/main/java/eu/svjatoslav/inspector/java/methods/JavaFile.java

index 09469f8..70d5dc2 100755 (executable)
@@ -13,10 +13,12 @@ import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
 
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import static eu.svjatoslav.commons.file.IOHelper.getFileContentsAsString;
 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
 
@@ -24,7 +26,7 @@ public class JavaFile {
 
     public static final String UTF_8 = "UTF-8";
     public final List<Clazz> classes = new ArrayList<>();
-    final StringBuffer contents = new StringBuffer();
+    String contents;
     private final List<Import> imports = new ArrayList<>();
     private final File file;
     private String packageName;
@@ -37,9 +39,9 @@ public class JavaFile {
     public void parse() throws IOException, InvalidSyntaxException {
         System.out.println("java file: " + file);
 
-        readFile();
+        contents = getFileContentsAsString(file);
 
-        final Tokenizer tokenizer = new Tokenizer(contents.toString());
+        final Tokenizer tokenizer = new Tokenizer(contents);
 
         // empty space
         tokenizer.addTerminator(" ", DROP);
@@ -154,25 +156,6 @@ public class JavaFile {
         tokenizer.expectAndConsumeNextToken(";");
     }
 
-    private void readFile() throws IOException {
-        InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file), UTF_8);
-
-        final BufferedReader bufferedReader = new BufferedReader(inputStream);
-
-        while (true) {
-            final String line = bufferedReader.readLine();
-
-            if (line == null)
-                break;
-
-            contents.append(line);
-            contents.append("\n");
-        }
-
-        bufferedReader.close();
-        inputStream.close();
-    }
-
     public void skipUntilSemicolon(final Tokenizer tokenizer) throws InvalidSyntaxException {
         while (true) {
             final TokenizerMatch token = tokenizer.getNextToken();