Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » store » jdbc » adapter » [javadoc | source]
    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.activemq.store.jdbc.adapter;
   18   
   19   import java.io.ByteArrayOutputStream;
   20   import java.io.IOException;
   21   import java.io.InputStream;
   22   import java.sql.PreparedStatement;
   23   import java.sql.ResultSet;
   24   import java.sql.SQLException;
   25   
   26   import org.apache.activemq.util.ByteArrayInputStream;
   27   
   28   /**
   29    * This JDBCAdapter inserts and extracts BLOB data using the
   30    * setBinaryStream()/getBinaryStream() operations.
   31    * 
   32    * The databases/JDBC drivers that use this adapter are:
   33    * <ul>
   34    * <li>Axion</li>
   35    * </ul>
   36    * 
   37    * @org.apache.xbean.XBean element="streamJDBCAdapter"
   38    * 
   39    * @version $Revision: 1.2 $
   40    */
   41   public class StreamJDBCAdapter extends DefaultJDBCAdapter {
   42   
   43       /**
   44        * @see org.apache.activemq.store.jdbc.adapter.DefaultJDBCAdapter#getBinaryData(java.sql.ResultSet,
   45        *      int)
   46        */
   47       protected byte[] getBinaryData(ResultSet rs, int index) throws SQLException {
   48   
   49           try {
   50               InputStream is = rs.getBinaryStream(index);
   51               ByteArrayOutputStream os = new ByteArrayOutputStream(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 e) {
   62               throw (SQLException)new SQLException("Error reading binary parameter: " + index).initCause(e);
   63           }
   64       }
   65   
   66       /**
   67        * @see org.apache.activemq.store.jdbc.adapter.DefaultJDBCAdapter#setBinaryData(java.sql.PreparedStatement,
   68        *      int, byte[])
   69        */
   70       protected void setBinaryData(PreparedStatement s, int index, byte[] data) throws SQLException {
   71           s.setBinaryStream(index, new ByteArrayInputStream(data), data.length);
   72       }
   73   
   74   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » store » jdbc » adapter » [javadoc | source]