新增更多基础类

master
linbin 2024-09-13 11:31:41 +08:00
parent c97e6603ff
commit fb1b565f88
15 changed files with 578 additions and 102 deletions

View File

@ -51,9 +51,9 @@
<version>1.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.furyio</groupId>
<groupId>org.apache.fury</groupId>
<artifactId>fury-core</artifactId>
<version>0.4.1</version>
<version>0.7.1</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -42,9 +42,4 @@ public interface JfireSE
Object readByUnderInstanceIdFlag(ByteArray byteArray, byte flag);
Object readByNameIdContent(ByteArray byteArray, boolean refTracking);
Object readByIdInstanceId(ByteArray byteArray);
Object readByIdContent(ByteArray byteArray, boolean refTracking);
}

View File

@ -6,25 +6,90 @@ import lombok.experimental.Accessors;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
@Data
@Accessors(chain = true)
public class JfireSEConfig
{
private boolean refTracking = false;
private short staticClassId = 0;
private StaticClasInfo[] clasInfos = new StaticClasInfo[10];
private List<Class<?>> list = new LinkedList<>();
private Set<Class<?>> set = new HashSet<>();
private boolean refTracking = false;
private short staticClassId = 0;
private StaticClasInfo[] clasInfos = new StaticClasInfo[10];
private Set<Class<?>> set = new HashSet<>();
private static Set<Class<?>> NO_NEED_TRACK_CLASS = new HashSet<>();
static
{
NO_NEED_TRACK_CLASS.add(Integer.class);
NO_NEED_TRACK_CLASS.add(Long.class);
NO_NEED_TRACK_CLASS.add(Short.class);
NO_NEED_TRACK_CLASS.add(Byte.class);
NO_NEED_TRACK_CLASS.add(Character.class);
NO_NEED_TRACK_CLASS.add(Float.class);
NO_NEED_TRACK_CLASS.add(Double.class);
NO_NEED_TRACK_CLASS.add(Boolean.class);
NO_NEED_TRACK_CLASS.add(String.class);
NO_NEED_TRACK_CLASS.add(int[].class);
NO_NEED_TRACK_CLASS.add(long[].class);
NO_NEED_TRACK_CLASS.add(short[].class);
NO_NEED_TRACK_CLASS.add(byte[].class);
NO_NEED_TRACK_CLASS.add(char[].class);
NO_NEED_TRACK_CLASS.add(float[].class);
NO_NEED_TRACK_CLASS.add(double[].class);
NO_NEED_TRACK_CLASS.add(boolean[].class);
NO_NEED_TRACK_CLASS.add(Integer[].class);
NO_NEED_TRACK_CLASS.add(Long[].class);
NO_NEED_TRACK_CLASS.add(Byte[].class);
NO_NEED_TRACK_CLASS.add(Boolean[].class);
NO_NEED_TRACK_CLASS.add(Character[].class);
NO_NEED_TRACK_CLASS.add(Double[].class);
NO_NEED_TRACK_CLASS.add(Float[].class);
NO_NEED_TRACK_CLASS.add(String[].class);
NO_NEED_TRACK_CLASS.add(Date.class);
NO_NEED_TRACK_CLASS.add(java.sql.Date.class);
NO_NEED_TRACK_CLASS.add(Calendar.class);
}
public JfireSEConfig()
{
staticRegisterClass(int[].class);
staticRegisterClass(long[].class);
staticRegisterClass(byte[].class);
staticRegisterClass(boolean[].class);
staticRegisterClass(char[].class);
staticRegisterClass(short[].class);
staticRegisterClass(double[].class);
staticRegisterClass(float[].class);
staticRegisterClass(Integer[].class);
staticRegisterClass(Long[].class);
staticRegisterClass(Byte[].class);
staticRegisterClass(Boolean[].class);
staticRegisterClass(Character[].class);
staticRegisterClass(Short[].class);
staticRegisterClass(Double[].class);
staticRegisterClass(Float[].class);
staticRegisterClass(String[].class);
staticRegisterClass(String.class);
staticRegisterClass(HashSet.class);
staticRegisterClass(HashMap.class);
staticRegisterClass(ConcurrentHashMap.class);
staticRegisterClass(Byte.class);
staticRegisterClass(Integer.class);
staticRegisterClass(Character.class);
staticRegisterClass(Long.class);
staticRegisterClass(Float.class);
staticRegisterClass(Double.class);
staticRegisterClass(Boolean.class);
staticRegisterClass(Short.class);
staticRegisterClass(ArrayList.class);
staticRegisterClass(LinkedList.class);
staticRegisterClass(HashMap.class);
staticRegisterClass(LinkedHashMap.class);
staticRegisterClass(ConcurrentHashMap.class);
staticRegisterClass(TreeMap.class);
staticRegisterClass(HashSet.class);
staticRegisterClass(TreeSet.class);
staticRegisterClass(LinkedHashSet.class);
staticRegisterClass(ConcurrentSkipListSet.class);
staticRegisterClass(Date.class);
staticRegisterClass(java.sql.Date.class);
}
public JfireSEConfig refTracking()
@ -37,7 +102,6 @@ public class JfireSEConfig
{
if (set.add(clazz))
{
list.add(clazz);
return this;
}
else
@ -48,13 +112,13 @@ public class JfireSEConfig
public JfireSE build()
{
StaticClasInfo[] array = list.stream().map(this::resolve).toArray(StaticClasInfo[]::new);
StaticClasInfo[] array = set.stream().map(this::resolve).toArray(StaticClasInfo[]::new);
return new JfireSEImpl(refTracking, array);
}
private StaticClasInfo resolve(Class<?> clazz)
{
StaticClasInfo staticClasInfo = new StaticClasInfo(staticClassId, clazz, refTracking);
StaticClasInfo staticClasInfo = new StaticClasInfo(staticClassId, clazz, NO_NEED_TRACK_CLASS.contains(clazz) ? false : refTracking);
staticClassId++;
return staticClasInfo;
}

View File

@ -14,7 +14,7 @@ public class JfireSEImpl implements JfireSE
{
private final boolean refTracking;
private final int staticClassId;
private int dyncmicClassId;
private int dynamicClassId;
/**
* classInfo
*/
@ -31,13 +31,17 @@ public class JfireSEImpl implements JfireSE
public JfireSEImpl(boolean refTracking, StaticClasInfo[] staticClasInfos)
{
for (StaticClasInfo each : staticClasInfos)
{
each.setSerializer(serializerFactory.getSerializer(each.getClazz()));
}
this.refTracking = refTracking;
this.staticClassId = staticClasInfos.length - 1;
serializedClassInfos = new ClassInfo[staticClasInfos.length * 2];
deSerializedClassInfos = new ClassInfo[serializedClassInfos.length];
System.arraycopy(staticClasInfos, 0, serializedClassInfos, 0, staticClasInfos.length);
System.arraycopy(staticClasInfos, 0, deSerializedClassInfos, 0, staticClasInfos.length);
dyncmicClassId = staticClassId + 1;
dynamicClassId = staticClassId + 1;
}
@Override
@ -53,15 +57,15 @@ public class JfireSEImpl implements JfireSE
classInfoCache = classInfo;
return classInfo;
}
if (dyncmicClassId == serializedClassInfos.length)
if (dynamicClassId == serializedClassInfos.length)
{
ClassInfo[] tmp = new ClassInfo[serializedClassInfos.length * 2];
System.arraycopy(serializedClassInfos, 0, tmp, 0, serializedClassInfos.length);
serializedClassInfos = tmp;
}
DynamicClassInfo dynamicClassInfo = new DynamicClassInfo((short) dyncmicClassId, clazz, refTracking);
serializedClassInfos[dyncmicClassId] = dynamicClassInfo;
dyncmicClassId++;
DynamicClassInfo dynamicClassInfo = new DynamicClassInfo((short) dynamicClassId, clazz, refTracking);
serializedClassInfos[dynamicClassId] = dynamicClassInfo;
dynamicClassId++;
Serializer serializer = serializerFactory.getSerializer(clazz);
dynamicClassInfo.setSerializer(serializer);
classInfoCache = dynamicClassInfo;
@ -70,7 +74,7 @@ public class JfireSEImpl implements JfireSE
private void resetSerialized()
{
for (int i = staticClassId; i < dyncmicClassId; i++)
for (int i = staticClassId; i < dynamicClassId; i++)
{
serializedClassInfos[i].reset();
}
@ -208,27 +212,4 @@ public class JfireSEImpl implements JfireSE
default -> throw new RuntimeException("未知的序列化类型");
}
}
@Override
public Object readByNameIdContent(ByteArray byteArray, boolean refTracking)
{
byte[] classNameBytes = byteArray.readBytesWithSizeEmbedded();
int classId = byteArray.readPositiveVarInt();
ClassInfo classInfo = find(classNameBytes, classId);
return refTracking ? classInfo.readWithTrack(byteArray) : classInfo.readWithoutTrack(byteArray);
}
@Override
public Object readByIdInstanceId(ByteArray byteArray)
{
int classId = byteArray.readPositiveVarInt();
return find(classId).getInstanceById(byteArray.readPositiveVarInt());
}
@Override
public Object readByIdContent(ByteArray byteArray, boolean refTracking)
{
ClassInfo classInfo = find(byteArray.readPositiveVarInt());
return refTracking ? classInfo.readWithTrack(byteArray) : classInfo.readWithoutTrack(byteArray);
}
}

