KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jdbc > adapter > StreamJDBCAdapter


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

17 package org.apache.servicemix.jdbc.adapter;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 /**
28  * This JDBCAdapter inserts and extracts BLOB data using the
29  * setBinaryStream()/getBinaryStream() operations.
30  *
31  * The databases/JDBC drivers that use this adapter are:
32  * <ul>
33  * <li>Axion</li>
34  * </ul>
35  *
36  * @org.apache.xbean.XBean element="streamJDBCAdapter"
37  *
38  * @version $Revision: 1.2 $
39  */

40 public class StreamJDBCAdapter extends DefaultJDBCAdapter {
41     
42     /**
43      * @see org.apache.activemq.store.jdbc.adapter.DefaultJDBCAdapter#getBinaryData(java.sql.ResultSet, int)
44      */

45     protected byte[] getBinaryData(ResultSet JavaDoc rs, int index) throws SQLException JavaDoc {
46         
47         try {
48             InputStream JavaDoc is = rs.getBinaryStream(index);
49             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc(1024 * 4);
50
51             int ch;
52             while ((ch = is.read()) >= 0) {
53                 os.write(ch);
54             }
55             is.close();
56             os.close();
57
58             return os.toByteArray();
59         } catch (IOException JavaDoc e) {
60             throw (SQLException JavaDoc)new SQLException JavaDoc("Error reading binary parameter: "+index).initCause(e);
61         }
62     }
63     
64     /**
65      * @see org.apache.activemq.store.jdbc.adapter.DefaultJDBCAdapter#setBinaryData(java.sql.PreparedStatement, int, byte[])
66      */

67     protected void setBinaryData(PreparedStatement JavaDoc s, int index, byte[] data) throws SQLException JavaDoc {
68         s.setBinaryStream(index, new ByteArrayInputStream JavaDoc(data), data.length);
69     }
70     
71 }
Popular Tags