KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > FormatIdInputStream


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.FormatIdInputStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import java.io.DataInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.StreamCorruptedException JavaDoc;
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 /**
39   A stream for reading objects with format id tags which was
40   produced by a FormatIdOutputStream.
41
42   <P>Please see the documentation for FormatIdOutputStream for
43   information about the streams format and capabilites.
44   */

45 public final class FormatIdInputStream extends DataInputStream JavaDoc
46      implements ErrorObjectInput, Resetable
47 {
48     protected ClassFactory cf;
49     private ErrorInfo errorInfo;
50     private Exception JavaDoc myNestedException;
51
52
53     /**
54       Constructor for a FormatIdInputStream
55
56       @param in bytes come from here.
57       */

58     public FormatIdInputStream(InputStream JavaDoc in)
59     {
60         super(in);
61     }
62
63     /**
64       Read an object from this stream.
65
66       @return The read object.
67       @exception java.io.IOException An IO or serialization error occured.
68       @exception java.lang.ClassNotFoundException A class for an object in
69       the stream could not be found.
70       */

71
72     public Object JavaDoc readObject() throws IOException JavaDoc, ClassNotFoundException JavaDoc
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 JavaDoc ois = getObjectStream();
94                 try {
95                     Object JavaDoc result = ois.readObject();
96                     return result;
97                 } catch (IOException JavaDoc ioe) {
98                     setErrorInfo((ErrorInfo) ois);
99                     throw ioe;
100                 } catch (ClassNotFoundException JavaDoc cnfe) {
101                     setErrorInfo((ErrorInfo) ois);
102                     throw cnfe;
103                 } catch (LinkageError JavaDoc le) {
104                     setErrorInfo((ErrorInfo) ois);
105                     throw le;
106                 } catch (ClassCastException JavaDoc 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 JavaDoc(se.toString());
130             }
131
132
133         }
134         catch (ClassCastException JavaDoc cce)
135         {
136             // We catch this here as it is usuall a user error.
137
// they have readExternal (or SQLData) that doesn't match
138
// the writeExternal. and thus the object read is of
139
// the incorrect type, e.g. Integer i = (Integer) in.readObject();
140
throw new StreamCorruptedException JavaDoc(cce.toString());
141         }
142     }
143
144     /**
145       Set the InputStream for this FormatIdInputStream to the stream
146       provided.
147
148       @param in The new input stream.
149       */

150     public void setInput(InputStream JavaDoc in)
151     {
152         this.in = in;
153     }
154
155     public InputStream JavaDoc getInputStream()
156     {
157         return in;
158     }
159
160     public String JavaDoc getErrorInfo()
161     {
162         if (errorInfo == null)
163             return "";
164
165         return errorInfo.getErrorInfo();
166     }
167
168     public Exception JavaDoc 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     /*
199     ** Class private methods
200     */

201
202     private ObjectInputStream JavaDoc getObjectStream() throws IOException JavaDoc {
203
204         return getClassFactory() == null ?
205             new ObjectInputStream JavaDoc(this) :
206             new ApplicationObjectInputStream(this, cf);
207     }
208
209
210
211     /*** Resetable interface ***/
212
213     public void resetStream()
214         throws IOException JavaDoc, 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