View File

@ -1,14 +1,12 @@
package com.jfirer.se2.serializer;
import com.jfirer.se2.JfireSE;
import com.jfirer.se2.serializer.impl.ArraySerializer;
import com.jfirer.se2.serializer.impl.BoxedArraySerializer;
import com.jfirer.se2.serializer.impl.BoxedTypeSerializer;
import com.jfirer.se2.serializer.impl.*;
import com.jfirer.se2.serializer.impl.ObjectSerializer.ObjectSerializer;
import com.jfirer.se2.serializer.impl.PrimitiveArraySerializer;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
public class SerializerFactory
{
@ -36,6 +34,26 @@ public class SerializerFactory
store.put(Float[].class, new BoxedArraySerializer.FloatArraySerializer());
store.put(String[].class, new BoxedArraySerializer.StringArraySerializer());
store.put(String.class, new BoxedTypeSerializer.StringSerializer());
store.put(Byte.class, new BoxedTypeSerializer.ByteSerializer());
store.put(Integer.class, new BoxedTypeSerializer.IntegerSerializer());
store.put(Character.class, new BoxedTypeSerializer.CharacterSerializer());
store.put(Long.class, new BoxedTypeSerializer.LongSerializer());
store.put(Float.class, new BoxedTypeSerializer.FloatSerializer());
store.put(Double.class, new BoxedTypeSerializer.FloatSerializer());
store.put(Boolean.class, new BoxedTypeSerializer.BooleanSerializer());
store.put(Short.class, new BoxedTypeSerializer.ShortSerializer());
store.put(ArrayList.class, new ListSerializer(jfireSE, ArrayList.class));
store.put(LinkedList.class, new ListSerializer(jfireSE, LinkedList.class));
store.put(HashMap.class, new MapSerializer(jfireSE, HashMap.class));
store.put(TreeMap.class, new MapSerializer(jfireSE, TreeMap.class));
store.put(ConcurrentHashMap.class, new MapSerializer(jfireSE, ConcurrentHashMap.class));
store.put(LinkedHashMap.class, new MapSerializer(jfireSE, LinkedHashMap.class));
store.put(HashSet.class, new SetSerializer(HashSet.class, jfireSE));
store.put(TreeSet.class, new SetSerializer(TreeSet.class, jfireSE));
store.put(ConcurrentSkipListSet.class, new SetSerializer(ConcurrentSkipListSet.class, jfireSE));
store.put(LinkedHashSet.class, new SetSerializer(LinkedHashSet.class, jfireSE));
store.put(Date.class, new DateSerializer());
store.put(java.sql.Date.class, new SqlDateSerializer());
}
public Serializer getSerializer(Class<?> clazz)

