From 37039d07ba0106c395b2b8c3abd064cb3e732628 Mon Sep 17 00:00:00 2001 From: Larry White Date: Sun, 31 Oct 2021 06:14:36 -0400 Subject: [PATCH] Removed IOException from write interfaces IOException is caught and re-thrown as RuntimeIOException(e) --- .../tech/tablesaw/io/DataFrameWriter.java | 27 ++++++++++--------- .../java/tech/tablesaw/io/DataWriter.java | 5 ++-- .../java/tech/tablesaw/io/Destination.java | 15 +++++------ .../java/tech/tablesaw/io/WriteOptions.java | 3 +-- .../tech/tablesaw/io/csv/CsvWriteOptions.java | 9 +++---- .../io/fixed/FixedWidthWriteOptions.java | 11 ++++---- .../tablesaw/io/fixed/FixedWidthWriter.java | 3 +-- .../java/tech/tablesaw/api/TableTest.java | 3 +-- .../tech/tablesaw/io/html/HtmlWriter.java | 7 +++-- .../tech/tablesaw/io/json/JsonWriter.java | 10 ++++--- 10 files changed, 46 insertions(+), 47 deletions(-) diff --git a/core/src/main/java/tech/tablesaw/io/DataFrameWriter.java b/core/src/main/java/tech/tablesaw/io/DataFrameWriter.java index 4648b6f1a..f49bcbd13 100644 --- a/core/src/main/java/tech/tablesaw/io/DataFrameWriter.java +++ b/core/src/main/java/tech/tablesaw/io/DataFrameWriter.java @@ -34,27 +34,32 @@ public DataFrameWriter(WriterRegistry registry, Table table) { this.table = table; } - public void toFile(String file) throws IOException { + public void toFile(String file) { toFile(new File(file)); } - public void toFile(File file) throws IOException { - String extension = Files.getFileExtension(file.getCanonicalPath()); + public void toFile(File file) { + String extension = null; + try { + extension = Files.getFileExtension(file.getCanonicalPath()); + } catch (IOException e) { + throw new RuntimeIOException(e); + } DataWriter dataWriter = registry.getWriterForExtension(extension); dataWriter.write(table, new Destination(file)); } - public void toStream(OutputStream stream, String extension) throws IOException { + public void toStream(OutputStream stream, String extension) { DataWriter dataWriter = registry.getWriterForExtension(extension); dataWriter.write(table, new Destination(stream)); } - public void toWriter(Writer writer, String extension) throws IOException { + public void toWriter(Writer writer, String extension) { DataWriter dataWriter = registry.getWriterForExtension(extension); dataWriter.write(table, new Destination(writer)); } - public void usingOptions(T options) throws IOException { + public void usingOptions(T options) { DataWriter dataWriter = registry.getWriterForOptions(options); dataWriter.write(table, options); } @@ -62,22 +67,18 @@ public void usingOptions(T options) throws IOException public String toString(String extension) { StringWriter writer = new StringWriter(); DataWriter dataWriter = registry.getWriterForExtension(extension); - try { - dataWriter.write(table, new Destination(writer)); - } catch (IOException e) { - throw new IllegalStateException(e); - } + dataWriter.write(table, new Destination(writer)); return writer.toString(); } // legacy methods left for backwards compatibility - public void csv(String file) throws IOException { + public void csv(String file) { CsvWriteOptions options = CsvWriteOptions.builder(file).build(); new CsvWriter().write(table, options); } - public void csv(File file) throws IOException { + public void csv(File file) { CsvWriteOptions options = CsvWriteOptions.builder(file).build(); new CsvWriter().write(table, options); } diff --git a/core/src/main/java/tech/tablesaw/io/DataWriter.java b/core/src/main/java/tech/tablesaw/io/DataWriter.java index dd689a2bb..f5afdfb78 100644 --- a/core/src/main/java/tech/tablesaw/io/DataWriter.java +++ b/core/src/main/java/tech/tablesaw/io/DataWriter.java @@ -1,11 +1,10 @@ package tech.tablesaw.io; -import java.io.IOException; import tech.tablesaw.api.Table; public interface DataWriter { - void write(Table table, Destination dest) throws IOException; + void write(Table table, Destination dest); - void write(Table table, O options) throws IOException; + void write(Table table, O options); } diff --git a/core/src/main/java/tech/tablesaw/io/Destination.java b/core/src/main/java/tech/tablesaw/io/Destination.java index b687b1296..99eb6ec23 100644 --- a/core/src/main/java/tech/tablesaw/io/Destination.java +++ b/core/src/main/java/tech/tablesaw/io/Destination.java @@ -1,19 +1,18 @@ package tech.tablesaw.io; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; +import java.io.*; public class Destination { protected final OutputStream stream; protected final Writer writer; - public Destination(File file) throws IOException { - this.stream = new FileOutputStream(file); + public Destination(File file) { + try { + this.stream = new FileOutputStream(file); + } catch (FileNotFoundException e) { + throw new RuntimeIOException(e); + } this.writer = null; } diff --git a/core/src/main/java/tech/tablesaw/io/WriteOptions.java b/core/src/main/java/tech/tablesaw/io/WriteOptions.java index 5f8846419..965246c0e 100644 --- a/core/src/main/java/tech/tablesaw/io/WriteOptions.java +++ b/core/src/main/java/tech/tablesaw/io/WriteOptions.java @@ -1,7 +1,6 @@ package tech.tablesaw.io; import java.io.File; -import java.io.IOException; import java.io.OutputStream; import java.io.Writer; @@ -42,7 +41,7 @@ protected Builder(Writer dest) { this.dest = new Destination(dest); } - protected Builder(File dest) throws IOException { + protected Builder(File dest) { this.dest = new Destination(dest); this.autoClose = true; } diff --git a/core/src/main/java/tech/tablesaw/io/csv/CsvWriteOptions.java b/core/src/main/java/tech/tablesaw/io/csv/CsvWriteOptions.java index 4153d07c2..43cfb7a87 100644 --- a/core/src/main/java/tech/tablesaw/io/csv/CsvWriteOptions.java +++ b/core/src/main/java/tech/tablesaw/io/csv/CsvWriteOptions.java @@ -1,7 +1,6 @@ package tech.tablesaw.io.csv; import java.io.File; -import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.nio.file.Paths; @@ -106,11 +105,11 @@ public static Builder builder(Writer dest) { return new Builder(dest); } - public static Builder builder(File dest) throws IOException { + public static Builder builder(File dest) { return new Builder(dest); } - public static Builder builder(String fileName) throws IOException { + public static Builder builder(String fileName) { return builder(new File(fileName)); } @@ -129,7 +128,7 @@ public static class Builder extends WriteOptions.Builder { private DateTimeFormatter dateFormatter; private Map columnNameMap = new HashMap<>(); - protected Builder(String fileName) throws IOException { + protected Builder(String fileName) { super(Paths.get(fileName).toFile()); } @@ -137,7 +136,7 @@ protected Builder(Destination dest) { super(dest); } - protected Builder(File file) throws IOException { + protected Builder(File file) { super(file); } diff --git a/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriteOptions.java b/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriteOptions.java index e0a3020f2..5e830471a 100644 --- a/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriteOptions.java +++ b/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriteOptions.java @@ -3,7 +3,6 @@ import com.univocity.parsers.fixed.FieldAlignment; import com.univocity.parsers.fixed.FixedWidthFields; import java.io.File; -import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import tech.tablesaw.io.Destination; @@ -146,15 +145,15 @@ public boolean autoClose() { return autoClose; } - public static Builder builder(Destination destination) throws IOException { + public static Builder builder(Destination destination) { return new Builder(destination); } - public static Builder builder(File file) throws IOException { + public static Builder builder(File file) { return new Builder(file); } - public static Builder builder(String fileName) throws IOException { + public static Builder builder(String fileName) { return builder(new File(fileName)); } @@ -190,11 +189,11 @@ public static class Builder extends WriteOptions.Builder { private char lookupWildcard = '?'; private char normalizedNewline = '\n'; - protected Builder(Destination destination) throws IOException { + protected Builder(Destination destination) { super(destination); } - protected Builder(File file) throws IOException { + protected Builder(File file) { super(file); } diff --git a/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriter.java b/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriter.java index 56bce22b8..b42f2b8f2 100644 --- a/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriter.java +++ b/core/src/main/java/tech/tablesaw/io/fixed/FixedWidthWriter.java @@ -16,7 +16,6 @@ import com.univocity.parsers.fixed.FixedWidthFormat; import com.univocity.parsers.fixed.FixedWidthWriterSettings; -import java.io.IOException; import java.io.Writer; import javax.annotation.concurrent.Immutable; import tech.tablesaw.api.Table; @@ -159,7 +158,7 @@ protected void skipIgnoreSettings( } @Override - public void write(Table table, Destination dest) throws IOException { + public void write(Table table, Destination dest) { write(table, FixedWidthWriteOptions.builder(dest).build()); } } diff --git a/core/src/test/java/tech/tablesaw/api/TableTest.java b/core/src/test/java/tech/tablesaw/api/TableTest.java index 6342649dd..0313e0111 100644 --- a/core/src/test/java/tech/tablesaw/api/TableTest.java +++ b/core/src/test/java/tech/tablesaw/api/TableTest.java @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.io.File; -import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.time.LocalDate; @@ -351,7 +350,7 @@ void testColumnCount() { } @Test - void testLast() throws IOException { + void testLast() { bush = bush.sortOn("date"); Table t1 = bush.last(3); assertEquals(3, t1.rowCount()); diff --git a/html/src/main/java/tech/tablesaw/io/html/HtmlWriter.java b/html/src/main/java/tech/tablesaw/io/html/HtmlWriter.java index 4d186e4fa..21a7793db 100644 --- a/html/src/main/java/tech/tablesaw/io/html/HtmlWriter.java +++ b/html/src/main/java/tech/tablesaw/io/html/HtmlWriter.java @@ -23,6 +23,7 @@ import tech.tablesaw.columns.Column; import tech.tablesaw.io.DataWriter; import tech.tablesaw.io.Destination; +import tech.tablesaw.io.RuntimeIOException; import tech.tablesaw.io.WriterRegistry; import tech.tablesaw.io.html.HtmlWriteOptions.ElementCreator; @@ -39,7 +40,7 @@ public static void register(WriterRegistry registry) { registry.registerOptions(HtmlWriteOptions.class, INSTANCE); } - public void write(Table table, HtmlWriteOptions options) throws IOException { + public void write(Table table, HtmlWriteOptions options) { ElementCreator elements = options.elementCreator(); Element html = elements.create("table"); html.appendChild(header(table.columns(), elements)); @@ -52,6 +53,8 @@ public void write(Table table, HtmlWriteOptions options) throws IOException { try (Writer writer = options.destination().createWriter()) { writer.write(html.toString()); + } catch (IOException e) { + throw new RuntimeIOException(e); } } @@ -84,7 +87,7 @@ private static Element header(List> cols, ElementCreator elements) { } @Override - public void write(Table table, Destination dest) throws IOException { + public void write(Table table, Destination dest) { write(table, HtmlWriteOptions.builder(dest).build()); } } diff --git a/json/src/main/java/tech/tablesaw/io/json/JsonWriter.java b/json/src/main/java/tech/tablesaw/io/json/JsonWriter.java index c9732054d..1997b0b7e 100644 --- a/json/src/main/java/tech/tablesaw/io/json/JsonWriter.java +++ b/json/src/main/java/tech/tablesaw/io/json/JsonWriter.java @@ -24,6 +24,7 @@ import tech.tablesaw.api.Table; import tech.tablesaw.io.DataWriter; import tech.tablesaw.io.Destination; +import tech.tablesaw.io.RuntimeIOException; import tech.tablesaw.io.WriterRegistry; public class JsonWriter implements DataWriter { @@ -41,7 +42,7 @@ public static void register(WriterRegistry registry) { registry.registerOptions(JsonWriteOptions.class, INSTANCE); } - public void write(Table table, JsonWriteOptions options) throws IOException { + public void write(Table table, JsonWriteOptions options) { ArrayNode output = mapper.createArrayNode(); if (options.asObjects()) { for (int r = 0; r < table.rowCount(); r++) { @@ -67,15 +68,16 @@ public void write(Table table, JsonWriteOptions options) throws IOException { output.add(row); } } - - String str = mapper.writeValueAsString(output); try (Writer writer = options.destination().createWriter()) { + String str = mapper.writeValueAsString(output); writer.write(str); + } catch (IOException e) { + throw new RuntimeIOException(e); } } @Override - public void write(Table table, Destination dest) throws IOException { + public void write(Table table, Destination dest) { write(table, JsonWriteOptions.builder(dest).build()); } }