Переглянути джерело

oj, oj... FileHelper : stare funkcje wywalone lub przeniesione do StringLoader

Ranides Atterwim 13 роки тому
батько
коміт
03e68fb536

+ 3 - 88
src/main/java/net/ranides/assira/io/FileHelper.java

@@ -8,13 +8,8 @@
 package net.ranides.assira.io;
 
 import java.io.*;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
 import java.util.MissingResourceException;
-import java.util.Scanner;
 import net.ranides.assira.generic.PrivilegedActions;
 import net.ranides.assira.generic.ValueUtils;
 import net.ranides.assira.trace.StackInspector;
@@ -54,7 +49,7 @@ public final class FileHelper {
      * @return
      * @throws IOException
      */
-    public static byte[] getFileContent(File file) throws IOException {
+    public static byte[] getContent(File file) throws IOException {
         RandomAccessFile istream = new RandomAccessFile(file, "r");
         byte[] content = new byte[ (int)istream.length() ];
         istream.readFully(content);
@@ -68,90 +63,10 @@ public final class FileHelper {
      * @return
      * @throws IOException
      */
-    public static byte[] getFileContent(String file) throws IOException {
-        return getFileContent(new File(file));
+    public static byte[] getContent(String file) throws IOException {
+        return getContent(new File(file));
     }
     
-    public static List<String> getFileText(File file, String charset) throws IOException {
-        FileInputStream istream = null;
-        BufferedReader reader = null;
-        ArrayList<String> result = new ArrayList<String>();
-        try {
-            istream = new FileInputStream(file);
-            reader = new BufferedReader(new InputStreamReader(istream, charset));
-            String line;
-            while( null != (line=reader.readLine())) {
-                result.add(line);
-            }
-        } finally {
-            FileHelper.close(reader);
-            FileHelper.close(istream);
-        }
-        result.trimToSize();
-        return result;
-    }
-    
-    public static List<String> getFileText(String file, String charset) throws IOException {
-        return getFileText(new File(file), charset);
-    }
-    
-    public static List<String> getFileText(File file) throws IOException {
-        return getFileText(file, "UTF-8");
-    }
-    
-    public static List<String> getFileText(String file) throws IOException {
-        return getFileText(file, "UTF-8");
-    }
-
-    /**
-     * Ładuje zawartość podanego pliku i zwraca w postaci tekstu.
-     * Funkcja do przetworzenia tekstu używa podanego kodowania znaków.
-     * @param file
-     * @param charset
-     * @return
-     * @throws IOException
-     */
-    public static String getFileTextContent(File file, String charset) throws IOException {
-        Scanner scanner = new Scanner(file, charset);
-        String result = scanner.useDelimiter("\\Z").next();
-        scanner.close();
-        return result;
-    }
-
-    /**
-     * Ładuje zawartość podanego pliku i zwraca w postaci tekstu.
-     * Funkcja do przetworzenia tekstu używa podanego kodowania znaków.
-     * @param file
-     * @param charset
-     * @return
-     * @throws IOException
-     */
-    public static String getFileTextContent(String file, String charset) throws IOException {
-        return getFileTextContent(new File(file), charset);
-    }
-
-    /**
-     * Ładuje zawartość podanego pliku i zwraca w postaci tekstu.
-     * Funkcja do przetworzenia tekstu używa kodowania UTF-8.
-     * @param file
-     * @return
-     * @throws IOException
-     */
-    public static String getFileTextContent(File file) throws IOException {
-        return getFileTextContent(file, "UTF-8");
-    }
-
-    /**
-     * Ładuje zawartość podanego pliku i zwraca w postaci tekstu.
-     * Funkcja do przetworzenia tekstu używa kodowania UTF-8.
-     * @param file
-     * @return
-     * @throws IOException
-     */
-    public static String getFileTextContent(String file) throws IOException {
-        return getFileTextContent(new File(file));
-    }
-
     /**
      * Zamyka podany plik. Funkcja odporna na {@code null}. Nie rzuca wyjątków.
      * <table style="margin: 10px 0 0 0; background: #c0F0c0; border: solid 1px #006000;" cellpadding="0"><tr><td>{@code null} safe method</td></tr></table>

+ 17 - 13
src/main/java/net/ranides/assira/text/SelfReader.java

@@ -32,20 +32,24 @@ public final class SelfReader {
     }
     
     private static String load(String file) {
-        InputStream istream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
-        if(istream != null) {
-            return StringLoader.fromStream(istream);
+        try {
+            InputStream istream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
+            if(istream != null) {
+                return StringLoader.fromStream(istream);
+            }
+            if(new File(file).exists() ) {
+                return StringLoader.fromFile(file);
+            }
+            if(new File("src/test/java/" + file).exists() ) {
+                return StringLoader.fromFile("src/test/java/" + file);
+            }
+            if(new File("src/test/main/" + file).exists() ) {
+                return StringLoader.fromFile("src/test/main/" + file);
+            }
+        } catch(IOException cause)  {
+            throw new AssertionError(cause);
         }
-        if(new File(file).exists() ) {
-            return StringLoader.fromFile(file);
-        }
-        if(new File("src/test/java/" + file).exists() ) {
-            return StringLoader.fromFile("src/test/java/" + file);
-        }
-        if(new File("src/test/main/" + file).exists() ) {
-            return StringLoader.fromFile("src/test/main/" + file);
-        }
-        throw new RuntimeException(new IOException("unknown location of file: " + file));
+        throw new AssertionError(new IOException("unknown location of file: " + file));
     }
 
     

+ 40 - 9
src/main/java/net/ranides/assira/text/StringLoader.java

@@ -13,11 +13,15 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StreamTokenizer;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Scanner;
+import net.ranides.assira.io.FileHelper;
 
 /**
  *
@@ -50,23 +54,19 @@ public final class StringLoader {
     }
 
 
-    public static String fromFile(File file, Charset charset) {
-        try {
-            return fromStream( new FileInputStream(file), charset );
-        } catch (FileNotFoundException _) {
-            return null;
-        }
+    public static String fromFile(File file, Charset charset) throws IOException {
+        return fromStream( new FileInputStream(file), charset );
     }
 
-    public static String fromFile(File file) {
+    public static String fromFile(File file) throws IOException {
         return fromFile(file, TextEncoding.NIO_UTF8);
     }
 
-    public static String fromFile(String path, Charset charset) {
+    public static String fromFile(String path, Charset charset) throws IOException {
         return fromFile(new File(path), charset);
     }
 
-    public static String fromFile(String path) {
+    public static String fromFile(String path) throws IOException {
         return fromFile(path, TextEncoding.NIO_UTF8 );
     }
 
@@ -103,4 +103,35 @@ public final class StringLoader {
         }
     }
     
+    public static List<String> linesFromFile(File file, Charset charset) throws IOException {
+        FileInputStream istream = null;
+        BufferedReader reader = null;
+        ArrayList<String> result = new ArrayList<String>();
+        try {
+            istream = new FileInputStream(file);
+            reader = new BufferedReader(new InputStreamReader(istream, charset));
+            String line;
+            while( null != (line=reader.readLine())) {
+                result.add(line);
+            }
+        } finally {
+            FileHelper.close(reader);
+            FileHelper.close(istream);
+        }
+        result.trimToSize();
+        return result;
+    }
+    
+    public static List<String> linesFromFile(String file, Charset charset) throws IOException {
+        return linesFromFile(new File(file), charset);
+    }
+    
+    public static List<String> linesFromFile(File file) throws IOException {
+        return linesFromFile(file, TextEncoding.NIO_UTF8);
+    }
+    
+    public static List<String> linesFromFile(String file) throws IOException {
+        return linesFromFile(file, TextEncoding.NIO_UTF8);
+    }
+    
 }

+ 5 - 4
src/main/java/net/ranides/assira/trace/MultiException.java

@@ -8,6 +8,7 @@ package net.ranides.assira.trace;
 
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import net.ranides.assira.collection.ArrayUtils;
@@ -20,14 +21,14 @@ public class MultiException extends RuntimeException {
 
     private List<Throwable> causeList;
     
-    public MultiException(Throwable... causeList) {
+    public MultiException(Collection<? extends Throwable> causeList) {
         super();
-        this.causeList = new LinkedList<Throwable>(ArrayUtils.asList(causeList));
+        this.causeList = new LinkedList<Throwable>(causeList);
     }
 
-    public MultiException(String message, Throwable... causeList) {
+    public <T extends Throwable> MultiException(String message, Collection<? extends Throwable> causeList) {
         super(message);
-        this.causeList = new LinkedList<Throwable>(ArrayUtils.asList(causeList));
+        this.causeList = new LinkedList<Throwable>(causeList);
     }
 
     public MultiException(String message) {