diff --git a/pom.xml b/pom.xml index 71db352e..70f8daad 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.spacious-team table-wrapper-csv-impl - 2023.1 + 2024.1 jar Table Wrapper API CSV Implementation @@ -49,8 +49,8 @@ 11 UTF-8 UTF-8 - 1.18.28 - 3.32.0 + 1.18.32 + 3.42.0 @@ -123,7 +123,7 @@ maven-surefire-plugin - 2.22.2 + 3.2.5 @@ -131,7 +131,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.10.1 + 3.13.0 true true @@ -174,7 +174,7 @@ org.jacoco jacoco-maven-plugin - 0.8.8 + 0.8.12 prepare-agent @@ -194,7 +194,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.1 attach-sources diff --git a/src/main/java/org/spacious_team/table_wrapper/csv/CloseIgnoringInputStream.java b/src/main/java/org/spacious_team/table_wrapper/csv/CloseIgnoringInputStream.java new file mode 100644 index 00000000..e8d4524c --- /dev/null +++ b/src/main/java/org/spacious_team/table_wrapper/csv/CloseIgnoringInputStream.java @@ -0,0 +1,33 @@ +/* + * Table Wrapper CSV Impl + * Copyright (C) 2024 Spacious Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.spacious_team.table_wrapper.csv; + +import java.io.FilterInputStream; + +class CloseIgnoringInputStream extends FilterInputStream { + + public CloseIgnoringInputStream(java.io.InputStream in) { + super(in); + } + + @Override + public void close() { + // do nothing + } +} diff --git a/src/main/java/org/spacious_team/table_wrapper/csv/CsvReportPage.java b/src/main/java/org/spacious_team/table_wrapper/csv/CsvReportPage.java index bf06c9f1..aab1ad55 100644 --- a/src/main/java/org/spacious_team/table_wrapper/csv/CsvReportPage.java +++ b/src/main/java/org/spacious_team/table_wrapper/csv/CsvReportPage.java @@ -20,6 +20,7 @@ import com.univocity.parsers.csv.CsvParser; import com.univocity.parsers.csv.CsvParserSettings; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.spacious_team.table_wrapper.api.AbstractReportPage; import org.spacious_team.table_wrapper.api.TableCellAddress; @@ -44,23 +45,38 @@ public class CsvReportPage extends AbstractReportPage { * Field and line delimiter detected automatically. UTF-8 encoded file expected. */ public CsvReportPage(Path path) throws IOException { - this(Files.newInputStream(path, StandardOpenOption.READ)); + try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ)) { + this.rows = readRows(inputStream, UTF_8, getDefaultCsvParserSettings()); + } } /** - * Closes inputStream if success. UTF-8 encoded stream data expected. + * Field and line delimiter detected automatically. UTF-8 encoded file expected. + * + * @implSpec Does not close inputStream */ public CsvReportPage(InputStream inputStream) throws IOException { this(inputStream, UTF_8, getDefaultCsvParserSettings()); } /** - * Closes inputStream if success + * @implSpec Does not close inputStream */ public CsvReportPage(InputStream inputStream, Charset charset, CsvParserSettings csvParserSettings) throws IOException { + CloseIgnoringInputStream closeIgnoringInputStream = new CloseIgnoringInputStream(inputStream); + this.rows = readRows(closeIgnoringInputStream, charset, csvParserSettings); + } + + /** + * @implSpec Closes inputStream + */ + private static String[] @NonNull [] readRows(InputStream inputStream, + Charset charset, + CsvParserSettings csvParserSettings) throws IOException { try (Reader inputReader = new InputStreamReader(inputStream, charset)) { CsvParser parser = new CsvParser(csvParserSettings); - rows = parser.parseAll(inputReader).toArray(new String[0][]); + return parser.parseAll(inputReader) + .toArray(new String[0][]); } } diff --git a/src/test/java/org/spacious_team/table_wrapper/csv/CsvReportPageTest.java b/src/test/java/org/spacious_team/table_wrapper/csv/CsvReportPageTest.java index 1ab425db..02266bec 100644 --- a/src/test/java/org/spacious_team/table_wrapper/csv/CsvReportPageTest.java +++ b/src/test/java/org/spacious_team/table_wrapper/csv/CsvReportPageTest.java @@ -32,8 +32,10 @@ import java.nio.file.Files; import java.nio.file.Path; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.*; class CsvReportPageTest { @@ -53,6 +55,20 @@ void createFromFile() throws IOException { } } + @Test + void createFomInputStream_inputStreamNotClosed() throws IOException { + ByteArrayInputStream is = spy(new ByteArrayInputStream(new byte[]{})); + new CsvReportPage(is); + verify(is, never()).close(); + } + + @Test + void createFomInputStreamAndSettings_inputStreamNotClosed() throws IOException { + ByteArrayInputStream is = spy(new ByteArrayInputStream(new byte[]{})); + new CsvReportPage(is, UTF_8, CsvReportPage.getDefaultCsvParserSettings()); + verify(is, never()).close(); + } + @ParameterizedTest @ValueSource(strings = {"UTF-8", "Windows-1251"}) void testInputDataCharset(String charsetName) throws IOException {