Explorar el Código

JoinList - new constructor

FileHelper - BUFFER_SIZE
FileHelper # append(output, input, bufsize)

PathHelper.FilePath - new constructors
PathHelper.FilePath # valueOf
PathHelper # join

StringEncoder # decodeURL

ExceptionInspector # assertError
Ranides Atterwim hace 11 años
padre
commit
782a3f12d6

+ 4 - 0
src/main/java/net/ranides/assira/collection/list/JoinList.java

@@ -24,6 +24,10 @@ public class JoinList<T> extends AbstractSequentialList<T> {
     public JoinList() {
         content = new LinkedList<List<T>>();
     }
+    
+    public JoinList(List<T> values[]) {
+        content = Arrays.asList(values);
+    }
 
     /**
      * Tworzy nowe złączenie na podstawie podanej listy list.

+ 21 - 8
src/main/java/net/ranides/assira/io/FileHelper.java

@@ -23,6 +23,8 @@ import net.ranides.assira.trace.StackInspector;
  */
 @PMD.StaticHelper
 public final class FileHelper {
+    
+    private static final int BUFFER_SIZE = 4096;
 
     private FileHelper() { }
 
@@ -98,14 +100,9 @@ public final class FileHelper {
      */
     public static void copy(OutputStream output, InputStream input, int bufsize) throws IOException {
         try {
-            byte[] buffer = new byte[bufsize];
-            int n;
-            while( (n = input.read(buffer)) > 0 ) {
-                output.write(buffer, 0, n);
-            }
+            append(output, input, bufsize);
         } finally {
             close(output);
-            close(input);
         }
     }
 
@@ -119,7 +116,7 @@ public final class FileHelper {
      * @throws IOException
      */
     public static void copy(OutputStream output, InputStream input) throws IOException {
-        copy(output, input, 4096);
+        copy(output, input, BUFFER_SIZE);
     }
 
     /**
@@ -153,6 +150,22 @@ public final class FileHelper {
     public static void copy(String output, String input) throws IOException {
         copy(new FileOutputStream(output), new FileInputStream(input));
     }
+    
+    public static void append(OutputStream output, InputStream input, int bufsize) throws IOException {
+        try {
+            byte[] buffer = new byte[bufsize];
+            int n;
+            while( (n = input.read(buffer)) > 0 ) {
+                output.write(buffer, 0, n);
+            }
+        } finally {
+            close(input);
+        }
+    }
+    
+    public static void append(OutputStream output, InputStream input) throws IOException {
+        append(output, input, BUFFER_SIZE);
+    }
 
     /**
      * Funkcja porównuje binarnie zawartość podanych strumieni. Strumienie nie są
@@ -193,7 +206,7 @@ public final class FileHelper {
      * @throws IOException
      */
     public static boolean equals(InputStream left, InputStream right) throws IOException {
-        return equals(left, right, 4096);
+        return equals(left, right, BUFFER_SIZE);
     }
 
     /**

+ 65 - 1
src/main/java/net/ranides/assira/io/PathHelper.java

@@ -10,8 +10,12 @@ package net.ranides.assira.io;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.regex.Pattern;
+import net.ranides.assira.collection.list.JoinList;
+import net.ranides.assira.collection.list.ListAdapter;
 import net.ranides.assira.collection.list.VirtualList;
 import net.ranides.assira.text.StringUtils;
 import net.ranides.assira.text.Strings;
@@ -21,7 +25,7 @@ import net.ranides.assira.text.Strings;
  * @author ranides
  */
 public final class PathHelper {
-
+    
     private PathHelper() { }
 
     /**
@@ -39,9 +43,35 @@ public final class PathHelper {
     public static final class FilePath extends VirtualList<String> {
         private final String[] components;
 
+        FilePath(String[] components) {
+            this.components = components;
+        }
+        
         FilePath(List<String> components) {
             this.components = components.toArray(new String[components.size()]);
         }
+        
+        FilePath(String path) {
+            String text = path.replace('/', File.separatorChar).replace('\\', File.separatorChar);
+            this.components = text.split(Pattern.quote(File.separator));
+        }
+        
+        FilePath(File path) {
+            this.components = path.getPath().split(Pattern.quote(File.separator));
+        }
+        
+        static FilePath valueOf(Object value) {
+            if(value instanceof FilePath) {
+                return (FilePath)value;
+            }
+            if(value instanceof String) {
+                return new FilePath((String)value);
+            }
+            if(value instanceof File) {
+                return new FilePath((File)value);
+            }
+            throw new IllegalArgumentException("Unsupported value: " + value);
+        }
 
         @Override
         public String get(int index) {
@@ -61,6 +91,11 @@ public final class PathHelper {
             return new File(StringUtils.join(components, File.separator));
         }
 
+        @Override
+        public String toString() {
+            return StringUtils.join(components, File.separator);
+        }
+
     }
 
     /**
@@ -327,4 +362,33 @@ public final class PathHelper {
     public static int compare(String path1, String path2) {
         return asFile(path1).compareTo(asFile(path2));
     }
+    
+    public static String join(FilePath... paths) {
+        return new FilePath(new JoinList<String>(paths)).toString();
+    }
+    
+    public static String join(File... paths) throws PathConvertException {
+        FilePath[] out = new FilePath[paths.length];
+        for(int i=0; i<paths.length; i++) {
+            out[i] = new FilePath(paths[i]);
+        }
+        return join(out);
+    }
+    
+    public static String join(String... paths) throws PathConvertException {
+        FilePath[] out = new FilePath[paths.length];
+        for(int i=0; i<paths.length; i++) {
+            out[i] = new FilePath(paths[i]);
+        }
+        return join(out);
+    }
+    
+    public static String join(Object... paths) {
+        FilePath[] out = new FilePath[paths.length];
+        for(int i=0; i<paths.length; i++) {
+            out[i] = FilePath.valueOf(paths[i]);
+        }
+        return join(out);
+    }
+
 }

+ 10 - 0
src/main/java/net/ranides/assira/text/StringEncoder.java

@@ -7,10 +7,13 @@
 
 package net.ranides.assira.text;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.util.Collection;
 import net.ranides.assira.generic.ValueUtils;
+import net.ranides.assira.trace.ExceptionInspector;
 
 /**
  *
@@ -103,5 +106,12 @@ public final class StringEncoder {
         return result.toString();
     }
 
+    public static String decodeURL(String input) {
+        try {
+            return URLDecoder.decode(input, "UTF-8");
+        } catch(UnsupportedEncodingException cause) {
+            throw ExceptionInspector.assertError("UTF-8 unsupported", cause);
+        }
+    }
 
 }

+ 5 - 1
src/main/java/net/ranides/assira/trace/ExceptionInspector.java

@@ -85,6 +85,10 @@ public final class ExceptionInspector {
 
     }
     
-    
+    public static AssertionError assertError(String message, Throwable cause) {
+        AssertionError error = new AssertionError(message);
+        error.initCause(cause);
+        return error;
+    }
 
 }

+ 14 - 0
src/test/java/net/ranides/assira/io/PathHelperTest.java

@@ -6,6 +6,7 @@
  */
 package net.ranides.assira.io;
 
+import java.io.File;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -59,6 +60,19 @@ public class PathHelperTest {
         assertPathEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home"));
 
     }
+    
+    @Test
+    public void testConcat() throws PathConvertException {
+        assertPathEquals("home\\file\\name", PathHelper.join("home", "file", "name"));
+        assertPathEquals("\\root\\home\\file\\name", PathHelper.join("/root/home", "file", "name"));
+        
+        assertPathEquals("home\\file\\name", PathHelper.join(new File("home"), new File("file"), new File("name")));
+        assertPathEquals("\\root\\home\\file\\name", PathHelper.join(new File("/root/home"), new File("file"), new File("name")));
+        
+        assertPathEquals("home\\file\\name", PathHelper.join(new File("home"), "file", "name"));
+        assertPathEquals("home\\path\\file\\name", PathHelper.join(new File("home"), "path/file", "name"));
+
+    }
 
     private static void assertPathEquals(String expected, String value) {
         assertEquals(PathHelper.asFile(expected), PathHelper.asFile(value));