1 24 25 package org.objectweb.dream.message.codec; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.OutputStream ; 32 33 import org.objectweb.dream.message.Message; 34 35 41 public abstract class AbstractMessageCodecObjectStream 42 extends 43 AbstractMessageCodecDataStream 44 implements 45 MessageCodecObjectStreamAttributeController 46 { 47 48 boolean reuseObjectStream; 52 53 57 60 public void encode(CodecInputOutput cio, Message message) throws IOException 61 { 62 Object output = cio.getOutput(); 63 ObjectOutputStream oos; 64 if (output instanceof ObjectOutputStream ) 65 { 66 oos = (ObjectOutputStream ) output; 67 } 68 else 69 { 70 if (!(output instanceof OutputStream )) 71 { 72 throw new IOException ("Unknown output : " + output); 73 } 74 oos = new ObjectOutputStream ((OutputStream ) output); 75 if (reuseObjectStream) 76 { 77 cio.setOutput(oos); 78 } 79 } 80 doEncode(message, oos); 81 oos.flush(); 82 } 83 84 87 public Message decode(CodecInputOutput cio) throws IOException 88 { 89 Object input = cio.getInput(); 90 ObjectInputStream ois; 91 if (input instanceof ObjectInputStream ) 92 { 93 ois = (ObjectInputStream ) input; 94 } 95 else 96 { 97 if (!(input instanceof InputStream )) 98 { 99 throw new IOException ("Unknown input : " + input); 100 } 101 ois = new ObjectInputStream ((InputStream ) input); 102 if (reuseObjectStream) 103 { 104 cio.setInput(ois); 105 } 106 } 107 return doDecode(ois); 108 } 109 110 114 117 public boolean getReuseObjectStream() 118 { 119 return reuseObjectStream; 120 } 121 122 125 public void setReuseObjectStream(boolean reuseObjectStream) 126 { 127 this.reuseObjectStream = reuseObjectStream; 128 } 129 } | Popular Tags |