Move Writer creation out of WriterImpls

This commit is contained in:
AsamK 2021-11-07 15:13:53 +01:00
parent b5eef3ccad
commit 5dd602614c
4 changed files with 23 additions and 22 deletions

View file

@ -32,8 +32,11 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -105,9 +108,10 @@ public class App {
var outputType = outputTypeInput == null var outputType = outputTypeInput == null
? command.getSupportedOutputTypes().stream().findFirst().orElse(null) ? command.getSupportedOutputTypes().stream().findFirst().orElse(null)
: outputTypeInput; : outputTypeInput;
var writer = new BufferedWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()));
var outputWriter = outputType == null var outputWriter = outputType == null
? null ? null
: outputType == OutputType.JSON ? new JsonWriterImpl(System.out) : new PlainTextWriterImpl(System.out); : outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer);
if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) { if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) {
throw new UserErrorException("Command doesn't support output type " + outputType); throw new UserErrorException("Command doesn't support output type " + outputType);

View file

@ -5,22 +5,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class JsonWriterImpl implements JsonWriter { public class JsonWriterImpl implements JsonWriter {
private final Writer writer; private final Writer writer;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
public JsonWriterImpl(final OutputStream outputStream) { public JsonWriterImpl(final Writer writer) {
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); this.writer = writer;
this.objectMapper = Util.createJsonObjectMapper();
objectMapper = Util.createJsonObjectMapper();
} }
public synchronized void write(final Object object) { public synchronized void write(final Object object) {

View file

@ -2,10 +2,7 @@ package org.asamk.signal;
import org.slf4j.helpers.MessageFormatter; import org.slf4j.helpers.MessageFormatter;
import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
public final class PlainTextWriterImpl implements PlainTextWriter { public final class PlainTextWriterImpl implements PlainTextWriter {
@ -14,8 +11,8 @@ public final class PlainTextWriterImpl implements PlainTextWriter {
private PlainTextWriter indentedWriter; private PlainTextWriter indentedWriter;
public PlainTextWriterImpl(final OutputStream outputStream) { public PlainTextWriterImpl(final Writer writer) {
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream)); this.writer = writer;
} }
@Override @Override

View file

@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -49,16 +50,20 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
m.setIgnoreAttachments(ignoreAttachments); m.setIgnoreAttachments(ignoreAttachments);
final var jsonOutputWriter = (JsonWriter) outputWriter; final var jsonOutputWriter = (JsonWriter) outputWriter;
final var reader = new BufferedReader(new InputStreamReader(System.in)); final Supplier<String> lineSupplier = getLineSupplier(new InputStreamReader(System.in));
final Supplier<String> lineSupplier = () -> {
try {
return reader.readLine();
} catch (IOException e) {
throw new AssertionError(e);
}
};
final var handler = new SignalJsonRpcDispatcherHandler(m, jsonOutputWriter, lineSupplier); final var handler = new SignalJsonRpcDispatcherHandler(m, jsonOutputWriter, lineSupplier);
handler.handleConnection(); handler.handleConnection();
private Supplier<String> getLineSupplier(final Reader reader) {
final var bufferedReader = new BufferedReader(reader);
return () -> {
try {
return bufferedReader.readLine();
} catch (IOException e) {
logger.error("Error occurred while reading line", e);
return null;
}
};
} }
} }