View File

@ -10,13 +10,13 @@ import java.lang.reflect.Array;
import static com.jfirer.se2.JfireSE.*;
public class ArraySerializer<T> implements Serializer
public class ArraySerializer implements Serializer
{
private Class<?> componentType;
private final Class<?> componentType;
private final ClassInfo typeDefinedClassInfo;
private JfireSE jfireSE;
private final JfireSE jfireSE;
public ArraySerializer(Class<T[]> clazz, JfireSE jfireSE)
public ArraySerializer(Class<?> clazz, JfireSE jfireSE)
{
this.jfireSE = jfireSE;
this.componentType = clazz.getComponentType();
@ -26,9 +26,9 @@ public class ArraySerializer<T> implements Serializer
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
T[] arr = (T[]) instance;
Object[] arr = (Object[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (T t : arr)
for (Object t : arr)
{
if (t == null)
{
@ -53,8 +53,8 @@ public class ArraySerializer<T> implements Serializer
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
int length = byteArray.readPositiveVarInt();
T[] arr = (T[]) Array.newInstance(componentType, length);
int length = byteArray.readPositiveVarInt();
Object[] arr = (Object[]) Array.newInstance(componentType, length);
if (refTracking != null)
{
refTracking.addTracking(arr);
@ -71,10 +71,10 @@ public class ArraySerializer<T> implements Serializer
switch (flag)
{
case NAME_ID_CONTENT_TRACK, NAME_ID_CONTENT_UN_TRACK, ID_INSTANCE_ID, ID_CONTENT_TRACK,
ID_CONTENT_UN_TRACK -> arr[i] = (T) jfireSE.readByUnderInstanceIdFlag(byteArray, flag);
case JfireSE.INSTANCE_ID -> arr[i] = (T) typeDefinedClassInfo.getInstanceById(byteArray.readPositiveVarInt());
case JfireSE.CONTENT_TRACK -> arr[i] = (T) typeDefinedClassInfo.readWithTrack(byteArray);
case JfireSE.CONTENT_UN_TRACK -> arr[i] = (T) typeDefinedClassInfo.readWithoutTrack(byteArray);
ID_CONTENT_UN_TRACK -> arr[i] = jfireSE.readByUnderInstanceIdFlag(byteArray, flag);
case JfireSE.INSTANCE_ID -> arr[i] = typeDefinedClassInfo.getInstanceById(byteArray.readPositiveVarInt());
case JfireSE.CONTENT_TRACK -> arr[i] = typeDefinedClassInfo.readWithTrack(byteArray);
case JfireSE.CONTENT_UN_TRACK -> arr[i] = typeDefinedClassInfo.readWithoutTrack(byteArray);
default -> throw new RuntimeException("未知的序列化类型");
}
}

View File

@ -5,18 +5,17 @@ import com.jfirer.se2.JfireSE;
import com.jfirer.se2.classinfo.RefTracking;
import com.jfirer.se2.serializer.Serializer;
public abstract class BoxedArraySerializer<T> implements Serializer
public abstract class BoxedArraySerializer implements Serializer
{
public static class IntegerArraySerializer extends BoxedArraySerializer<Integer[]>
public static class IntegerArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Integer[] arr = (Integer[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Integer each : arr)
{
Integer each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -53,16 +52,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
}
}
public static class LongArraySerializer extends BoxedArraySerializer<Long[]>
public static class LongArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Long[] arr = (Long[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Long each : arr)
{
Long each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -99,16 +97,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
}
}
public static class FloatArraySerializer extends BoxedArraySerializer<Float[]>
public static class FloatArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Float[] arr = (Float[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Float each : arr)
{
Float each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -145,16 +142,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
}
}
public static class DoubleArraySerializer extends BoxedArraySerializer<Double[]>
public static class DoubleArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Double[] arr = (Double[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Double each : arr)
{
Double each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -191,16 +187,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
}
}
public static class ByteArraySerializer extends BoxedArraySerializer<Byte[]>
public static class ByteArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Byte[] arr = (Byte[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Byte each : arr)
{
Byte each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -237,16 +232,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
}
}
public static class BooleanArraySerializer extends BoxedArraySerializer<Boolean[]>
public static class BooleanArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Boolean[] arr = (Boolean[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Boolean each : arr)
{
Boolean each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -283,16 +277,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
}
}
public static class CharArraySerializer extends BoxedArraySerializer<Character[]>
public static class CharArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Character[] arr = (Character[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Character each : arr)
{
Character each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -336,9 +329,8 @@ public abstract class BoxedArraySerializer<T> implements Serializer
{
Short[] arr = (Short[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (Short each : arr)
{
Short each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);
@ -374,16 +366,15 @@ public abstract class BoxedArraySerializer<T> implements Serializer
return arr;
}
}
public static class StringArraySerializer extends BoxedArraySerializer<String[]>
public static class StringArraySerializer extends BoxedArraySerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
String[] arr = (String[]) instance;
byteArray.writePositiveVarInt(arr.length);
for (int i = 0; i < arr.length; i++)
for (String each : arr)
{
String each = arr[i];
if (each == null)
{
byteArray.put(JfireSE.NULL);

View File

@ -17,12 +17,121 @@ public abstract class BoxedTypeSerializer implements Serializer
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
String s = byteArray.readString();
if (refTracking != null)
{
refTracking.addTracking(s);
}
return s;
return byteArray.readString();
}
}
public static class IntegerSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeVarInt((Integer) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.readVarInt();
}
}
public static class ShortSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeVarInt((Short) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return Short.valueOf((short) byteArray.readVarInt());
}
}
public static class ByteSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.put((Byte) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.get();
}
}
public static class LongSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeVarLong((Long) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.readVarLong();
}
}
public static class FloatSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeFloat((Float) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.readFloat();
}
}
public static class DoubleSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeDouble((Double) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.readDouble();
}
}
public static class BooleanSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeBoolean((Boolean) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.readBoolean();
}
}
public static class CharacterSerializer extends BoxedTypeSerializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeChar((Character) instance);
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return byteArray.readChar();
}
}
}

