All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.activej.serializer.def.impl.Inet4AddressSerializerDef Maven / Gradle / Ivy

Go to download

Extremely fast and space-efficient serializers. Implemented using bytecode engineering.

There is a newer version: 6.0-rc2
Show newest version
/*
 * Copyright (C) 2020 ActiveJ LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.activej.serializer.def.impl;

import io.activej.common.annotation.ExposedInternals;
import io.activej.serializer.*;
import io.activej.serializer.def.SimpleSerializerDef;

import java.net.Inet4Address;
import java.net.UnknownHostException;

@ExposedInternals
public final class Inet4AddressSerializerDef extends SimpleSerializerDef {
	@Override
	protected BinarySerializer createSerializer(int version, CompatibilityLevel compatibilityLevel) {
		return new BinarySerializer<>() {
			@Override
			public void encode(BinaryOutput out, Inet4Address address) {
				out.write(address.getAddress());
			}

			@Override
			public Inet4Address decode(BinaryInput in) throws CorruptedDataException {
				byte[] addressBytes = new byte[4];
				in.read(addressBytes);

				try {
					return (Inet4Address) Inet4Address.getByAddress(addressBytes);
				} catch (UnknownHostException e) {
					throw new CorruptedDataException(e.getMessage());
				}
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy