1 21 22 package org.apache.derby.iapi.services.io; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.services.info.JVMInfo; 27 28 import java.io.DataOutputStream ; 29 import java.io.IOException ; 30 import java.io.ObjectOutput ; 31 import java.io.ObjectOutputStream ; 32 import java.io.OutputStream ; 33 import java.io.Serializable ; 34 35 36 72 public class FormatIdOutputStream 73 extends DataOutputStream implements ObjectOutput , ErrorInfo 74 { 75 76 81 public FormatIdOutputStream(OutputStream out) 82 { 83 super(out); 84 } 85 86 93 public void writeObject(Object ref) throws IOException 94 { 95 if (ref == null) 96 { 97 FormatIdUtil.writeFormatIdInteger(this, StoredFormatIds.NULL_FORMAT_ID); 98 return; 99 } 100 101 if (ref instanceof String ) 102 { 103 111 String str = (String ) ref; 112 113 if (str.length() <= 20000) 114 { 115 FormatIdUtil.writeFormatIdInteger( 116 this, StoredFormatIds.STRING_FORMAT_ID); 117 118 this.writeUTF((String )ref); 119 return; 120 } 121 } 122 123 OutputStream oldOut = null; 126 if (SanityManager.DEBUG) { 127 128 if (ref instanceof Formatable) { 129 130 oldOut = this.out; 131 132 this.out = new DebugByteTeeOutputStream(oldOut); 133 } 134 } 135 136 if (ref instanceof Storable) 137 { 138 Storable s = (Storable)ref; 139 140 int fmtId = s.getTypeFormatId(); 141 142 if (fmtId != StoredFormatIds.SERIALIZABLE_FORMAT_ID) { 143 FormatIdUtil.writeFormatIdInteger(this, fmtId); 144 boolean isNull = s.isNull(); 145 writeBoolean(isNull); 146 if (!isNull) 147 { 148 s.writeExternal(this); 149 } 150 if (SanityManager.DEBUG) { 151 ((DebugByteTeeOutputStream) this.out).checkObject(s); 152 this.out = oldOut; 153 } 154 return; 155 } 156 } 157 else if (ref instanceof Formatable) 158 { 159 Formatable f = 160 (Formatable) ref; 161 int fmtId = f.getTypeFormatId(); 162 163 if (fmtId != StoredFormatIds.SERIALIZABLE_FORMAT_ID) { 164 FormatIdUtil.writeFormatIdInteger(this,fmtId); 165 f.writeExternal(this); 166 167 if (SanityManager.DEBUG) { 168 ((DebugByteTeeOutputStream) this.out).checkObject(f); 169 this.out = oldOut; 170 } 171 return; 172 } 173 } 174 175 182 { 183 184 191 if (SanityManager.DEBUG) 192 { 193 if (SanityManager.DEBUG_ON("SerializedTrace")) 194 { 195 String name = ref.getClass().getName(); 196 if ( 197 !name.startsWith("java.lang") && 198 !name.startsWith("java.math")) 199 { 200 SanityManager.DEBUG("SerializedTrace", 201 "...writing serialized class: "+name); 202 System.out.println( 203 "...writing serialized class: "+name); 204 } 205 } 206 } 207 208 FormatIdUtil.writeFormatIdInteger(this, StoredFormatIds.SERIALIZABLE_FORMAT_ID); 209 ObjectOutputStream oos = new ObjectOutputStream (this); 210 oos.writeObject(ref); 211 oos.flush(); 212 213 if (SanityManager.DEBUG && ref instanceof Formatable) { 214 ((DebugByteTeeOutputStream) this.out).checkObject((Formatable) ref); 215 this.out = oldOut; 216 } 217 } 218 } 219 220 227 public void setOutput(OutputStream out) 228 { 229 this.out = out; 230 this.written = 0; 231 } 232 233 234 235 236 public String getErrorInfo() 237 { 238 return null; 239 } 240 241 public Exception getNestedException() 242 { 243 return null; 244 } 245 246 } 247 | Popular Tags |