View File

@ -0,0 +1,20 @@
package com.jfirer.se2.serializer.impl;
import com.jfirer.se2.ByteArray;
import com.jfirer.se2.classinfo.RefTracking;
import com.jfirer.se2.serializer.Serializer;
public class DateSerializer implements Serializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeVarLong(((java.util.Date) instance).getTime());
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return new java.util.Date(byteArray.readVarLong());
}
}

View File

@ -0,0 +1,76 @@
package com.jfirer.se2.serializer.impl;
import com.jfirer.se2.ByteArray;
import com.jfirer.se2.JfireSE;
import com.jfirer.se2.classinfo.RefTracking;
import com.jfirer.se2.serializer.Serializer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static com.jfirer.se2.JfireSE.*;
public class ListSerializer implements Serializer
{
private JfireSE jfireSE;
private int type;
private static final int ARRAY_LIST = 1;
private static final int LINKED_LIST = 2;
public ListSerializer(JfireSE jfireSE, Class<?> type)
{
this.jfireSE = jfireSE;
this.type = type == ArrayList.class ? ARRAY_LIST : LINKED_LIST;
}
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
List<?> list = (List<?>) instance;
int size = list.size();
byteArray.writePositiveVarInt(size);
for (Object each : list)
{
if (each == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
jfireSE.getOrCreateClassInfo(each.getClass()).write(byteArray, each);
}
}
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
int size = byteArray.readPositiveVarInt();
List<Object> list = type == ARRAY_LIST ? new ArrayList<>() : new LinkedList<>();
if (refTracking != null)
{
refTracking.addTracking(list);
}
for (int i = 0; i < size; i++)
{
byte flag = byteArray.get();
if (flag == JfireSE.NULL)
{
list.add(null);
}
else
{
if (flag >= NAME_ID_CONTENT_TRACK && flag <= ID_CONTENT_UN_TRACK)
{
list.add(jfireSE.readByUnderInstanceIdFlag(byteArray, flag));
}
else
{
throw new IllegalStateException("Unexpected value: " + flag);
}
}
}
return list;
}
}

