mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Refactor ReceiveMessageHandler
Introduce PlainTextWriter to improve indentation handling.
This commit is contained in:
parent
6c33a89f82
commit
03c30519b1
6 changed files with 641 additions and 507 deletions
|
@ -33,7 +33,7 @@ public abstract class GroupId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public GroupId(final byte[] id) {
|
protected GroupId(final byte[] id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,18 +6,20 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Writer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class JsonWriter {
|
public class JsonWriter {
|
||||||
|
|
||||||
private final OutputStreamWriter writer;
|
private final Writer writer;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
public JsonWriter(final OutputStream writer) {
|
public JsonWriter(final OutputStream writer) {
|
||||||
this.writer = new OutputStreamWriter(writer, StandardCharsets.UTF_8);
|
this.writer = new BufferedWriter(new OutputStreamWriter(writer, StandardCharsets.UTF_8));
|
||||||
|
|
||||||
objectMapper = new ObjectMapper();
|
objectMapper = new ObjectMapper();
|
||||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
|
||||||
|
|
23
src/main/java/org/asamk/signal/PlainTextWriter.java
Normal file
23
src/main/java/org/asamk/signal/PlainTextWriter.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package org.asamk.signal;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface PlainTextWriter {
|
||||||
|
|
||||||
|
void println(String format, Object... args) throws IOException;
|
||||||
|
|
||||||
|
PlainTextWriter indentedWriter();
|
||||||
|
|
||||||
|
default void println() throws IOException {
|
||||||
|
println("");
|
||||||
|
}
|
||||||
|
|
||||||
|
default void indent(final WriterConsumer subWriter) throws IOException {
|
||||||
|
subWriter.consume(indentedWriter());
|
||||||
|
}
|
||||||
|
|
||||||
|
interface WriterConsumer {
|
||||||
|
|
||||||
|
void consume(PlainTextWriter writer) throws IOException;
|
||||||
|
}
|
||||||
|
}
|
67
src/main/java/org/asamk/signal/PlainTextWriterImpl.java
Normal file
67
src/main/java/org/asamk/signal/PlainTextWriterImpl.java
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
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 {
|
||||||
|
|
||||||
|
private final Writer writer;
|
||||||
|
|
||||||
|
private PlainTextWriter indentedWriter;
|
||||||
|
|
||||||
|
public PlainTextWriterImpl(final OutputStream outputStream) {
|
||||||
|
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void println(String format, Object... args) throws IOException {
|
||||||
|
final String message = MessageFormatter.arrayFormat(format, args).getMessage();
|
||||||
|
|
||||||
|
writer.write(message);
|
||||||
|
writer.write(System.lineSeparator());
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PlainTextWriter indentedWriter() {
|
||||||
|
if (indentedWriter == null) {
|
||||||
|
indentedWriter = new IndentedPlainTextWriter(this, writer);
|
||||||
|
}
|
||||||
|
return indentedWriter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class IndentedPlainTextWriter implements PlainTextWriter {
|
||||||
|
|
||||||
|
private final static int INDENTATION = 2;
|
||||||
|
|
||||||
|
private final String spaces = " ".repeat(INDENTATION);
|
||||||
|
private final PlainTextWriter plainTextWriter;
|
||||||
|
private final Writer writer;
|
||||||
|
|
||||||
|
private PlainTextWriter indentedWriter;
|
||||||
|
|
||||||
|
private IndentedPlainTextWriter(final PlainTextWriter plainTextWriter, final Writer writer) {
|
||||||
|
this.plainTextWriter = plainTextWriter;
|
||||||
|
this.writer = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void println(final String format, final Object... args) throws IOException {
|
||||||
|
writer.write(spaces);
|
||||||
|
plainTextWriter.println(format, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PlainTextWriter indentedWriter() {
|
||||||
|
if (indentedWriter == null) {
|
||||||
|
indentedWriter = new IndentedPlainTextWriter(this, writer);
|
||||||
|
}
|
||||||
|
return indentedWriter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load diff
|
@ -14,7 +14,7 @@ public class DateUtils {
|
||||||
|
|
||||||
public static String formatTimestamp(long timestamp) {
|
public static String formatTimestamp(long timestamp) {
|
||||||
Date date = new Date(timestamp);
|
Date date = new Date(timestamp);
|
||||||
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
|
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); // Quoted "Z" to indicate UTC, no timezone offset
|
||||||
df.setTimeZone(tzUTC);
|
df.setTimeZone(tzUTC);
|
||||||
return timestamp + " (" + df.format(date) + ")";
|
return timestamp + " (" + df.format(date) + ")";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue