KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > EXTDTAInputStream


1 /*
2  
3  Derby - Class org.apache.derby.impl.drda.DRDAStatement
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 package org.apache.derby.impl.drda;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Blob JavaDoc;
29 import java.sql.Clob JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 import java.io.UnsupportedEncodingException JavaDoc;
33
34 import org.apache.derby.iapi.reference.DRDAConstants;
35 import org.apache.derby.iapi.services.sanity.SanityManager;
36 import org.apache.derby.impl.jdbc.Util;
37
38 /**
39  * @author marsden
40  *
41  * EXTDTAObjectHolder provides Externalized Large Object representation that
42  * does not hold locks until the end of the transaction (DERBY-255)
43  *
44  * It serves as a holder for lob data and is only valid as long as the original
45  * result set from which it came is on the same row.
46  *
47  *
48  */

49 class EXTDTAInputStream extends InputStream JavaDoc {
50
51     private InputStream JavaDoc binaryInputStream = null;
52
53     private boolean isEmptyStream;
54     
55     private ResultSet JavaDoc dataResultSet = null;
56     private Blob JavaDoc blob = null;
57     private Clob JavaDoc clob = null;
58     
59     private EXTDTAInputStream(ResultSet JavaDoc rs,
60                   int columnNumber,
61                   int ndrdaType)
62         throws SQLException JavaDoc, IOException JavaDoc
63     {
64     
65         this.dataResultSet = rs;
66         this.isEmptyStream = ! initInputStream(rs,
67                            columnNumber,
68                            ndrdaType);
69         
70     }
71
72     
73     
74     /**
75      * Retrieve stream from the ResultSet and column specified. Create an
76      * input stream for the large object being retrieved. Do not hold
77      * locks until end of transaction. DERBY-255.
78      *
79      *
80      * See DDMWriter.writeScalarStream
81      *
82      * @param rs
83      * result set from which to retrieve the lob
84      * @param column
85      * column number
86      * @param drdaType
87      * FD:OCA type of object one of
88      * DRDAConstants.DRDA_TYPE_NLOBBYTES
89      * DRDAConstants.DRDA_TYPE_LOBBYTES
90      * DRDAConstants.DRDA_TYPE_NLOBCMIXED
91      * DRDAConstants.DRDA_TYPE_LOBCMIXED
92      *
93      * @return null if the value is null or a new EXTDTAInputStream corresponding to
94      * rs.getBinaryStream(column) value and associated length
95      *
96      * @throws SQLException
97      */

98     public static EXTDTAInputStream getEXTDTAStream(ResultSet JavaDoc rs, int column, int drdaType)
99             throws SQLException JavaDoc {
100         try{
101         int ndrdaType = drdaType | 1; //nullable drdaType
102

103         return new EXTDTAInputStream(rs,
104                          column,
105                          ndrdaType);
106         
107         }catch(IOException JavaDoc e){
108         throw new SQLException JavaDoc(e.getMessage());
109         }
110         
111     }
112
113     
114     /**
115      * Get the length of the InputStream
116      * This method is currently not used because there seems to be no way to
117      * reset the she stream.
118      *
119      * @param binaryInputStream
120      * an InputStream whose length needs to be calclulated
121      * @return length of stream
122      */

123     private static long getInputStreamLength(InputStream JavaDoc binaryInputStream)
124             throws SQLException JavaDoc {
125         long length = 0;
126         if (binaryInputStream == null)
127             return length;
128         
129         try {
130             for (;;) {
131                 int avail = binaryInputStream.available();
132                 binaryInputStream.skip(avail);
133                 if (avail == 0)
134                     break;
135                 length += avail;
136                 
137             }
138             //binaryInputStream.close();
139
} catch (IOException JavaDoc ioe) {
140             throw Util.javaException(ioe);
141         }
142
143         return length;
144
145     }
146     
147     
148     /**
149      *
150      *
151      * @see java.io.InputStream#read()
152      */

153     public int read() throws IOException JavaDoc {
154         return binaryInputStream.read();
155     }
156
157     /**
158      *
159      *
160      * @see java.io.InputStream#available()
161      */

162     public int available() throws IOException JavaDoc {
163         return binaryInputStream.available();
164     }
165
166     /**
167      *
168      *
169      * @see java.io.InputStream#close()
170      */

171     public void close() throws IOException JavaDoc {
172         
173         try{
174         if (binaryInputStream != null)
175             binaryInputStream.close();
176         binaryInputStream = null;
177
178         }finally{
179         
180         blob = null;
181         clob = null;
182         dataResultSet = null;
183         }
184         
185     }
186
187     /**
188      *
189      *
190      * @see java.lang.Object#equals(java.lang.Object)
191      */

192     public boolean equals(Object JavaDoc arg0) {
193         return binaryInputStream.equals(arg0);
194     }
195
196     /**
197      *
198      *
199      * @see java.lang.Object#hashCode()
200      */

201     public int hashCode() {
202         return binaryInputStream.hashCode();
203     }
204
205     /**
206      *
207      *
208      * @see java.io.InputStream#mark(int)
209      */

210     public void mark(int arg0) {
211         binaryInputStream.mark(arg0);
212     }
213
214     /**
215      *
216      *
217      * @see java.io.InputStream#markSupported()
218      */

219     public boolean markSupported() {
220         return binaryInputStream.markSupported();
221     }
222
223     /**
224      *
225      *
226      * @see java.io.InputStream#read(byte[])
227      */

228     public int read(byte[] arg0) throws IOException JavaDoc {
229         return binaryInputStream.read(arg0);
230     }
231
232     /**
233      *
234      *
235      * @see java.io.InputStream#read(byte[], int, int)
236      */

237     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
238         return binaryInputStream.read(arg0, arg1, arg2);
239     }
240
241     /**
242      *
243      *
244      * @see java.io.InputStream#reset()
245      */