View File

@ -0,0 +1,108 @@
package com.jfirer.se2.serializer.impl;
import com.jfirer.se2.ByteArray;
import com.jfirer.se2.JfireSE;
import com.jfirer.se2.classinfo.RefTracking;
import com.jfirer.se2.serializer.Serializer;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
public class MapSerializer implements Serializer
{
private JfireSE jfireSE;
private int type;
public static final int HASHMAP = 1;
public static final int TREEMAP = 2;
public static final int LINKEDHASHMAP = 3;
public static final int CONCURRENTHASHMAP = 4;
public MapSerializer(JfireSE jfireSE, Class<? extends Map> type)
{
this.jfireSE = jfireSE;
if (type.equals(Map.class))
{
this.type = HASHMAP;
}
else if (type.equals(TreeMap.class))
{
this.type = TREEMAP;
}
else if (type.equals(LinkedHashMap.class))
{
this.type = LINKEDHASHMAP;
}
else if (type.equals(ConcurrentHashMap.class))
{
this.type = CONCURRENTHASHMAP;
}
}
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Map<?, ?> map = (Map) instance;
int size = map.size();
byteArray.writePositiveVarInt(size);
for (Map.Entry<?, ?> entry : map.entrySet())
{
Object key = entry.getKey();
Object value = entry.getValue();
jfireSE.getOrCreateClassInfo(key.getClass()).write(byteArray, key);
if (value == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
jfireSE.getOrCreateClassInfo(value.getClass()).write(byteArray, value);
}
}
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
Map<Object, Object> map = switch (type)
{
case HASHMAP -> new HashMap<>();
case TREEMAP -> new TreeMap<>();
case LINKEDHASHMAP -> new LinkedHashMap<>();
case CONCURRENTHASHMAP -> new ConcurrentHashMap<>();
default -> throw new IllegalStateException("Unexpected value: " + type);
};
int size = byteArray.readPositiveVarInt();
for (int i = 0; i < size; i++)
{
Object key;
byte flag = byteArray.get();
if (flag >= JfireSE.NAME_ID_CONTENT_TRACK && flag <= JfireSE.ID_CONTENT_UN_TRACK)
{
key = jfireSE.readByUnderInstanceIdFlag(byteArray, flag);
}
else
{
throw new IllegalStateException("Unexpected value: " + flag);
}
Object value;
byte b = byteArray.get();
if (b == JfireSE.NULL)
{
value = null;
}
else if (b >= JfireSE.NAME_ID_CONTENT_TRACK && b <= JfireSE.ID_CONTENT_UN_TRACK)
{
value = jfireSE.readByUnderInstanceIdFlag(byteArray, b);
}
else
{
throw new IllegalStateException("Unexpected value: " + b);
}
map.put(key, value);
}
return map;
}
}

