1 8 9 package com.sleepycat.bind.serial; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.ObjectOutputStream ; 14 import java.io.ObjectStreamClass ; 15 import java.io.ObjectStreamConstants ; 16 import java.io.OutputStream ; 17 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.util.RuntimeExceptionWrapper; 20 21 40 public class SerialOutput extends ObjectOutputStream { 41 42 47 private final static byte[] STREAM_HEADER; 48 static { 49 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 50 try { 51 new SerialOutput(baos, null); 52 } catch (IOException e) { 53 throw new RuntimeExceptionWrapper(e); 54 } 55 STREAM_HEADER = baos.toByteArray(); 56 } 57 58 private ClassCatalog classCatalog; 59 60 69 public SerialOutput(OutputStream out, ClassCatalog classCatalog) 70 throws IOException { 71 72 super(out); 73 this.classCatalog = classCatalog; 74 75 76 77 useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2); 78 } 79 80 protected void writeClassDescriptor(ObjectStreamClass classdesc) 82 throws IOException { 83 84 try { 85 byte[] id = classCatalog.getClassID(classdesc); 86 writeByte(id.length); 87 write(id); 88 } catch (DatabaseException e) { 89 94 throw new RuntimeExceptionWrapper(e); 95 } catch (ClassNotFoundException e) { 96 throw new RuntimeExceptionWrapper(e); 97 } 98 } 99 100 110 public static byte[] getStreamHeader() { 111 112 return STREAM_HEADER; 113 } 114 } 115 | Popular Tags |