1 4 package com.tc.object.dna.impl; 5 6 import com.tc.io.TCByteBufferInputStream; 7 import com.tc.io.TCByteBufferOutput; 8 import com.tc.io.TCByteBufferOutputStream; 9 import com.tc.io.TCDataOutput; 10 import com.tc.io.TCSerializable; 11 import com.tc.util.Assert; 12 13 import gnu.trove.TIntObjectHashMap; 14 import gnu.trove.TLongObjectHashMap; 15 import gnu.trove.TObjectIntHashMap; 16 import gnu.trove.TObjectIntProcedure; 17 18 import java.io.IOException ; 19 20 public class ObjectStringSerializer implements TCSerializable { 21 22 private final TObjectIntHashMap stringToID = new TObjectIntHashMap(); 23 private final TIntObjectHashMap idToString = new TIntObjectHashMap(); 24 private final TLongObjectHashMap fields = new TLongObjectHashMap(); 25 26 private static class SerializeProcedure implements TObjectIntProcedure { 27 private final TCDataOutput out; 28 29 public SerializeProcedure(TCDataOutput out) { 30 this.out = out; 31 } 32 33 public boolean execute(Object key, int value) { 34 out.writeString((String ) key); 35 out.writeInt(value); 36 return true; 37 } 38 } 39 40 public synchronized void serializeTo(TCByteBufferOutput serialOutput) { 41 serialOutput.writeInt(stringToID.size()); 42 stringToID.forEachEntry(new SerializeProcedure(serialOutput)); 43 } 44 45 public synchronized Object deserializeFrom(TCByteBufferInputStream serialInput) throws IOException { 46 int size = serialInput.readInt(); 47 for (int i = 0; i < size; i++) { 48 addStringAndID(serialInput.readString(), serialInput.readInt()); 49 } 50 return this; 51 } 52 53 public synchronized void writeString(TCByteBufferOutputStream out, String string) { 54 int sid = -1; 55 if (stringToID.containsKey(string)) { 56 sid = idForString(string); 57 } else { 58 sid = createID(string); 59 } 60 out.writeInt(sid); 61 } 62 63 public synchronized void writeFieldName(TCByteBufferOutputStream out, String fieldName) { 64 int lastDot = fieldName.lastIndexOf('.'); 65 String cn = fieldName.substring(0, lastDot); 66 String fn = fieldName.substring(lastDot + 1, fieldName.length()); 67 68 writeString(out, cn); 69 writeString(out, fn); 70 } 71 72 public synchronized String readString(TCByteBufferInputStream in) throws IOException { 73 int id = in.readInt(); 74 75 String string = stringForID(id); 76 if (string == null) { throw new AssertionError ("cid:" + id + " map:" + stringToID); } 77 78 return string; 79 } 80 81 public synchronized String readFieldName(TCByteBufferInputStream in) throws IOException { 82 final int classId; 83 classId = in.readInt(); 84 85 final int fieldId; 86 fieldId = in.readInt(); 87 88 long key = ((long) classId << 32) + fieldId; 89 String rv = (String ) fields.get(key); 90 if (rv == null) { 91 String cn = stringForID(classId); 92 Assert.eval(cn != null); 93 String fn = stringForID(fieldId); 94 Assert.eval(fn != null); 95 StringBuffer buf = new StringBuffer (cn.length() + fn.length() + 1); buf.append(cn).append('.').append(fn); 97 rv = buf.toString().intern(); 98 fields.put(key, rv); 99 } 100 101 return rv; 102 } 103 104 private void addStringAndID(String name, int id) { 105 name = name.intern(); 106 stringToID.put(name, id); 107 idToString.put(id, name); 108 } 109 110 private String stringForID(int id) { 111 return (String ) idToString.get(id); 112 } 113 114 private int idForString(String string) { 115 return stringToID.get(string); 116 } 117 118 private int createID(String string) { 119 Assert.assertNotNull(string); 120 int newID = stringToID.size() + 1; 121 stringToID.put(string, newID); 122 idToString.put(newID, string); 123 return newID; 124 } 125 } 126 | Popular Tags |