View File

@ -0,0 +1,94 @@
package com.jfirer.se2.serializer.impl;
import com.jfirer.se2.ByteArray;
import com.jfirer.se2.JfireSE;
import com.jfirer.se2.classinfo.RefTracking;
import com.jfirer.se2.serializer.Serializer;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
public class SetSerializer implements Serializer
{
private JfireSE jfireSE;
private int type;
private static final int HASH_SET = 1;
private static final int LINKED_HASH_SET = 2;
private static final int TREE_SET = 3;
private static final int CONCURRENT_SKIP_LIST_SET = 4;
public SetSerializer(Class<?> type, JfireSE jfireSE)
{
this.jfireSE = jfireSE;
if (type == HashSet.class)
{
this.type = HASH_SET;
}
else if (type == LinkedHashSet.class)
{
this.type = LINKED_HASH_SET;
}
else if (type == TreeSet.class)
{
this.type = TREE_SET;
}
else if (type == ConcurrentSkipListSet.class)
{
this.type = CONCURRENT_SKIP_LIST_SET;
}
else
{
throw new IllegalArgumentException();
}
}
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
Set<?> collection = (Set<?>) instance;
int size = collection.size();
byteArray.writePositiveVarInt(size);
for (Object each : collection)
{
if (each == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
jfireSE.getOrCreateClassInfo(each.getClass()).write(byteArray, each);
}
}
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
Set<Object> set = switch (type)
{
case HASH_SET -> new HashSet<>();
case LINKED_HASH_SET -> new LinkedHashSet<>();
case TREE_SET -> new TreeSet<>();
case CONCURRENT_SKIP_LIST_SET -> new ConcurrentSkipListSet<>();
default -> throw new IllegalStateException("Unexpected value: " + type);
};
int size = byteArray.readPositiveVarInt();
if (refTracking != null)
{
refTracking.addTracking(set);
}
for (int i = 0; i < size; i++)
{
byte b = byteArray.get();
if (b == JfireSE.NULL)
{
set.add(null);
}
else
{
set.add(jfireSE.readByUnderInstanceIdFlag(byteArray, b));
}
}
return set;
}
}

View File

@ -0,0 +1,20 @@
package com.jfirer.se2.serializer.impl;
import com.jfirer.se2.ByteArray;
import com.jfirer.se2.classinfo.RefTracking;
import com.jfirer.se2.serializer.Serializer;
public class SqlDateSerializer implements Serializer
{
@Override
public void writeBytes(ByteArray byteArray, Object instance)
{
byteArray.writeVarLong(((java.sql.Date) instance).getTime());
}
@Override
public Object read(ByteArray byteArray, RefTracking refTracking)
{
return new java.sql.Date(byteArray.readVarLong());
}
}

View File

@ -3,8 +3,8 @@ package org.example;
import com.jfirer.fse.ByteArray;
import com.jfirer.fse.Fse;
import com.jfirer.se2.JfireSE;
import io.fury.Fury;
import io.fury.config.Language;
import org.apache.fury.Fury;
import org.apache.fury.config.Language;
import org.example.sm.TestDataSm;
import org.example.sm2.TestDataSm2;
import org.openjdk.jmh.annotations.*;

View File

@ -2,7 +2,7 @@ package org.example;
import com.jfirer.se2.ByteArray;
import com.jfirer.se2.JfireSE;
import io.fury.Fury;
import org.apache.fury.Fury;
import org.example.sm.TestDataSm;
import org.example.sm2.TestDataSm2;
import org.junit.Assert;
@ -47,7 +47,7 @@ public class FunctionTest
@Test
public void test3()
{
Fury fury = Fury.builder().requireClassRegistration(false).build();
Fury fury = Fury.builder().requireClassRegistration(false).build();
TestData[] data = new TestData[2];
data[0] = new TestData();
fury.serialize(data);