StreamJDBCAdapter xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * 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   **/
    18  package org.codehaus.activemq.store.jdbc.adapter;
    19
    20  import java.io.ByteArrayInputStream;
    21  import java.io.ByteArrayOutputStream;
    22  import java.io.IOException;
    23  import java.io.InputStream;
    24  import java.sql.PreparedStatement;
    25  import java.sql.ResultSet;
    26  import java.sql.SQLException;
    27
    28  import org.codehaus.activemq.store.jdbc.StatementProvider;
    29
    30  /***
    31   * This JDBCAdapter inserts and extracts BLOB data using the
    32   * setBinaryStream()/getBinaryStream() operations.
    33   *
    34   * The databases/JDBC drivers that use this adapter are:
    35   * <ul>
    36   * <li>Axion</li>
    37   * </ul>
    38   *
    39   * @version $Revision: 1.1 $
    40   */
    41  public class StreamJDBCAdapter extends DefaultJDBCAdapter {
    42
    43      public StreamJDBCAdapter() {
    44          super();
    45      }
    46
    47      public StreamJDBCAdapter(StatementProvider provider) {
    48          super(provider);
    49      }
    50
    51      /***
    52       * @see org.codehaus.activemq.store.jdbc.adapter.DefaultJDBCAdapter#getBinaryData(java.sql.ResultSet, int)
    53       */
    54      protected byte[] getBinaryData(ResultSet rs, int index) throws SQLException {
    55
    56          try {
    57              InputStream is = rs.getBinaryStream(index);
    58              ByteArrayOutputStream os = new ByteArrayOutputStream(1024 * 4);
    59
    60              int ch;
    61              while ((ch = is.read()) >= 0) {
    62                  os.write(ch);
    63              }
    64              is.close();
    65              os.close();
    66
    67              return os.toByteArray();
    68          } catch (IOException e) {
    69              throw (SQLException)new SQLException("Error reading binary parameter: "+index).initCause(e);
    70          }
    71      }
    72
    73      /***
    74       * @see org.codehaus.activemq.store.jdbc.adapter.DefaultJDBCAdapter#setBinaryData(java.sql.PreparedStatement, int, byte[])
    75       */
    76      protected void setBinaryData(PreparedStatement s, int index, byte[] data) throws SQLException {
    77          s.setBinaryStream(index, new ByteArrayInputStream(data), data.length);
    78      }
    79
    80  }