Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed IOException from write interfaces #1030

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 14 additions & 13 deletions core/src/main/java/tech/tablesaw/io/DataFrameWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,51 @@ 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 <T extends WriteOptions> void usingOptions(T options) throws IOException {
public <T extends WriteOptions> void usingOptions(T options) {
DataWriter<T> dataWriter = registry.getWriterForOptions(options);
dataWriter.write(table, options);
}

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);
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/tech/tablesaw/io/DataWriter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package tech.tablesaw.io;

import java.io.IOException;
import tech.tablesaw.api.Table;

public interface DataWriter<O extends WriteOptions> {

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);
}
15 changes: 7 additions & 8 deletions core/src/main/java/tech/tablesaw/io/Destination.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/tech/tablesaw/io/WriteOptions.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.tablesaw.io;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/tech/tablesaw/io/csv/CsvWriteOptions.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}

Expand All @@ -129,15 +128,15 @@ public static class Builder extends WriteOptions.Builder {
private DateTimeFormatter dateFormatter;
private Map<String, String> columnNameMap = new HashMap<>();

protected Builder(String fileName) throws IOException {
protected Builder(String fileName) {
super(Paths.get(fileName).toFile());
}

protected Builder(Destination dest) {
super(dest);
}

protected Builder(File file) throws IOException {
protected Builder(File file) {
super(file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
3 changes: 1 addition & 2 deletions core/src/test/java/tech/tablesaw/api/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
7 changes: 5 additions & 2 deletions html/src/main/java/tech/tablesaw/io/html/HtmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -84,7 +87,7 @@ private static Element header(List<Column<?>> 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());
}
}
10 changes: 6 additions & 4 deletions json/src/main/java/tech/tablesaw/io/json/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonWriteOptions> {
Expand All @@ -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++) {
Expand All @@ -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());
}
}