Use console charset for reading/writing to stdin/out

This commit is contained in:
AsamK 2022-05-26 14:49:16 +02:00
parent 425e451237
commit e03c48e0ae
4 changed files with 9 additions and 5 deletions

View file

@ -46,7 +46,6 @@ 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.io.OutputStreamWriter;
import java.nio.charset.Charset;
import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS; import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS;
@ -123,7 +122,7 @@ 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 writer = new BufferedWriter(new OutputStreamWriter(System.out, IOUtils.getConsoleCharset()));
var outputWriter = outputType == null var outputWriter = outputType == null
? null ? null
: outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer); : outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer);

View file

@ -48,7 +48,8 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
m.setIgnoreAttachments(ignoreAttachments); m.setIgnoreAttachments(ignoreAttachments);
final var jsonOutputWriter = (JsonWriter) outputWriter; final var jsonOutputWriter = (JsonWriter) outputWriter;
final Supplier<String> lineSupplier = IOUtils.getLineSupplier(new InputStreamReader(System.in)); final Supplier<String> lineSupplier = IOUtils.getLineSupplier(new InputStreamReader(System.in,
IOUtils.getConsoleCharset()));
final var handler = new SignalJsonRpcDispatcherHandler(jsonOutputWriter, lineSupplier, false); final var handler = new SignalJsonRpcDispatcherHandler(jsonOutputWriter, lineSupplier, false);
handler.handleConnection(m); handler.handleConnection(m);

View file

@ -24,7 +24,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -120,7 +119,7 @@ public class SendCommand implements JsonRpcLocalCommand {
if (readMessageFromStdin || (messageText == null && sticker == null)) { if (readMessageFromStdin || (messageText == null && sticker == null)) {
logger.debug("Reading message from stdin..."); logger.debug("Reading message from stdin...");
try { try {
messageText = IOUtils.readAll(System.in, Charset.defaultCharset()); messageText = IOUtils.readAll(System.in, IOUtils.getConsoleCharset());
} catch (IOException e) { } catch (IOException e) {
throw new UserErrorException("Failed to read message from stdin: " + e.getMessage()); throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
} }

View file

@ -39,6 +39,11 @@ public class IOUtils {
private IOUtils() { private IOUtils() {
} }
public static Charset getConsoleCharset() {
final var console = System.console();
return console == null ? Charset.defaultCharset() : console.charset();
}
public static String readAll(InputStream in, Charset charset) throws IOException { public static String readAll(InputStream in, Charset charset) throws IOException {
var output = new StringWriter(); var output = new StringWriter();
var buffer = new byte[4096]; var buffer = new byte[4096];