00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.bind.serial;
00011
00012 import java.io.ByteArrayOutputStream;
00013 import java.io.IOException;
00014 import java.io.ObjectOutputStream;
00015 import java.io.ObjectStreamClass;
00016 import java.io.ObjectStreamConstants;
00017 import java.io.OutputStream;
00018
00019 import com.sleepycat.db.DatabaseException;
00020 import com.sleepycat.util.RuntimeExceptionWrapper;
00021
00041 public class SerialOutput extends ObjectOutputStream {
00042
00043
00044
00045
00046
00047
00048 private final static byte[] STREAM_HEADER;
00049 static {
00050 ByteArrayOutputStream baos = new ByteArrayOutputStream();
00051 try {
00052 new SerialOutput(baos, null);
00053 } catch (IOException e) {
00054 throw new RuntimeExceptionWrapper(e);
00055 }
00056 STREAM_HEADER = baos.toByteArray();
00057 }
00058
00059 private ClassCatalog classCatalog;
00060
00070 public SerialOutput(OutputStream out, ClassCatalog classCatalog)
00071 throws IOException {
00072
00073 super(out);
00074 this.classCatalog = classCatalog;
00075
00076
00077
00078 useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
00079 }
00080
00081
00082 protected void writeClassDescriptor(ObjectStreamClass classdesc)
00083 throws IOException {
00084
00085 try {
00086 byte[] id = classCatalog.getClassID(classdesc);
00087 writeByte(id.length);
00088 write(id);
00089 } catch (DatabaseException e) {
00090
00091
00092
00093
00094
00095 throw new RuntimeExceptionWrapper(e);
00096 } catch (ClassNotFoundException e) {
00097 throw new RuntimeExceptionWrapper(e);
00098 }
00099 }
00100
00111 public static byte[] getStreamHeader() {
00112
00113 return STREAM_HEADER;
00114 }
00115 }