001 /**
002 *
003 * Copyright 2004 Hiram Chirino
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.store.jdbc.adapter;
019
020 import java.io.ByteArrayInputStream;
021 import java.io.ByteArrayOutputStream;
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.sql.PreparedStatement;
025 import java.sql.ResultSet;
026 import java.sql.SQLException;
027
028 import org.activemq.store.jdbc.StatementProvider;
029
030 /**
031 * This JDBCAdapter inserts and extracts BLOB data using the
032 * setBinaryStream()/getBinaryStream() operations.
033 *
034 * The databases/JDBC drivers that use this adapter are:
035 * <ul>
036 * <li>Axion</li>
037 * </ul>
038 *
039 * @version $Revision: 1.1 $
040 */
041 public class StreamJDBCAdapter extends DefaultJDBCAdapter {
042
043 public StreamJDBCAdapter() {
044 super();
045 }
046
047 public StreamJDBCAdapter(StatementProvider provider) {
048 super(provider);
049 }
050
051 /**
052 * @see org.activemq.store.jdbc.adapter.DefaultJDBCAdapter#getBinaryData(java.sql.ResultSet, int)
053 */
054 protected byte[] getBinaryData(ResultSet rs, int index) throws SQLException {
055
056 try {
057 InputStream is = rs.getBinaryStream(index);
058 ByteArrayOutputStream os = new ByteArrayOutputStream(1024 * 4);
059
060 int ch;
061 while ((ch = is.read()) >= 0) {
062 os.write(ch);
063 }
064 is.close();
065 os.close();
066
067 return os.toByteArray();
068 } catch (IOException e) {
069 throw (SQLException)new SQLException("Error reading binary parameter: "+index).initCause(e);
070 }
071 }
072
073 /**
074 * @see org.activemq.store.jdbc.adapter.DefaultJDBCAdapter#setBinaryData(java.sql.PreparedStatement, int, byte[])
075 */
076 protected void setBinaryData(PreparedStatement s, int index, byte[] data) throws SQLException {
077 s.setBinaryStream(index, new ByteArrayInputStream(data), data.length);
078 }
079
080 }