Skip to main content

toad_jni/java/net/
inetsocketaddress.rs

1use java::Object;
2
3use super::{InetAddress, SocketAddress};
4use crate::java;
5
6/// `java.net.InetSocketAddress`
7pub struct InetSocketAddress(java::lang::Object);
8
9impl InetSocketAddress {
10  /// Downcast self to [`SocketAddress`]
11  pub fn as_socket_address(&self, e: &mut java::Env) -> SocketAddress {
12    self.downcast_ref(e).upcast_to(e)
13  }
14
15  /// Upcast [`SocketAddress`] to self
16  pub fn from_socket_address(e: &mut java::Env, addr: SocketAddress) -> Self {
17    addr.downcast_ref(e).upcast_to(e)
18  }
19
20  /// Create a new socket address, using the local wildcard address
21  /// as the IP address
22  ///
23  /// <https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/net/InetSocketAddress.html#<init>(int)>
24  pub fn new_wildcard_address(e: &mut java::Env, port: i32) -> Self {
25    static CTOR: java::Constructor<InetSocketAddress, fn(i32)> = java::Constructor::new();
26    CTOR.invoke(e, port)
27  }
28
29  /// Create a new socket address, resolving the hostname to an IP
30  /// address (unless the string is an IP literal)
31  ///
32  /// <https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/net/InetSocketAddress.html#%3Cinit%3E(java.lang.String,int)>
33  pub fn new_resolved(e: &mut java::Env, host: impl ToString, port: i32) -> Self {
34    static CTOR: java::Constructor<InetSocketAddress, fn(String, i32)> = java::Constructor::new();
35    CTOR.invoke(e, host.to_string(), port)
36  }
37
38  /// Create a new socket address from a known IP and port
39  ///
40  /// <https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/net/InetSocketAddress.html#<init>(java.net.InetAddress,int)>
41  pub fn new(e: &mut java::Env, addr: InetAddress, port: i32) -> Self {
42    static CTOR: java::Constructor<InetSocketAddress, fn(InetAddress, i32)> =
43      java::Constructor::new();
44    CTOR.invoke(e, addr, port)
45  }
46
47  /// Get the IP address
48  pub fn address(&self, e: &mut java::Env) -> InetAddress {
49    static GET_ADDRESS: java::Method<InetSocketAddress, fn() -> InetAddress> =
50      java::Method::new("getAddress");
51    GET_ADDRESS.invoke(e, self)
52  }
53
54  /// Get the port
55  pub fn port(&self, e: &mut java::Env) -> u16 {
56    static GET_PORT: java::Method<InetSocketAddress, fn() -> i32> = java::Method::new("getPort");
57    GET_PORT.invoke(e, self) as u16
58  }
59
60  /// Convert `InetSocketAddress` to `std::net::SocketAddr`
61  pub fn to_std(&self, e: &mut java::Env) -> std::net::SocketAddr {
62    std::net::SocketAddr::new(self.address(e).to_std(e), self.port(e))
63  }
64
65  /// Convert `InetSocketAddress` to `no_std_net::SocketAddr`
66  pub fn to_no_std(&self, e: &mut java::Env) -> no_std_net::SocketAddr {
67    no_std_net::SocketAddr::new(self.address(e).to_no_std(e), self.port(e))
68  }
69
70  /// Convert `std::net::SocketAddr` to `InetSocketAddress`
71  pub fn from_std(e: &mut java::Env, addr: std::net::SocketAddr) -> Self {
72    let ip = InetAddress::from_std(e, addr.ip());
73    Self::new(e, ip, addr.port() as i32)
74  }
75
76  /// Convert `std::net::SocketAddr` to `InetSocketAddress`
77  pub fn from_no_std(e: &mut java::Env, addr: no_std_net::SocketAddr) -> Self {
78    let ip = InetAddress::from_no_std(e, addr.ip());
79    Self::new(e, ip, addr.port() as i32)
80  }
81}
82
83java::object_newtype!(InetSocketAddress);
84impl java::Class for InetSocketAddress {
85  const PATH: &'static str = "java/net/InetSocketAddress";
86}