aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/Devices.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/invalid/lena/scrcpy/Devices.java')
-rw-r--r--app/src/main/java/invalid/lena/scrcpy/Devices.java44
1 files changed, 43 insertions, 1 deletions
diff --git a/app/src/main/java/invalid/lena/scrcpy/Devices.java b/app/src/main/java/invalid/lena/scrcpy/Devices.java
index c3e5141..0ba61a3 100644
--- a/app/src/main/java/invalid/lena/scrcpy/Devices.java
+++ b/app/src/main/java/invalid/lena/scrcpy/Devices.java
@@ -30,9 +30,13 @@ public final class Devices {
this.port = port;
}
+ // Bracket IPv6 literals so the port stays unambiguous. Round-trips
+ // through parseAddress().
@Override
public String toString() {
- return host + ":" + port;
+ return host.indexOf(':') < 0
+ ? host + ":" + port
+ : "[" + host + "]:" + port;
}
@Override
@@ -49,6 +53,44 @@ public final class Devices {
private Devices() {}
+ // Parse "host:port" exactly as the target's Wireless debugging screen
+ // prints it. IPv6 literals must be bracketed ("[fe80::1]:5555") because
+ // the address itself contains colons; an unbracketed one is rejected
+ // rather than silently split at the wrong colon. Returns null on
+ // anything malformed - the only caller is a text field.
+ public static Device parseAddress(String s) {
+ if (s == null) return null;
+ s = s.trim();
+ String host, port;
+ if (s.startsWith("[")) {
+ int end = s.indexOf(']');
+ if (end < 0 || !s.startsWith("]:", end)) return null;
+ host = s.substring(1, end);
+ port = s.substring(end + 2);
+ } else {
+ int colon = s.indexOf(':');
+ if (colon < 0 || colon != s.lastIndexOf(':')) return null;
+ host = s.substring(0, colon);
+ port = s.substring(colon + 1);
+ }
+ int p = parsePort(port);
+ if (host.isEmpty() || p < 0) return null;
+ return new Device(host, p);
+ }
+
+ // Digits only, 1-65535. Returns -1 if it is not a usable port.
+ // Integer.parseInt() alone would accept "+5555" and " 5555".
+ public static int parsePort(String s) {
+ if (s == null) return -1;
+ s = s.trim();
+ if (s.isEmpty() || s.length() > 5) return -1;
+ for (int i = 0; i < s.length(); i++) {
+ if (s.charAt(i) < '0' || s.charAt(i) > '9') return -1;
+ }
+ int p = Integer.parseInt(s);
+ return p >= 1 && p <= 65535 ? p : -1;
+ }
+
// Pure-java parse: returns whatever rows are well-formed; logs and
// skips anything malformed instead of nuking the list.
public static List<Device> parse(String json) {