246     public void reset() throws IOException JavaDoc {
247         binaryInputStream.reset();
248     }
249
250     /**
251      *
252      *
253      * @see java.io.InputStream#skip(long)
254      */

255     public long skip(long arg0) throws IOException JavaDoc {
256         return binaryInputStream.skip(arg0);
257     }
258
259
260     protected boolean isEmptyStream(){
261     return isEmptyStream;
262     }
263     
264     
265     /**
266      * This method takes information of ResultSet and
267      * initialize binaryInputStream variable of this object with not empty stream and return true.
268      * If the stream was empty, this method remain binaryInputStream null and return false.
269      *
270      * @param rs ResultSet object to get stream from.
271      * @param column index number of column in ResultSet to get stream.
272      * @param ndrdaType describe type column to get stream.
273      *
274      * @return true if the stream was not empty, false if the stream was empty.
275      *
276      */

277     private boolean initInputStream(ResultSet JavaDoc rs,
278                     int column,
279                     int ndrdaType)
280     throws SQLException JavaDoc,
281            IOException JavaDoc
282     {
283
284     InputStream JavaDoc is = null;
285     try{
286         // BLOBS
287
if (ndrdaType == DRDAConstants.DRDA_TYPE_NLOBBYTES)
288         {
289             blob = rs.getBlob(column);
290             if(blob == null){
291             return false;
292             }
293             
294             is = blob.getBinaryStream();
295             
296         }
297         // CLOBS
298
else if (ndrdaType == DRDAConstants.DRDA_TYPE_NLOBCMIXED)
299         {
300             try {
301             clob = rs.getClob(column);
302             
303             if(clob == null){
304                 return false;
305             }
306
307             is = new ReEncodedInputStream(clob.getCharacterStream());
308             
309             }catch (java.io.UnsupportedEncodingException JavaDoc e) {
310             throw new SQLException JavaDoc (e.getMessage());
311             
312             }catch (IOException JavaDoc e){
313             throw new SQLException JavaDoc (e.getMessage());
314             
315             }
316             
317         }
318         else
319         {
320             if (SanityManager.DEBUG)
321             {
322                 SanityManager.THROWASSERT("NDRDAType: " + ndrdaType +
323                               " not valid EXTDTA object type");
324             }
325         }
326         
327         boolean exist = is.read() > -1;
328         
329         is.close();
330         is = null;
331         
332         if(exist){
333         openInputStreamAgain();
334         }
335
336         return exist;
337         
338     }catch(IllegalStateException JavaDoc e){
339         throw Util.javaException(e);
340
341     }finally{
342         if(is != null)
343         is.close();
344         
345     }
346     
347     }
348     
349     
350     /**
351      *
352      * This method is called from initInputStream and
353      * opens inputstream again to stream actually.
354      *
355      */

356     private void openInputStreamAgain() throws IllegalStateException JavaDoc,SQLException JavaDoc {
357     
358     if(this.binaryInputStream != null){
359         return;
360     }
361         
362     InputStream JavaDoc is = null;
363     try{
364         
365         if(SanityManager.DEBUG){
366         SanityManager.ASSERT( ( blob != null && clob == null ) ||
367                       ( clob != null && blob == null ),
368                       "One of blob or clob must be non-null.");
369         }
370
371         if(blob != null){
372         is = blob.getBinaryStream();
373         
374         }else if(clob != null){
375         is = new ReEncodedInputStream(clob.getCharacterStream());
376         }
377         
378     }catch(IOException JavaDoc e){
379         throw new IllegalStateException JavaDoc(e.getMessage());
380     }
381     
382     if(! is.markSupported() ){
383         is = new BufferedInputStream JavaDoc(is);
384     }
385
386     this.binaryInputStream = is;
387
388     }
389     
390     
391     protected void finalize() throws Throwable JavaDoc{
392     close();
393     }
394
395
396 }
397
Popular Tags