1 5 package com.tc.io.serializer; 6 7 import com.tc.io.serializer.api.Serializer; 8 import com.tc.io.serializer.api.SerializerPolicy; 9 import com.tc.io.serializer.api.StringIndex; 10 import com.tc.io.serializer.impl.BooleanSerializer; 11 import com.tc.io.serializer.impl.ByteSerializer; 12 import com.tc.io.serializer.impl.CharacterSerializer; 13 import com.tc.io.serializer.impl.DoubleSerializer; 14 import com.tc.io.serializer.impl.FloatSerializer; 15 import com.tc.io.serializer.impl.IntegerSerializer; 16 import com.tc.io.serializer.impl.LongSerializer; 17 import com.tc.io.serializer.impl.ObjectIDSerializer; 18 import com.tc.io.serializer.impl.ObjectSerializer; 19 import com.tc.io.serializer.impl.ShortSerializer; 20 import com.tc.io.serializer.impl.StringSerializer; 21 import com.tc.io.serializer.impl.StringUTFSerializer; 22 import com.tc.object.ObjectID; 23 24 import gnu.trove.TIntObjectHashMap; 25 26 import java.io.DataOutput ; 27 import java.io.IOException ; 28 import java.io.ObjectInput ; 29 import java.io.ObjectOutput ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 public final class DSOSerializerPolicy implements SerializerPolicy { 34 35 private final Map class2SerializerDescriptor; 36 private final TIntObjectHashMap id2Serializer; 37 private final DSOSerializerPolicy.SDesc objectSerializer; 38 39 public DSOSerializerPolicy(StringIndex stringIndex) { 40 this(stringIndex, false); 41 } 42 43 public DSOSerializerPolicy() { 44 this(null, true); 45 } 46 47 private DSOSerializerPolicy(StringIndex stringIndex, boolean useUTFStrings) { 48 class2SerializerDescriptor = new HashMap(); 49 id2Serializer = new TIntObjectHashMap(); 50 51 addSerializerMapping(Object .class, new ObjectSerializer()); 52 addSerializerMapping(ObjectID.class, new ObjectIDSerializer()); 53 addSerializerMapping(Boolean .class, new BooleanSerializer()); 54 addSerializerMapping(Byte .class, new ByteSerializer()); 55 addSerializerMapping(Character .class, new CharacterSerializer()); 56 if (useUTFStrings) { 57 addSerializerMapping(String .class, new StringUTFSerializer()); 58 } else { 59 addSerializerMapping(String .class, new StringSerializer(stringIndex)); 60 } 61 addSerializerMapping(Double .class, new DoubleSerializer()); 62 addSerializerMapping(Float .class, new FloatSerializer()); 63 addSerializerMapping(Integer .class, new IntegerSerializer()); 64 addSerializerMapping(Long .class, new LongSerializer()); 65 addSerializerMapping(Short .class, new ShortSerializer()); 66 67 objectSerializer = (SDesc) class2SerializerDescriptor.get(Object .class); 68 } 69 70 private void addSerializerMapping(Class clazz, Serializer serializer) { 71 SDesc desc = new SDesc(clazz, serializer); 72 if (id2Serializer.containsKey(desc.getID())) throw new AssertionError ("Duplicate desc ids: " + desc.getID()); 73 id2Serializer.put(desc.getID(), desc.serializer); 74 class2SerializerDescriptor.put(desc.clazz, desc); 75 } 76 77 public Serializer getSerializerFor(Object o, ObjectOutput out) throws IOException { 78 DSOSerializerPolicy.SDesc desc = null; 79 if (o != null) { 80 desc = (DSOSerializerPolicy.SDesc) class2SerializerDescriptor.get(o.getClass()); 81 } 82 desc = (desc == null) ? objectSerializer : desc; 83 return extractSerializer(desc, out); 84 } 85 86 private Serializer extractSerializer(DSOSerializerPolicy.SDesc desc, DataOutput out) throws IOException { 87 out.writeByte(desc.getID()); 88 return desc.serializer; 89 } 90 91 public Serializer getSerializerFor(ObjectInput in) throws IOException { 92 Serializer rv = (Serializer) id2Serializer.get(in.readByte()); 93 return rv == null ? objectSerializer.serializer : rv; 94 } 95 96 static final class SDesc { 97 Class clazz; 98 Serializer serializer; 99 100 SDesc(Class clazz, Serializer serializer) { 101 this.clazz = clazz; 102 this.serializer = serializer; 103 } 104 105 int getID() { 106 return serializer.getSerializerID(); 107 } 108 } 109 } | Popular Tags |