1 21 22 package org.apache.derby.iapi.services.io; 23 24 import java.io.DataInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.ObjectInputStream ; 28 import java.io.StreamCorruptedException ; 29 import org.apache.derby.iapi.reference.SQLState; 30 import org.apache.derby.iapi.services.monitor.Monitor; 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 import org.apache.derby.iapi.error.StandardException; 33 import org.apache.derby.iapi.services.loader.ClassFactory; 34 import org.apache.derby.iapi.services.loader.ClassFactoryContext; 35 import org.apache.derby.iapi.types.Resetable; 36 37 import org.apache.derby.iapi.services.context.ContextService; 38 45 public final class FormatIdInputStream extends DataInputStream 46 implements ErrorObjectInput, Resetable 47 { 48 protected ClassFactory cf; 49 private ErrorInfo errorInfo; 50 private Exception myNestedException; 51 52 53 58 public FormatIdInputStream(InputStream in) 59 { 60 super(in); 61 } 62 63 71 72 public Object readObject() throws IOException , ClassNotFoundException 73 { 74 setErrorInfo(null); 75 76 int fmtId = FormatIdUtil.readFormatIdInteger(this); 77 78 if (fmtId == StoredFormatIds.NULL_FORMAT_ID) 79 { 80 return null; 81 } 82 83 if (fmtId == StoredFormatIds.STRING_FORMAT_ID) 84 { 85 return readUTF(); 86 } 87 88 try 89 { 90 91 if (fmtId == StoredFormatIds.SERIALIZABLE_FORMAT_ID) 92 { 93 ObjectInputStream ois = getObjectStream(); 94 try { 95 Object result = ois.readObject(); 96 return result; 97 } catch (IOException ioe) { 98 setErrorInfo((ErrorInfo) ois); 99 throw ioe; 100 } catch (ClassNotFoundException cnfe) { 101 setErrorInfo((ErrorInfo) ois); 102 throw cnfe; 103 } catch (LinkageError le) { 104 setErrorInfo((ErrorInfo) ois); 105 throw le; 106 } catch (ClassCastException cce) { 107 setErrorInfo((ErrorInfo) ois); 108 throw cce; 109 } 110 } 111 112 try { 113 114 Formatable f = (Formatable)Monitor.newInstanceFromIdentifier(fmtId); 115 if (f instanceof Storable) 116 { 117 boolean isNull = this.readBoolean(); 118 if (isNull == true) 119 { 120 Storable s = (Storable)f; 121 s.restoreToNull(); 122 return s; 123 } 124 } 125 126 f.readExternal(this); 127 return f; 128 } catch (StandardException se) { 129 throw new ClassNotFoundException (se.toString()); 130 } 131 132 133 } 134 catch (ClassCastException cce) 135 { 136 throw new StreamCorruptedException (cce.toString()); 141 } 142 } 143 144 150 public void setInput(InputStream in) 151 { 152 this.in = in; 153 } 154 155 public InputStream getInputStream() 156 { 157 return in; 158 } 159 160 public String getErrorInfo() 161 { 162 if (errorInfo == null) 163 return ""; 164 165 return errorInfo.getErrorInfo(); 166 } 167 168 public Exception getNestedException() 169 { 170 if (myNestedException != null) 171 return null; 172 173 if (errorInfo == null) 174 return null; 175 176 return errorInfo.getNestedException(); 177 } 178 179 private void setErrorInfo(ErrorInfo ei) 180 { 181 errorInfo = ei; 182 } 183 184 185 ClassFactory getClassFactory() { 186 if (cf == null) { 187 188 ClassFactoryContext cfc = 189 (ClassFactoryContext) ContextService.getContextOrNull 190 (ClassFactoryContext.CONTEXT_ID); 191 192 if (cfc != null) 193 cf = cfc.getClassFactory(); 194 } 195 return cf; 196 } 197 198 201 202 private ObjectInputStream getObjectStream() throws IOException { 203 204 return getClassFactory() == null ? 205 new ObjectInputStream (this) : 206 new ApplicationObjectInputStream(this, cf); 207 } 208 209 210 211 212 213 public void resetStream() 214 throws IOException , StandardException 215 { 216 if (SanityManager.DEBUG) 217 SanityManager.ASSERT(in instanceof Resetable); 218 ((Resetable) in).resetStream(); 219 } 220 221 222 public void initStream() 223 throws StandardException 224 { 225 if (SanityManager.DEBUG) 226 SanityManager.ASSERT(in instanceof Resetable); 227 ((Resetable) in).initStream(); 228 } 229 230 231 public void closeStream() 232 { 233 if (SanityManager.DEBUG) 234 SanityManager.ASSERT(in instanceof Resetable); 235 ((Resetable) in).closeStream(); 236 } 237 238 } 239 240 | Popular Tags |