package org.projog.build;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Constants and utility methods used in the build process.
 */
class BuildUtilsConstants {
   private static final File BUILD_DIR = new File("build");

   static final File WEB_SRC_DIR = new File("web");
   static final File DOCS_OUTPUT_DIR = new File(BUILD_DIR, "docs");
   static final File SCRIPTS_OUTPUT_DIR = new File(BUILD_DIR, "scripts");
   static final File MANUAL_TEMPLATE = new File(WEB_SRC_DIR, "manual.txt");
   static final File STATIC_PAGES_LIST = new File(WEB_SRC_DIR, "static_pages.properties");
   static final String LINE_BREAK = "\r\n";
   static final String HTML_FILE_EXTENSION = ".html";
   static final String PROLOG_FILE_EXTENSION = ".pl";
   static final String TEXT_FILE_EXTENSION = ".txt";
   static final String MANUAL_HTML = "manual" + HTML_FILE_EXTENSION;
   static final String HEADER_HTML = "header" + HTML_FILE_EXTENSION;
   static final String FOOTER_HTML = "footer" + HTML_FILE_EXTENSION;

   /**
    * Returns the contents of the specified file as a byte array.
    * 
    * @param f file to read
    * @return contents of file
    */
   static byte[] toByteArray(File f) {
      try (FileInputStream fis = new FileInputStream(f); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
         int next;
         while ((next = fis.read()) != -1) {
            baos.write(next);
         }
         return baos.toByteArray();
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   /**
    * Returns list of lines contained in specified text file.
    * 
    * @param f text file to read
    * @return list of lines contained in specified file
    */
   static List<String> readFile(File f) {
      List<String> result = new ArrayList<>();
      try (FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr)) {
         String next;
         while ((next = br.readLine()) != null) {
            result.add(next);
         }
         if (result.isEmpty()) {
            throw new Exception("File is empty");
         }
      } catch (Exception e) {
         throw new RuntimeException("could not read text file: " + f, e);
      }
      return result;
   }
}
@@ -35,30 +35,20 @@
 java.io.
-BufferedReader
+File
 ;%0Aimport
@@ -57,53 +57,27 @@
 ava.
+n
 io.
-ByteArrayOutputStream;%0Aimport java.io.File
+charset.Charset
 ;%0Aim
@@ -90,81 +90,22 @@
 ava.
+n
 io.
-FileInputStream;%0Aimport java.io.FileReader;%0Aimport java.util.ArrayList
+file.Files
 ;%0Aim
@@ -1256,253 +1256,55 @@
 try 
-(FileInputStream fis = new FileInputStream(f); ByteArrayOutputStream baos = new ByteArrayOutputStream()) %7B%0A         int next;%0A         while ((next = fis.read()) != -1) %7B%0A            baos.write(next);%0A         %7D%0A         return baos.toByteArray(
+%7B%0A         return Files.readAllBytes(f.toPath()
 );%0A 
@@ -1368,16 +1368,45 @@
 ception(
+%22could not read file: %22 + f, 
 e);%0A    
@@ -1639,347 +1639,87 @@
     
-List%3CString%3E result = new ArrayList%3C%3E();%0A      try (FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr)) %7B%0A         String next;%0A         while ((next = br.readLine()) != null) %7B%0A            result.add(next);%0A         %7D%0A         if (result.isEmpty()) %7B%0A            throw new Exception(%22File is empty%22);%0A         %7D
+try %7B%0A         return Files.readAllLines(f.toPath(), Charset.defaultCharset());
 %0A   
@@ -1831,29 +1831,8 @@
   %7D%0A
-      return result;%0A
    %7D

use java.nio.file.Files