Pārlūkot izejas kodu

new: IOStreams#pipe

Ranides Atterwim 10 gadi atpakaļ
vecāks
revīzija
4b83ca8403

+ 1 - 0
assira/pom.xml

@@ -55,6 +55,7 @@
                                 <include>net/ranides/assira/io/URIOutput</include>
                                 <include>net/ranides/assira/io/URIWriter</include>
                                 <include>net/ranides/assira/io/URIReader</include>
+                                <include>net/ranides/assira/io/IOStreams*</include>
                                 
                                 <include>net/ranides/assira/lexer/**</include>
                                 

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 670 - 594
assira/src/main/java/net/ranides/assira/io/IOStreams.java


+ 68 - 68
assira/src/main/java/net/ranides/assira/io/IOUtils.java

@@ -1,68 +1,68 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.io;
-
-import java.io.Closeable;
-import java.io.IOException;
-import java.util.function.Consumer;
-import net.ranides.assira.trace.ExceptionUtils;
-import net.ranides.assira.trace.LoggerUtils;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public final class IOUtils {
-    
-    private static final org.slf4j.Logger LOGGER = LoggerUtils.getLogger();
-    
-    private IOUtils() {
-        /* utility class */
-    }
-    
-    public static IOException wrap(Throwable cause) {
-        return (cause instanceof IOException) ? (IOException)cause : new IOException(cause);
-    }
-    
-    public static RuntimeException rethrow(Throwable cause) {
-        return ExceptionUtils.rethrow(wrap(cause));
-    }
-    
-    public static boolean close(Closeable object) throws IOException {
-        if(null != object) {
-            object.close();
-            return true;
-        }
-        return false;
-    }
-    
-    public static boolean close(Object object) throws Exception {
-        if(object instanceof Closeable) {
-            ((Closeable)object).close();
-            return true;
-        }
-        if(object instanceof AutoCloseable) {
-            ((AutoCloseable)object).close();
-            return true;
-        }
-        return false;
-    }
-    
-    public static boolean close(Object object, Consumer<Exception> handler) {
-        try {
-            return close(object);
-        } catch(Exception cause) {
-            if(null != handler) {
-                handler.accept(cause);
-            } else {
-                LOGGER.warn("Exception ignored on #close", cause);
-            }
-            return false;
-        }
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.io;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.function.Consumer;
+import net.ranides.assira.trace.ExceptionUtils;
+import net.ranides.assira.trace.LoggerUtils;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public final class IOUtils {
+    
+    private static final org.slf4j.Logger LOGGER = LoggerUtils.getLogger();
+    
+    private IOUtils() {
+        /* utility class */
+    }
+    
+    public static IOException wrap(Throwable cause) {
+        return (cause instanceof IOException) ? (IOException)cause : new IOException(cause);
+    }
+    
+    public static RuntimeException rethrow(Throwable cause) {
+        return ExceptionUtils.rethrow(wrap(cause));
+    }
+    
+    public static boolean close(Closeable object) throws IOException {
+        if(null != object) {
+            object.close();
+            return true;
+        }
+        return false;
+    }
+    
+    public static boolean close(Object object) throws Exception {
+        if(object instanceof Closeable) {
+            ((Closeable)object).close();
+            return true;
+        }
+        if(object instanceof AutoCloseable) {
+            ((AutoCloseable)object).close();
+            return true;
+        }
+        return false;
+    }
+    
+    public static boolean close(Object object, Consumer<? super Exception> handler) {
+        try {
+            return close(object);
+        } catch(Exception cause) {
+            if(null != handler) {
+                handler.accept(cause);
+            } else {
+                LOGGER.warn("Exception ignored on #close", cause);
+            }
+            return false;
+        }
+    }
+    
+}

+ 158 - 162
assira/src/test/java/net/ranides/assira/io/IOUtilsTest.java

@@ -1,162 +1,158 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.io;
-
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.Closeable;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Random;
-import java.util.stream.Collectors;
-import net.ranides.assira.events.Events;
-import net.ranides.assira.events.EventListener;
-import net.ranides.assira.junit.LogObserver;
-import static net.ranides.assira.junit.NewAssert.*;
-import net.ranides.assira.trace.LoggerUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class IOUtilsTest {
-    
-    private static final org.slf4j.Logger LOGGER = LoggerUtils.getLogger();
-    
-    @Before
-    public void init() {
-        LogObserver.reset(true);
-    }
-    
-    @After
-    public void release() {
-        LogObserver.reset(false);
-    }
-    
-    @Test
-    public void testClose_Object() throws Exception {
-        List<Integer> out = new ArrayList<>();
-        
-        assertFalse( IOUtils.close((Object)null) );
-        assertTrue( IOUtils.close((Object) (Closeable)() -> { out.add(7); }) );
-        assertTrue( IOUtils.close((Object) (AutoCloseable)() -> { out.add(8); }) );
-        
-        assertEquals(Arrays.asList(7,8), out);
-                
-        assertThrows(IOException.class, ()->{
-            IOUtils.close((Object)new CFile("IO"));
-        });
-        assertThrows(SQLException.class, ()->{
-            IOUtils.close((Object)new SFile());
-        });
-    }
-    
-    @Test
-    public void testClose_Closeable() throws IOException {
-        List<Integer> out = new ArrayList<>();
-        
-        assertFalse( IOUtils.close((Closeable)null) );
-        assertTrue( IOUtils.close((Closeable)() -> { out.add(7); }) );
-        
-        assertEquals(Arrays.asList(7), out);
-                
-        assertThrows(IOException.class, ()->{
-            IOUtils.close(new CFile("IO"));
-        });
-    }
- 
-    @Test
-    public void testClose_Consumer() {
-        List<Throwable> errors = new ArrayList<>();
-        
-        EventListener<Events.Failure> listener = (e) -> {
-            errors.add(e.cause());
-        };
-        
-        // listener
-        IOUtils.close(new CFile("IO"), listener.consumer(e -> Events.failure(e)));
-        
-        // listener
-        IOUtils.close(new SFile(), listener.consumer(Events::failure));
-        
-        // consumer
-        IOUtils.close(new CFile("NC"), errors::add);
-        
-        // real NOP - ignore exception silently
-        IOUtils.close(new SFile(), e -> {});
-        
-        // "safe" NOP - write exception to log (with WARN level)
-        IOUtils.close(new SFile(), null);
-                
-        // explicit logger
-        IOUtils.close(new SFile(), e -> LOGGER.error("close!", e));
-        
-        List<String> expected = Arrays.asList(
-                "java.io.IOException: IO", 
-                "java.sql.SQLException: SQL", 
-                "java.io.IOException: NC"
-        );
-        List<String> errstr = errors.stream().map(Object::toString).collect(Collectors.toList());
-        assertEquals(expected, errstr);
-        
-        assertTrue("WARN expected", LogObserver.match(m->
-            m.text().equals("[main] WARN net.ranides.assira.io.IOUtils - Exception ignored on #close @ java.sql.SQLException: SQL")
-        ));
-        assertTrue("ERROR expected", LogObserver.match(m->
-            m.text().equals("[main] ERROR net.ranides.assira.io.IOUtilsTest - close! @ java.sql.SQLException: SQL")
-        ));
-    }
-    
-    @Test
-    public void testWrap() {
-        assertEquals(IOException.class, IOUtils.wrap(new SQLException()).getClass());
-        IOException e = new IOException();
-        assertSame(e, IOUtils.wrap(e));
-    }
-    
-    @Test
-    public void testRethrow() {
-        assertThrows(IOException.class, ()->{
-            throw IOUtils.rethrow(new SQLException());
-        });
-        assertThrows(IOException.class, ()->{
-            throw IOUtils.rethrow(new IOException());
-        });
-    }
-    
-    private static final class CFile implements Closeable {
-        
-        private final String msg;
-
-        public CFile(String msg) {
-            this.msg = msg;
-        }
-
-        @Override
-        public void close() throws IOException {
-            throw new IOException(msg);
-        }
-    
-    }
-    
-    private static final class SFile implements AutoCloseable {
-
-        @Override
-        public void close() throws Exception {
-            throw new SQLException("SQL");
-        }
-    
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.io;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import net.ranides.assira.events.Events;
+import net.ranides.assira.events.EventListener;
+import net.ranides.assira.junit.LogObserver;
+import static net.ranides.assira.junit.NewAssert.*;
+import net.ranides.assira.trace.LoggerUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class IOUtilsTest {
+    
+    private static final org.slf4j.Logger LOGGER = LoggerUtils.getLogger();
+    
+    @Before
+    public void init() {
+        LogObserver.reset(true);
+    }
+    
+    @After
+    public void release() {
+        LogObserver.reset(false);
+    }
+    
+    @Test
+    public void testClose_Object() throws Exception {
+        List<Integer> out = new ArrayList<>();
+        
+        assertFalse( IOUtils.close((Object)null) );
+        assertTrue( IOUtils.close((Object) (Closeable)() -> { out.add(7); }) );
+        assertTrue( IOUtils.close((Object) (AutoCloseable)() -> { out.add(8); }) );
+        
+        assertEquals(Arrays.asList(7,8), out);
+                
+        assertThrows(IOException.class, ()->{
+            IOUtils.close((Object)new CFile("IO"));
+        });
+        assertThrows(SQLException.class, ()->{
+            IOUtils.close((Object)new SFile());
+        });
+    }
+    
+    @Test
+    public void testClose_Closeable() throws IOException {
+        List<Integer> out = new ArrayList<>();
+        
+        assertFalse( IOUtils.close((Closeable)null) );
+        assertTrue( IOUtils.close((Closeable)() -> { out.add(7); }) );
+        
+        assertEquals(Arrays.asList(7), out);
+                
+        assertThrows(IOException.class, ()->{
+            IOUtils.close(new CFile("IO"));
+        });
+    }
+ 
+    @Test
+    public void testClose_Consumer() {
+        List<Throwable> errors = new ArrayList<>();
+        
+        EventListener<Events.Failure> listener = (e) -> {
+            errors.add(e.cause());
+        };
+        
+        // listener
+        IOUtils.close(new CFile("IO"), listener.consumer(e -> Events.failure(e)));
+        
+        // listener
+        IOUtils.close(new SFile(), listener.consumer(Events::failure));
+        
+        // consumer
+        IOUtils.close(new CFile("NC"), errors::add);
+        
+        // real NOP - ignore exception silently
+        IOUtils.close(new SFile(), e -> {});
+        
+        // "safe" NOP - write exception to log (with WARN level)
+        IOUtils.close(new SFile(), null);
+                
+        // explicit logger
+        IOUtils.close(new SFile(), e -> LOGGER.error("close!", e));
+        
+        List<String> expected = Arrays.asList(
+                "java.io.IOException: IO", 
+                "java.sql.SQLException: SQL", 
+                "java.io.IOException: NC"
+        );
+        List<String> errstr = errors.stream().map(Object::toString).collect(Collectors.toList());
+        assertEquals(expected, errstr);
+        
+        assertTrue("WARN expected", LogObserver.match(m->
+            m.text().equals("[main] WARN net.ranides.assira.io.IOUtils - Exception ignored on #close @ java.sql.SQLException: SQL")
+        ));
+        assertTrue("ERROR expected", LogObserver.match(m->
+            m.text().equals("[main] ERROR net.ranides.assira.io.IOUtilsTest - close! @ java.sql.SQLException: SQL")
+        ));
+    }
+    
+    @Test
+    public void testWrap() {
+        assertEquals(IOException.class, IOUtils.wrap(new SQLException()).getClass());
+        IOException e = new IOException();
+        assertSame(e, IOUtils.wrap(e));
+    }
+    
+    @Test
+    public void testRethrow() {
+        assertThrows(IOException.class, ()->{
+            throw IOUtils.rethrow(new SQLException());
+        });
+        assertThrows(IOException.class, ()->{
+            throw IOUtils.rethrow(new IOException());
+        });
+    }
+    
+    private static final class CFile implements Closeable {
+        
+        private final String msg;
+
+        public CFile(String msg) {
+            this.msg = msg;
+        }
+
+        @Override
+        public void close() throws IOException {
+            throw new IOException(msg);
+        }
+    
+    }
+    
+    private static final class SFile implements AutoCloseable {
+
+        @Override
+        public void close() throws Exception {
+            throw new SQLException("SQL");
+        }
+    
+    }
+    
+}