Skip to main content

toad_jni/java/net/
inetaddress.rs

1use java::Object;
2
3use crate::java;
4
5/// `java.net.Inet4Address`
6pub struct Inet4Address(java::lang::Object);
7java::object_newtype!(Inet4Address);
8impl java::Class for Inet4Address {
9  const PATH: &'static str = "java/net/Inet4Address";
10}
11
12/// `java.net.Inet6Address`
13pub struct Inet6Address(java::lang::Object);
14java::object_newtype!(Inet6Address);
15impl java::Class for Inet6Address {
16  const PATH: &'static str = "java/net/Inet6Address";
17}
18
19/// `java.net.InetAddress`
20#[allow(missing_docs)]
21pub enum InetAddress {
22  V4(Inet4Address),
23  V6(Inet6Address),
24}
25
26static INETADDRESS_GET_BY_ADDRESS: java::StaticMethod<InetAddress, fn(Vec<i8>) -> InetAddress> =
27  java::StaticMethod::new("getByAddress");
28
29macro_rules! to_net_impl {
30  ($ip_addr:ty, $self_:expr, $e:expr) => {{
31    let bytes = $self_.get_address($e);
32    match $self_ {
33      | Self::V4(_) => {
34        let bytes: [u8; 4] = bytes.as_slice().try_into().unwrap();
35        <$ip_addr>::from(bytes)
36      },
37      | Self::V6(_) => {
38        let bytes: [u8; 16] = bytes.as_slice().try_into().unwrap();
39        <$ip_addr>::from(bytes)
40      },
41    }
42  }};
43}
44
45macro_rules! from_net_impl {
46  ($ip_addr:ty, $e:expr, $addr:expr) => {{
47    type IpAddr = $ip_addr;
48    match $addr {
49      | IpAddr::V4(ip) => Self::new_ipv4($e, ip.octets()),
50      | IpAddr::V6(ip) => Self::new_ipv6($e, ip.octets()),
51    }
52  }};
53}
54
55impl InetAddress {
56  /// [`InetAddress getByAddress(byte[])`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/net/InetAddress.html#getByAddress(byte%5B%5D))
57  pub fn new_ipv4(e: &mut java::Env, addr: [u8; 4]) -> Self {
58    INETADDRESS_GET_BY_ADDRESS.invoke(e,
59                                      addr.iter()
60                                          .copied()
61                                          .map(|u| i8::from_be_bytes(u.to_be_bytes()))
62                                          .collect())
63  }
64
65  /// [`InetAddress getByAddress(byte[])`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/net/InetAddress.html#getByAddress(byte%5B%5D))
66  pub fn new_ipv6(e: &mut java::Env, addr: [u8; 16]) -> Self {
67    INETADDRESS_GET_BY_ADDRESS.invoke(e,
68                                      addr.iter()
69                                          .copied()
70                                          .map(|u| i8::from_be_bytes(u.to_be_bytes()))
71                                          .collect())
72  }
73
74  fn get_address(&self, e: &mut java::Env) -> Vec<u8> {
75    static GET_ADDRESS: java::Method<InetAddress, fn() -> Vec<i8>> =
76      java::Method::new("getAddress");
77
78    let bytes = GET_ADDRESS.invoke(e, self);
79    bytes.iter()
80         .copied()
81         .map(|i| u8::from_be_bytes(i.to_be_bytes()))
82         .collect::<Vec<u8>>()
83  }
84
85  /// Convert `InetAddress` to `std::net::IpAddr`
86  pub fn to_std(&self, e: &mut java::Env) -> std::net::IpAddr {
87    to_net_impl!(std::net::IpAddr, self, e)
88  }
89
90  /// Convert `InetAddress` to `no_std_net::IpAddr`
91  pub fn to_no_std(&self, e: &mut java::Env) -> no_std_net::IpAddr {
92    to_net_impl!(no_std_net::IpAddr, self, e)
93  }
94
95  /// Convert `std::net::IpAddr` to `InetAddress`
96  pub fn from_std(e: &mut java::Env, addr: std::net::IpAddr) -> Self {
97    from_net_impl!(std::net::IpAddr, e, addr)
98  }
99
100  /// Convert `no_std_net::IpAddr` to `InetAddress`
101  pub fn from_no_std(e: &mut java::Env, addr: no_std_net::IpAddr) -> Self {
102    from_net_impl!(no_std_net::IpAddr, e, addr)
103  }
104}
105
106impl java::Object for InetAddress {
107  fn upcast(e: &mut java::Env, jobj: java::lang::Object) -> Self {
108    if jobj.is_instance_of::<Inet4Address>(e) {
109      Self::V4(jobj.upcast_to::<Inet4Address>(e))
110    } else {
111      Self::V6(jobj.upcast_to::<Inet6Address>(e))
112    }
113  }
114
115  fn downcast(self, e: &mut java::Env) -> java::lang::Object {
116    match self {
117      | Self::V4(o) => o.downcast(e),
118      | Self::V6(o) => o.downcast(e),
119    }
120  }
121
122  fn downcast_ref(&self, e: &mut java::Env) -> java::lang::Object {
123    match self {
124      | Self::V4(o) => o.downcast_ref(e),
125      | Self::V6(o) => o.downcast_ref(e),
126    }
127  }
128}
129
130impl java::Class for InetAddress {
131  const PATH: &'static str = "java/net/InetAddress";
132}