Skip to content

Instantly share code, notes, and snippets.

@Kamillaova
Created April 25, 2023 12:35
Show Gist options
  • Save Kamillaova/c7a450bb49a807e251ca699bfee20ca8 to your computer and use it in GitHub Desktop.
Save Kamillaova/c7a450bb49a807e251ca699bfee20ca8 to your computer and use it in GitHub Desktop.
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.ImmutableSerializer;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
public final class InetAddressSerializers {
private InetAddressSerializers() {}
public static class Inet4AddresSerializer extends ImmutableSerializer<Inet4Address> {
public static final Inet4AddresSerializer INSTANCE = new Inet4AddresSerializer();
@Override
public void write(Kryo kryo, Output output, Inet4Address addr) {
output.writeBytes(addr.getAddress());
}
@Override
public Inet4Address read(Kryo kryo, Input input, Class<? extends Inet4Address> type) {
try {
return (Inet4Address) InetAddress.getByAddress(input.readBytes(4));
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e);
}
}
}
public static class Inet6AddresSerializer extends ImmutableSerializer<Inet6Address> {
public static final Inet6AddresSerializer INSTANCE = new Inet6AddresSerializer();
@Override
public void write(Kryo kryo, Output output, Inet6Address addr) {
output.writeBytes(addr.getAddress());
}
@Override
public Inet6Address read(Kryo kryo, Input input, Class<? extends Inet6Address> type) {
try {
return (Inet6Address) InetAddress.getByAddress(input.readBytes(16));
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment