mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Move Writer creation out of WriterImpls
This commit is contained in:
parent
b5eef3ccad
commit
5dd602614c
4 changed files with 23 additions and 22 deletions
|
@ -32,8 +32,11 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -105,9 +108,10 @@ public class App {
|
|||
var outputType = outputTypeInput == null
|
||||
? command.getSupportedOutputTypes().stream().findFirst().orElse(null)
|
||||
: outputTypeInput;
|
||||
var writer = new BufferedWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()));
|
||||
var outputWriter = outputType == 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)) {
|
||||
throw new UserErrorException("Command doesn't support output type " + outputType);
|
||||
|
|
|
@ -5,22 +5,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
|
||||
import org.asamk.signal.util.Util;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JsonWriterImpl implements JsonWriter {
|
||||
|
||||
private final Writer writer;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JsonWriterImpl(final OutputStream outputStream) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
|
||||
|
||||
objectMapper = Util.createJsonObjectMapper();
|
||||
public JsonWriterImpl(final Writer writer) {
|
||||
this.writer = writer;
|
||||
this.objectMapper = Util.createJsonObjectMapper();
|
||||
}
|
||||
|
||||
public synchronized void write(final Object object) {
|
||||
|
|
|
@ -2,10 +2,7 @@ package org.asamk.signal;
|
|||
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public final class PlainTextWriterImpl implements PlainTextWriter {
|
||||
|
@ -14,8 +11,8 @@ public final class PlainTextWriterImpl implements PlainTextWriter {
|
|||
|
||||
private PlainTextWriter indentedWriter;
|
||||
|
||||
public PlainTextWriterImpl(final OutputStream outputStream) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
||||
public PlainTextWriterImpl(final Writer writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
@ -49,16 +50,20 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
|
|||
m.setIgnoreAttachments(ignoreAttachments);
|
||||
|
||||
final var jsonOutputWriter = (JsonWriter) outputWriter;
|
||||
final var reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
final Supplier<String> lineSupplier = () -> {
|
||||
try {
|
||||
return reader.readLine();
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
};
|
||||
final Supplier<String> lineSupplier = getLineSupplier(new InputStreamReader(System.in));
|
||||
|
||||
final var handler = new SignalJsonRpcDispatcherHandler(m, jsonOutputWriter, lineSupplier);
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue