Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SerialOutput.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: SerialOutput.java,v 12.2 2005/08/01 20:25:07 mark Exp $
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      * Serialization version constants. Instead of hardcoding these we get them
00045      * by creating a SerialOutput, which itself guarantees that we'll always
00046      * use a PROTOCOL_VERSION_2 header.
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         /* guarantee that we'll always use the same serialization format */
00077 
00078         useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
00079     }
00080 
00081     // javadoc is inherited
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              * Do not throw IOException from here since ObjectOutputStream
00092              * will write the exception to the stream, which causes another
00093              * call here, etc.
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 }

Generated on Sun Dec 25 12:14:30 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2