1 8 9 package com.sleepycat.bind.serial; 10 11 import java.io.IOException ; 12 13 import com.sleepycat.bind.EntryBinding; 14 import com.sleepycat.je.DatabaseEntry; 15 import com.sleepycat.util.FastInputStream; 16 import com.sleepycat.util.FastOutputStream; 17 import com.sleepycat.util.RuntimeExceptionWrapper; 18 19 42 public class SerialBinding extends SerialBase implements EntryBinding { 43 44 private ClassCatalog classCatalog; 45 private Class baseClass; 46 47 57 public SerialBinding(ClassCatalog classCatalog, Class baseClass) { 58 59 if (classCatalog == null) { 60 throw new NullPointerException ("classCatalog must be non-null"); 61 } 62 this.classCatalog = classCatalog; 63 this.baseClass = baseClass; 64 } 65 66 71 public final Class getBaseClass() { 72 73 return baseClass; 74 } 75 76 93 public ClassLoader getClassLoader() { 94 95 return null; 96 } 97 98 108 public Object entryToObject(DatabaseEntry entry) { 109 110 int length = entry.getSize(); 111 byte[] hdr = SerialOutput.getStreamHeader(); 112 byte[] bufWithHeader = new byte[length + hdr.length]; 113 114 System.arraycopy(hdr, 0, bufWithHeader, 0, hdr.length); 115 System.arraycopy(entry.getData(), entry.getOffset(), 116 bufWithHeader, hdr.length, length); 117 118 try { 119 SerialInput jin = new SerialInput( 120 new FastInputStream(bufWithHeader, 0, bufWithHeader.length), 121 classCatalog, 122 getClassLoader()); 123 return jin.readObject(); 124 } catch (IOException e) { 125 throw new RuntimeExceptionWrapper(e); 126 } catch (ClassNotFoundException e) { 127 throw new RuntimeExceptionWrapper(e); 128 } 129 } 130 131 148 public void objectToEntry(Object object, DatabaseEntry entry) { 149 150 if (baseClass != null && !baseClass.isInstance(object)) { 151 throw new IllegalArgumentException ( 152 "Data object class (" + object.getClass() + 153 ") not an instance of binding's base class (" + 154 baseClass + ')'); 155 } 156 FastOutputStream fo = getSerialOutput(object); 157 try { 158 SerialOutput jos = new SerialOutput(fo, classCatalog); 159 jos.writeObject(object); 160 } catch (IOException e) { 161 throw new RuntimeExceptionWrapper(e); 162 } 163 164 byte[] hdr = SerialOutput.getStreamHeader(); 165 entry.setData(fo.getBufferBytes(), hdr.length, 166 fo.getBufferLength() - hdr.length); 167 } 168 } 169 | Popular Tags |