]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/util/IOUtils.java
Recreate recipient database with aci column
[signal-cli] / src / main / java / org / asamk / signal / util / IOUtils.java
index b4c4c6dd883f1ea24cc81df9ed99283e62078971..7f93417f12e06aedf6efb1d60b2dbf345f45edcb 100644 (file)
@@ -15,6 +15,7 @@ import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.net.StandardProtocolFamily;
 import java.net.UnixDomainSocketAddress;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 import java.nio.charset.Charset;
@@ -34,11 +35,16 @@ import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;
 
 public class IOUtils {
 
-    private final static Logger logger = LoggerFactory.getLogger(IOUtils.class);
+    private static final Logger logger = LoggerFactory.getLogger(IOUtils.class);
 
     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 {
         var output = new StringWriter();
         var buffer = new byte[4096];
@@ -88,6 +94,9 @@ public class IOUtils {
         return () -> {
             try {
                 return bufferedReader.readLine();
+            } catch (ClosedChannelException ignored) {
+                logger.trace("Line supplier has been interrupted.");
+                return null;
             } catch (IOException e) {
                 logger.error("Error occurred while reading line", e);
                 return null;
@@ -98,25 +107,31 @@ public class IOUtils {
     public static InetSocketAddress parseInetSocketAddress(final String tcpAddress) throws UserErrorException {
         final var colonIndex = tcpAddress.lastIndexOf(':');
         if (colonIndex < 0) {
-            throw new UserErrorException("Invalid tcp bind address: " + tcpAddress);
+            throw new UserErrorException("Invalid tcp bind address (expected host:port): " + tcpAddress);
         }
-        final String host = tcpAddress.substring(0, colonIndex);
+        final var host = tcpAddress.substring(0, colonIndex);
+        final var portString = tcpAddress.substring(colonIndex + 1);
+
         final int port;
         try {
-            port = Integer.parseInt(tcpAddress.substring(colonIndex + 1));
+            port = Integer.parseInt(portString);
         } catch (NumberFormatException e) {
-            throw new UserErrorException("Invalid tcp bind address: " + tcpAddress, e);
+            throw new UserErrorException("Invalid tcp port: " + portString, e);
+        }
+        final var socketAddress = new InetSocketAddress(host, port);
+        if (socketAddress.isUnresolved()) {
+            throw new UserErrorException("Invalid tcp bind address, invalid host: " + host);
         }
-        return new InetSocketAddress(host, port);
+        return socketAddress;
     }
 
-    public static UnixDomainPrincipal getUnixDomainPrincipal(final SocketChannel channel) throws IOException {
+    public static String getUnixDomainPrincipal(final SocketChannel channel) throws IOException {
         UnixDomainPrincipal principal = null;
         try {
             principal = channel.getOption(ExtendedSocketOptions.SO_PEERCRED);
-        } catch (UnsupportedOperationException ignored) {
+        } catch (UnsupportedOperationException | NoClassDefFoundError ignored) {
         }
-        return principal;
+        return principal == null ? null : principal.toString();
     }
 
     public static ServerSocketChannel bindSocket(final SocketAddress address) throws IOErrorException {
@@ -127,10 +142,10 @@ public class IOUtils {
                     ? ServerSocketChannel.open(StandardProtocolFamily.UNIX)
                     : ServerSocketChannel.open();
             serverChannel.bind(address);
-            logger.info("Listening on socket: " + address);
+            logger.debug("Listening on socket: " + address);
             postBind(address);
         } catch (IOException e) {
-            throw new IOErrorException("Failed to bind socket: " + e.getMessage(), e);
+            throw new IOErrorException("Failed to bind socket " + address + ": " + e.getMessage(), e);
         }
         return serverChannel;
     }