Removed serializers that were added to kryo 5.5.0

master
Robinson 2023-06-25 19:23:34 +02:00
parent 88e5b96c13
commit 2c704ac307
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
8 changed files with 0 additions and 579 deletions

View File

@ -1,137 +0,0 @@
/*
* Copyright 2021 dorkbox, 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 dorkbox.serializers;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* A kryo {@link Serializer} for lists created via {@link Arrays#asList(Object...)}.
* <p>
* Note: This serializer does not support cyclic references, so if one of the objects
* gets set the list as attribute this might cause an error during deserialization.
* </p>
*
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/
public
class ArraysAsListSerializer extends Serializer<List<?>> {
public
ArraysAsListSerializer() {
}
@Override
public
List<?> read(final Kryo kryo, final Input input, final Class<? extends List<?>> type) {
final int length = input.readInt(true);
Class<?> componentType = kryo.readClass(input)
.getType();
if (componentType.isPrimitive()) {
componentType = getPrimitiveWrapperClass(componentType);
}
try {
final Object items = Array.newInstance(componentType, length);
for (int i = 0; i < length; i++) {
Array.set(items, i, kryo.readClassAndObject(input));
}
return Arrays.asList((Object[]) items);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public
void write(final Kryo kryo, final Output output, final List<?> obj) {
try {
int size = obj.size();
output.writeInt(size, true);
if (size == 0) {
final Class<?> componentType = obj.toArray()
.getClass()
.getComponentType();
kryo.writeClass(output, componentType);
}
else {
kryo.writeClass(output,
obj.get(0)
.getClass());
for (final Object item : obj) {
kryo.writeClassAndObject(output, item);
}
}
} catch (final RuntimeException e) {
// Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write...
// handles SerializationException specifically (resizing the buffer)...
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public
List<?> copy(Kryo kryo, List<?> original) {
try {
Object[] object = original.toArray();
kryo.reference(object);
Object[] arrayCopy = kryo.copy(object);
return Arrays.asList(arrayCopy);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
private static
Class<?> getPrimitiveWrapperClass(final Class<?> c) {
if (c.isPrimitive()) {
if (c.equals(Long.TYPE)) {
return Long.class;
}
else if (c.equals(Integer.TYPE)) {
return Integer.class;
}
else if (c.equals(Double.TYPE)) {
return Double.class;
}
else if (c.equals(Float.TYPE)) {
return Float.class;
}
else if (c.equals(Boolean.TYPE)) {
return Boolean.class;
}
else if (c.equals(Character.TYPE)) {
return Character.class;
}
else if (c.equals(Short.TYPE)) {
return Short.class;
}
else if (c.equals(Byte.TYPE)) {
return Byte.class;
}
}
return c;
}
}

View File

@ -1,59 +0,0 @@
/*
* Copyright 2010 Martin Grotzke
*
* 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 &quot;AS IS&quot; 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 dorkbox.serializers;
import java.util.Collections;
import java.util.List;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* A kryo {@link Serializer} for {@link List}s created via {@link Collections#singletonList(Object)}.
* <p>
* Note: This serializer does not support cyclic references, if a serialized object
* is part of a cycle this might cause an error during deserialization.
* </p>
*
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/
public class CollectionsSingletonListSerializer extends Serializer<List<?>> {
public CollectionsSingletonListSerializer() {
}
@Override
public List<?> read(final Kryo kryo, final Input input, final Class<? extends List<?>> type) {
final Object obj = kryo.readClassAndObject( input );
return Collections.singletonList( obj );
}
@Override
public void write(final Kryo kryo, final Output output, final List<?> list) {
kryo.writeClassAndObject(output, list.get( 0 ));
}
@Override
public List<?> copy(Kryo kryo, List<?> original) {
Object singleton = original.get(0);
kryo.reference(singleton);
Object newSingleton = kryo.copy(singleton);
return Collections.singletonList(newSingleton);
}
}

View File

@ -1,59 +0,0 @@
/*
* Copyright 2010 Martin Grotzke
*
* 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 &quot;AS IS&quot; 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 dorkbox.serializers;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* A kryo {@link Serializer} for {@link List}s created via {@link Collections#singletonMap(Object, Object)}.
* <p>
* Note: This serializer does not support cyclic references, if a serialized object
* is part of a cycle this might cause an error during deserialization.
* </p>
*
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/
public class CollectionsSingletonMapSerializer extends Serializer<Map<?, ?>> {
public CollectionsSingletonMapSerializer() {
setImmutable(true);
}
@Override
public Map<?, ?> read(final Kryo kryo, final Input input, final Class<? extends Map<?, ?>> type) {
final Object key = kryo.readClassAndObject( input );
final Object value = kryo.readClassAndObject( input );
return Collections.singletonMap( key, value );
}
@Override
public void write(final Kryo kryo, final Output output, final Map<?, ?> map) {
final Entry<?, ?> entry = map.entrySet().iterator().next();
kryo.writeClassAndObject( output, entry.getKey() );
kryo.writeClassAndObject( output, entry.getValue() );
}
}

View File

@ -1,53 +0,0 @@
/*
* Copyright 2010 Martin Grotzke
*
* 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 &quot;AS IS&quot; 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 dorkbox.serializers;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* A kryo {@link Serializer} for {@link List}s created via {@link Collections#singletonList(Object)}.
* <p>
* Note: This serializer does not support cyclic references, if a serialized object
* is part of a cycle this might cause an error during deserialization.
* </p>
*
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/
public class CollectionsSingletonSetSerializer extends Serializer<Set<?>> {
public CollectionsSingletonSetSerializer() {
setImmutable(true);
}
@Override
public Set<?> read(final Kryo kryo, final Input input, final Class<? extends Set<?>> type) {
final Object obj = kryo.readClassAndObject( input );
return Collections.singleton( obj );
}
@Override
public void write(final Kryo kryo, final Output output, final Set<?> set) {
kryo.writeClassAndObject( output, set.iterator().next() );
}
}

View File

@ -1,78 +0,0 @@
/*
* Copyright 2010 Martin Grotzke
*
* 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 &quot;AS IS&quot; 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 dorkbox.serializers;
import java.util.EnumSet;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* A serializer for {@link EnumSet}s.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public
class EnumSetSerializer extends Serializer<EnumSet<? extends Enum<?>>> {
@Override
public
EnumSet<? extends Enum<?>> copy(final Kryo kryo, final EnumSet<? extends Enum<?>> original) {
return original.clone();
}
@Override
public
EnumSet read(final Kryo kryo, final Input input, final Class<? extends EnumSet<? extends Enum<?>>> type) {
final Class<Enum> elementType = kryo.readClass(input)
.getType();
final EnumSet result = EnumSet.noneOf(elementType);
final int size = input.readInt(true);
final Enum<?>[] enumConstants = elementType.getEnumConstants();
for (int i = 0; i < size; i++) {
result.add(enumConstants[input.readInt(true)]);
}
return result;
}
@Override
public
void write(final Kryo kryo, final Output output, final EnumSet<? extends Enum<?>> set) {
if (set.isEmpty()) {
EnumSet<? extends Enum<?>> tmp = EnumSet.complementOf(set);
if (tmp.isEmpty()) throw new KryoException("An EnumSet must have a defined Enum to be serialized.");
Class<Enum<?>> type = (Class<Enum<?>>) tmp.iterator()
.next()
.getDeclaringClass();
kryo.writeClass(output, type);
output.writeInt(0, true);
}
else {
Class<Enum<?>> type = (Class<Enum<?>>) set.iterator()
.next()
.getDeclaringClass();
kryo.writeClass(output, type);
output.writeInt(set.size(), true);
for (final Enum item : set) {
output.writeInt(item.ordinal(), true);
}
}
}
}

View File

@ -1,150 +0,0 @@
/*
* Copyright 2021 dorkbox, 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 dorkbox.serializers
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Registration
import com.esotericsoftware.kryo.Serializer
import java.io.File
import java.io.IOException
import java.math.BigDecimal
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.URI
import java.nio.file.Path
import java.util.*
import java.util.regex.Pattern
@Suppress("MemberVisibilityCanBePrivate")
object SerializationDefaults {
/**
* Gets the version number.
*/
const val version = "2.8"
init {
// Add this project to the updates system, which verifies this class + UUID + version information
dorkbox.updates.Updates.add(SerializationDefaults::class.java, "316353f5338341a8a3edc01d702703f8", version)
}
val regexSerializer by lazy { RegexSerializer() }
val uriSerializer by lazy { URISerializer() }
val uuidSerializer by lazy { UUIDSerializer() }
val collectionsSingletonMapSerializer by lazy { CollectionsSingletonMapSerializer() }
val collectionsSingletonListSerializer by lazy { CollectionsSingletonListSerializer() }
val collectionsSingletonSetSerializer by lazy { CollectionsSingletonSetSerializer() }
val enumSetSerializer by lazy { EnumSetSerializer() }
val enumMapSerializer by lazy { EnumMapSerializer() }
val arraysAsListSerializer by lazy { ArraysAsListSerializer() }
val inet4AddressSerializer by lazy { Inet4AddressSerializer() }
val inet6AddressSerializer by lazy { Inet6AddressSerializer() }
val fileSerializer by lazy { FileSerializer() }
/**
* Allows for the kryo registration of sensible defaults in a common, well-used way. Uses a hashmap to return registration data
*/
fun register(): MutableMap<Class<*>, Serializer<*>?> {
val registeredClasses: MutableMap<Class<*>, Serializer<*>?> = mutableMapOf()
val kryo = object : Kryo() {
override fun register(type: Class<*>): Registration {
registeredClasses[type] = null
return super.register(type)
}
override fun register(type: Class<*>, serializer: Serializer<*>?): Registration {
registeredClasses[type] = serializer
return super.register(type, serializer)
}
}
register(kryo)
return registeredClasses
}
/**
* Allows for the kryo registration of sensible defaults in a common, well-used way.
*/
fun register(kryo: Kryo) {
// these are registered using the default serializers. We don't customize these, because we don't care about it.
kryo.register(String::class.java)
kryo.register(Array<String>::class.java)
kryo.register(IntArray::class.java)
kryo.register(ShortArray::class.java)
kryo.register(FloatArray::class.java)
kryo.register(DoubleArray::class.java)
kryo.register(LongArray::class.java)
kryo.register(ByteArray::class.java)
kryo.register(CharArray::class.java)
kryo.register(BooleanArray::class.java)
kryo.register(Array<Int>::class.java)
kryo.register(Array<Short>::class.java)
kryo.register(Array<Float>::class.java)
kryo.register(Array<Double>::class.java)
kryo.register(Array<Long>::class.java)
kryo.register(Array<Byte>::class.java)
kryo.register(Array<Char>::class.java)
kryo.register(Array<Boolean>::class.java)
kryo.register(Array<Any>::class.java)
kryo.register(Array<Array<Any>>::class.java)
kryo.register(Class::class.java)
kryo.register(Exception::class.java)
kryo.register(IOException::class.java)
kryo.register(RuntimeException::class.java)
kryo.register(NullPointerException::class.java)
kryo.register(BigDecimal::class.java)
kryo.register(BitSet::class.java)
// necessary for the transport of exceptions.
kryo.register(StackTraceElement::class.java)
kryo.register(Array<StackTraceElement>::class.java)
kryo.register(ArrayList::class.java)
kryo.register(HashMap::class.java)
kryo.register(HashSet::class.java)
kryo.register(EnumSet::class.java, enumSetSerializer)
kryo.register(EnumMap::class.java, enumMapSerializer)
kryo.register(Arrays.asList("").javaClass, arraysAsListSerializer)
kryo.register(emptyList<Any>().javaClass)
kryo.register(emptySet<Any>().javaClass)
kryo.register(emptyMap<Any, Any>().javaClass)
kryo.register(Collections.EMPTY_LIST.javaClass)
kryo.register(Collections.EMPTY_SET.javaClass)
kryo.register(Collections.EMPTY_MAP.javaClass)
kryo.register(Collections.emptyNavigableSet<Any>().javaClass)
kryo.register(Collections.emptyNavigableMap<Any, Any>().javaClass)
kryo.register(Collections.singletonMap("", "").javaClass, collectionsSingletonMapSerializer)
kryo.register(listOf("").javaClass, collectionsSingletonListSerializer)
kryo.register(setOf("").javaClass, collectionsSingletonSetSerializer)
kryo.register(Pattern::class.java, regexSerializer)
kryo.register(URI::class.java, uriSerializer)
kryo.register(UUID::class.java, uuidSerializer)
kryo.register(Inet4Address::class.java, inet4AddressSerializer)
kryo.register(Inet6Address::class.java, inet6AddressSerializer)
kryo.register(File::class.java, fileSerializer)
UnmodifiableCollectionsSerializer.registerSerializers(kryo)
SynchronizedCollectionsSerializer.registerSerializers(kryo)
}
}

View File

@ -1,21 +0,0 @@
package dorkbox.serializers
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.net.URI
class URISerializer : Serializer<URI>() {
init {
isImmutable = true
}
override fun write(kryo: Kryo, output: Output, uri: URI) {
output.writeString(uri.toString())
}
override fun read(kryo: Kryo, input: Input, uriClass: Class<out URI>): URI {
return URI.create(input.readString())
}
}

View File

@ -1,22 +0,0 @@
package dorkbox.serializers
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.util.*
class UUIDSerializer : Serializer<UUID>() {
init {
isImmutable = true
}
override fun write(kryo: Kryo, output: Output, uuid: UUID) {
output.writeLong(uuid.mostSignificantBits)
output.writeLong(uuid.leastSignificantBits)
}
override fun read(kryo: Kryo, input: Input, uuidClass: Class<out UUID>): UUID {
return UUID(input.readLong(), input.readLong())
}
}