KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > store > jdbc > adapter > StreamJDBCAdapter


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

18 package org.apache.activemq.store.jdbc.adapter;
19
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 import org.apache.activemq.util.ByteArrayInputStream;
28
29 /**
30  * This JDBCAdapter inserts and extracts BLOB data using the
31  * setBinaryStream()/getBinaryStream() operations.
32  *
33  * The databases/JDBC drivers that use this adapter are:
34  * <ul>
35  * <li>Axion</li>
36  * </ul>
37  *
38  * @org.apache.xbean.XBean element="streamJDBCAdapter"
39  *
40  * @version $Revision: 1.2 $
41  */

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

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

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