1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.FormatIdInputStream |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import java.io.DataInputStream; |
24 | import java.io.IOException; |
25 | import java.io.InputStream; |
26 | import java.io.ObjectInputStream; |
27 | import java.io.StreamCorruptedException; |
28 | import org.apache.derby.iapi.reference.SQLState; |
29 | import org.apache.derby.iapi.services.monitor.Monitor; |
30 | import org.apache.derby.iapi.services.sanity.SanityManager; |
31 | import org.apache.derby.iapi.error.StandardException; |
32 | import org.apache.derby.iapi.services.loader.ClassFactory; |
33 | import org.apache.derby.iapi.services.loader.ClassFactoryContext; |
34 | import org.apache.derby.iapi.types.Resetable; |
35 | |
36 | import org.apache.derby.iapi.services.context.ContextService; |
37 | /** |
38 | A stream for reading objects with format id tags which was |
39 | produced by a FormatIdOutputStream. |
40 | |
41 | <P>Please see the documentation for FormatIdOutputStream for |
42 | information about the streams format and capabilites. |
43 | */ |
44 | public final class FormatIdInputStream extends DataInputStream |
45 | implements ErrorObjectInput, Resetable |
46 | { |
47 | protected ClassFactory cf; |
48 | private ErrorInfo errorInfo; |
49 | private Exception myNestedException; |
50 | |
51 | |
52 | /** |
53 | Constructor for a FormatIdInputStream |
54 | |
55 | @param in bytes come from here. |
56 | */ |
57 | public FormatIdInputStream(InputStream in) |
58 | { |
59 | super(in); |
60 | } |
61 | |
62 | /** |
63 | Read an object from this stream. |
64 | |
65 | @return The read object. |
66 | @exception java.io.IOException An IO or serialization error occured. |
67 | @exception java.lang.ClassNotFoundException A class for an object in |
68 | the stream could not be found. |
69 | */ |
70 | |
71 | public Object readObject() throws IOException, ClassNotFoundException |
72 | { |
73 | setErrorInfo(null); |
74 | |
75 | int fmtId = FormatIdUtil.readFormatIdInteger(this); |
76 | |
77 | if (fmtId == StoredFormatIds.NULL_FORMAT_ID) |
78 | { |
79 | return null; |
80 | } |
81 | |
82 | if (fmtId == StoredFormatIds.STRING_FORMAT_ID) |
83 | { |
84 | return readUTF(); |
85 | } |
86 | |
87 | try |
88 | { |
89 | |
90 | if (fmtId == StoredFormatIds.SERIALIZABLE_FORMAT_ID) |
91 | { |
92 | ObjectInputStream ois = getObjectStream(); |
93 | try { |
94 | Object result = ois.readObject(); |
95 | return result; |
96 | } catch (IOException ioe) { |
97 | setErrorInfo((ErrorInfo) ois); |
98 | throw ioe; |
99 | } catch (ClassNotFoundException cnfe) { |
100 | setErrorInfo((ErrorInfo) ois); |
101 | throw cnfe; |
102 | } catch (LinkageError le) { |
103 | setErrorInfo((ErrorInfo) ois); |
104 | throw le; |
105 | } catch (ClassCastException cce) { |
106 | setErrorInfo((ErrorInfo) ois); |
107 | throw cce; |
108 | } |
109 | } |
110 | |
111 | try { |
112 | |
113 | Formatable f = (Formatable)Monitor.newInstanceFromIdentifier(fmtId); |
114 | if (f instanceof Storable) |
115 | { |
116 | boolean isNull = this.readBoolean(); |
117 | if (isNull == true) |
118 | { |
119 | Storable s = (Storable)f; |
120 | s.restoreToNull(); |
121 | return s; |
122 | } |
123 | } |
124 | |
125 | f.readExternal(this); |
126 | return f; |
127 | } catch (StandardException se) { |
128 | throw new ClassNotFoundException(se.toString()); |
129 | } |
130 | |
131 | |
132 | } |
133 | catch (ClassCastException cce) |
134 | { |
135 | // We catch this here as it is usuall a user error. |
136 | // they have readExternal (or SQLData) that doesn't match |
137 | // the writeExternal. and thus the object read is of |
138 | // the incorrect type, e.g. Integer i = (Integer) in.readObject(); |
139 | throw new StreamCorruptedException(cce.toString()); |
140 | } |
141 | } |
142 | |
143 | /** |
144 | Set the InputStream for this FormatIdInputStream to the stream |
145 | provided. |
146 | |
147 | @param in The new input stream. |
148 | */ |
149 | public void setInput(InputStream in) |
150 | { |
151 | this.in = in; |
152 | } |
153 | |
154 | public InputStream getInputStream() |
155 | { |
156 | return in; |
157 | } |
158 | |
159 | public String getErrorInfo() |
160 | { |
161 | if (errorInfo == null) |
162 | return ""; |
163 | |
164 | return errorInfo.getErrorInfo(); |
165 | } |
166 | |
167 | public Exception getNestedException() |
168 | { |
169 | if (myNestedException != null) |
170 | return null; |
171 | |
172 | if (errorInfo == null) |
173 | return null; |
174 | |
175 | return errorInfo.getNestedException(); |
176 | } |
177 | |
178 | private void setErrorInfo(ErrorInfo ei) |
179 | { |
180 | errorInfo = ei; |
181 | } |
182 | |
183 | |
184 | ClassFactory getClassFactory() { |
185 | if (cf == null) { |
186 | |
187 | ClassFactoryContext cfc = |
188 | (ClassFactoryContext) ContextService.getContextOrNull |
189 | (ClassFactoryContext.CONTEXT_ID); |
190 | |
191 | if (cfc != null) |
192 | cf = cfc.getClassFactory(); |
193 | } |
194 | return cf; |
195 | } |
196 | |
197 | /* |
198 | ** Class private methods |
199 | */ |
200 | |
201 | private ObjectInputStream getObjectStream() throws IOException { |
202 | |
203 | return getClassFactory() == null ? |
204 | new ObjectInputStream(this) : |
205 | new ApplicationObjectInputStream(this, cf); |
206 | } |
207 | |
208 | |
209 | |
210 | /*** Resetable interface ***/ |
211 | |
212 | public void resetStream() |
213 | throws IOException, StandardException |
214 | { |
215 | if (SanityManager.DEBUG) |
216 | SanityManager.ASSERT(in instanceof Resetable); |
217 | ((Resetable) in).resetStream(); |
218 | } |
219 | |
220 | |
221 | public void initStream() |
222 | throws StandardException |
223 | { |
224 | if (SanityManager.DEBUG) |
225 | SanityManager.ASSERT(in instanceof Resetable); |
226 | ((Resetable) in).initStream(); |
227 | } |
228 | |
229 | |
230 | public void closeStream() |
231 | { |
232 | if (SanityManager.DEBUG) |
233 | SanityManager.ASSERT(in instanceof Resetable); |
234 | ((Resetable) in).closeStream(); |
235 | } |
236 | |
237 | } |
238 | |