|
@@ -7,8 +7,10 @@
|
|
|
package net.ranides.assira.io;
|
|
package net.ranides.assira.io;
|
|
|
|
|
|
|
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
@@ -29,9 +31,12 @@ import java.util.Arrays;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
|
import java.util.function.Predicate;
|
|
import java.util.function.Predicate;
|
|
|
|
|
+import java.util.function.Supplier;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
import net.ranides.assira.events.EventDispatcher;
|
|
import net.ranides.assira.events.EventDispatcher;
|
|
|
import net.ranides.assira.events.EventRouter;
|
|
import net.ranides.assira.events.EventRouter;
|
|
|
|
|
+import net.ranides.assira.functional.CheckedFunction;
|
|
|
|
|
+import net.ranides.assira.functional.FunctionUtils;
|
|
|
import net.ranides.assira.text.FormatHex;
|
|
import net.ranides.assira.text.FormatHex;
|
|
|
import net.ranides.assira.trace.ExceptionUtils;
|
|
import net.ranides.assira.trace.ExceptionUtils;
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
@@ -434,6 +439,60 @@ public class IOStreamsTest {
|
|
|
|
|
|
|
|
assertEquals("Hello world\nEOF\n", so.toString("UTF-8"));
|
|
assertEquals("Hello world\nEOF\n", so.toString("UTF-8"));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void testSubStream() throws IOException {
|
|
|
|
|
+ InputStream istream = new BufferedInputStream(new FileInputStream("src/test/resources/text/substream.txt"));
|
|
|
|
|
+
|
|
|
|
|
+ try(InputStream ss = IOStreams.substream(istream, 5, 12)) {
|
|
|
|
|
+ assertEquals(7, ss.available());
|
|
|
|
|
+ assertRead(" porro ", new byte[10], c -> ss.read(c));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try(InputStream ss = IOStreams.substream(istream, 4, 12)) {
|
|
|
|
|
+
|
|
|
|
|
+ assertRead("qu", new byte[10], c -> ss.read(c,0,2));
|
|
|
|
|
+ assertRead("am est", new byte[10], c -> ss.read(c,0,30));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(-1, ss.read(new byte[10]));
|
|
|
|
|
+ assertEquals(-1, ss.read(new byte[10], 2, 4));
|
|
|
|
|
+ assertEquals(0, ss.skip(10));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try(InputStream ss = IOStreams.substream(istream, 0, 20)) {
|
|
|
|
|
+ assertRead(" qui", new byte[4], c -> ss.read(c));
|
|
|
|
|
+ assertEquals(5, ss.skip(5));
|
|
|
|
|
+ assertRead("rem ", new byte[4], b -> ss.read(b));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try(InputStream ss = IOStreams.limit(istream, 1, 20)) {
|
|
|
|
|
+ ss.mark(30);
|
|
|
|
|
+ assertRead("psum quia", new byte[20], b -> ss.read(b,0,9));
|
|
|
|
|
+ ss.reset();
|
|
|
|
|
+ assertRead("psum quia", new byte[20], b -> ss.read(b,0,9));
|
|
|
|
|
+ assertEquals(' ', ss.read());
|
|
|
|
|
+ assertEquals(8, ss.skip(8));
|
|
|
|
|
+ assertEquals('t', ss.read());
|
|
|
|
|
+ assertEquals(-1, ss.read());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ assertThrows(e -> "java.io.IOException: Stream closed".equals(e.toString()), ()->{
|
|
|
|
|
+ IOStreams.substream(istream, 0, 20);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ istream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void assertRead(String text, byte[] buffer, CheckedFunction<byte[],Integer> value) {
|
|
|
|
|
+ int exp = text.length();
|
|
|
|
|
+ int ret = FunctionUtils.rfunction(value).apply(buffer);
|
|
|
|
|
+ assertEquals(exp, ret);
|
|
|
|
|
+ assertEquals(text, new String(buffer, 0, exp, Charsets.US_ASCII));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void assertRead(String text, byte[] buffer, int value) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
private Predicate<Throwable> matchMessages(List<String> exp) {
|
|
private Predicate<Throwable> matchMessages(List<String> exp) {
|
|
|
return cause -> Arrays
|
|
return cause -> Arrays
|