diff --git a/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java b/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java index 32e598a..14d709a 100644 --- a/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java +++ b/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java @@ -5,7 +5,9 @@ import name.velikodniy.vitaliy.fixedlength.annotation.SplitLineAfter; import name.velikodniy.vitaliy.fixedlength.formatters.Formatter; +import java.io.BufferedReader; import java.io.InputStream; +import java.io.Reader; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -290,21 +292,34 @@ public List parse(InputStream stream) throws FixedLengthException { return this.parseAsStream(stream).collect(Collectors.toList()); } + public List parse(Reader reader) throws FixedLengthException { + return parseAsStream(reader).collect(Collectors.toList()); + } + public Stream parseAsStream(InputStream inputStream) throws FixedLengthException { + Stream lines = StreamSupport.stream( + Spliterators.spliterator( + new Scanner(inputStream, this.charset.name()).useDelimiter(this.delimiter), + Long.MAX_VALUE, + Spliterator.ORDERED | Spliterator.NONNULL + ), false); + + return parseAsStream(lines); + } + + public Stream parseAsStream(Reader reader) throws FixedLengthException { + return parseAsStream(new BufferedReader(reader).lines()); + } + + private Stream parseAsStream(Stream lines) throws FixedLengthException { if (lineTypes.isEmpty()) { throw new FixedLengthException( "Specify at least one line type" ); } - return StreamSupport.stream( - Spliterators.spliterator( - new Scanner(inputStream, this.charset.name()).useDelimiter(this.delimiter), - Long.MAX_VALUE, - Spliterator.ORDERED | Spliterator.NONNULL - ), false) - .map(this::fixedFormatLine) + return lines.map(this::fixedFormatLine) .filter(Objects::nonNull) .flatMap(fixedFormatRecord -> lineToObjects(fixedFormatRecord).stream()); } diff --git a/src/test/java/ParserTest.java b/src/test/java/ParserTest.java index 82b06a0..002c119 100644 --- a/src/test/java/ParserTest.java +++ b/src/test/java/ParserTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; +import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.format.DateTimeParseException; @@ -58,7 +59,6 @@ void testParseInheritedOneLineType() throws FixedLengthException { assertNotNull(((InheritedEmployee)e).lastName); }); } - @Test @DisplayName("Parse as input stream with default charset and one line type") @@ -178,4 +178,14 @@ void testParseMixedLineTypeCustomDelimiter() throws FixedLengthException { assertEquals(3, parse.size()); } + + @Test + @DisplayName("Parse as reader with default charset and one line type") + void testParseReaderWithDefaultCharset() throws FixedLengthException { + List parse = new FixedLength() + .registerLineType(Employee.class) + .parse(new StringReader(singleTypeExample)); + + assertEquals(2, parse.size()); + } }