diff --git a/src/dorkbox/serializers/ArraysAsListSerializer.java b/src/dorkbox/serializers/ArraysAsListSerializer.java deleted file mode 100644 index 1c7ebf7..0000000 --- a/src/dorkbox/serializers/ArraysAsListSerializer.java +++ /dev/null @@ -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...)}. - *

- * 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. - *

- * - * @author Martin Grotzke - */ -public -class ArraysAsListSerializer extends Serializer> { - - public - ArraysAsListSerializer() { - } - - @Override - public - List read(final Kryo kryo, final Input input, final Class> 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; - } -} diff --git a/src/dorkbox/serializers/CollectionsSingletonListSerializer.java b/src/dorkbox/serializers/CollectionsSingletonListSerializer.java deleted file mode 100644 index 3eac576..0000000 --- a/src/dorkbox/serializers/CollectionsSingletonListSerializer.java +++ /dev/null @@ -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 "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.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)}. - *

- * Note: This serializer does not support cyclic references, if a serialized object - * is part of a cycle this might cause an error during deserialization. - *

- * - * @author Martin Grotzke - */ -public class CollectionsSingletonListSerializer extends Serializer> { - - public CollectionsSingletonListSerializer() { - } - - @Override - public List read(final Kryo kryo, final Input input, final Class> 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); - } -} diff --git a/src/dorkbox/serializers/CollectionsSingletonMapSerializer.java b/src/dorkbox/serializers/CollectionsSingletonMapSerializer.java deleted file mode 100644 index b090bf6..0000000 --- a/src/dorkbox/serializers/CollectionsSingletonMapSerializer.java +++ /dev/null @@ -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 "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.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)}. - *

- * Note: This serializer does not support cyclic references, if a serialized object - * is part of a cycle this might cause an error during deserialization. - *

- * - * @author Martin Grotzke - */ -public class CollectionsSingletonMapSerializer extends Serializer> { - - public CollectionsSingletonMapSerializer() { - setImmutable(true); - } - - @Override - public Map read(final Kryo kryo, final Input input, final Class> 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() ); - } - - -} diff --git a/src/dorkbox/serializers/CollectionsSingletonSetSerializer.java b/src/dorkbox/serializers/CollectionsSingletonSetSerializer.java deleted file mode 100644 index 5224097..0000000 --- a/src/dorkbox/serializers/CollectionsSingletonSetSerializer.java +++ /dev/null @@ -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 "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.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)}. - *

- * Note: This serializer does not support cyclic references, if a serialized object - * is part of a cycle this might cause an error during deserialization. - *

- * - * @author Martin Grotzke - */ -public class CollectionsSingletonSetSerializer extends Serializer> { - - public CollectionsSingletonSetSerializer() { - setImmutable(true); - } - - @Override - public Set read(final Kryo kryo, final Input input, final Class> 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() ); - } -} diff --git a/src/dorkbox/serializers/EnumSetSerializer.java b/src/dorkbox/serializers/EnumSetSerializer.java deleted file mode 100644 index f81f013..0000000 --- a/src/dorkbox/serializers/EnumSetSerializer.java +++ /dev/null @@ -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 "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.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>> { - - @Override - public - EnumSet> copy(final Kryo kryo, final EnumSet> original) { - return original.clone(); - } - - @Override - public - EnumSet read(final Kryo kryo, final Input input, final Class>> type) { - final Class 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> set) { - if (set.isEmpty()) { - EnumSet> tmp = EnumSet.complementOf(set); - if (tmp.isEmpty()) throw new KryoException("An EnumSet must have a defined Enum to be serialized."); - Class> type = (Class>) tmp.iterator() - .next() - .getDeclaringClass(); - kryo.writeClass(output, type); - output.writeInt(0, true); - } - else { - Class> type = (Class>) set.iterator() - .next() - .getDeclaringClass(); - kryo.writeClass(output, type); - output.writeInt(set.size(), true); - - for (final Enum item : set) { - output.writeInt(item.ordinal(), true); - } - } - } -} diff --git a/src/dorkbox/serializers/SerializationDefaults.kt b/src/dorkbox/serializers/SerializationDefaults.kt deleted file mode 100644 index 5b2d32a..0000000 --- a/src/dorkbox/serializers/SerializationDefaults.kt +++ /dev/null @@ -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, Serializer<*>?> { - val registeredClasses: MutableMap, 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::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::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array::class.java) - kryo.register(Array>::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::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().javaClass) - kryo.register(emptySet().javaClass) - kryo.register(emptyMap().javaClass) - kryo.register(Collections.EMPTY_LIST.javaClass) - kryo.register(Collections.EMPTY_SET.javaClass) - kryo.register(Collections.EMPTY_MAP.javaClass) - - kryo.register(Collections.emptyNavigableSet().javaClass) - kryo.register(Collections.emptyNavigableMap().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) - } -} diff --git a/src/dorkbox/serializers/URISerializer.kt b/src/dorkbox/serializers/URISerializer.kt deleted file mode 100644 index 394dd41..0000000 --- a/src/dorkbox/serializers/URISerializer.kt +++ /dev/null @@ -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() { - 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): URI { - return URI.create(input.readString()) - } -} diff --git a/src/dorkbox/serializers/UUIDSerializer.kt b/src/dorkbox/serializers/UUIDSerializer.kt deleted file mode 100644 index 4b4f4fd..0000000 --- a/src/dorkbox/serializers/UUIDSerializer.kt +++ /dev/null @@ -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() { - 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): UUID { - return UUID(input.readLong(), input.readLong()) - } -}