KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > io > serializer > impl > StringSerializer


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.io.serializer.impl;
5
6 import com.tc.io.serializer.api.Serializer;
7 import com.tc.io.serializer.api.StringIndex;
8
9 import java.io.IOException JavaDoc;
10 import java.io.ObjectInput JavaDoc;
11 import java.io.ObjectOutput JavaDoc;
12
13 /**
14  * Strings
15  */

16 public final class StringSerializer implements Serializer {
17
18   private final StringIndex stringIndex;
19
20   public StringSerializer(StringIndex stringIndex) {
21     this.stringIndex = stringIndex;
22   }
23
24   public void serializeTo(Object JavaDoc o, ObjectOutput JavaDoc out) throws IOException JavaDoc {
25     out.writeLong(this.stringIndex.getOrCreateIndexFor((String JavaDoc) o));
26   }
27
28   public Object JavaDoc deserializeFrom(ObjectInput JavaDoc in) throws IOException JavaDoc {
29     return this.stringIndex.getStringFor(in.readLong());
30   }
31
32   public byte getSerializerID() {
33     return STRING_INDEX;
34   }
35 }
36
Popular Tags