View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.dataset.datatype;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.sql.PreparedStatement;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  
34  
35  /**
36   * @author fede
37   * @author Last changed by: $Author$
38   * @version $Revision$ $Date$
39   * @since Sep 12, 2004 (pre 2.3)
40   */
41  public class BinaryStreamDataType extends BytesDataType
42  {
43  
44      /**
45       * Logger for this class
46       */
47      private static final Logger logger = LoggerFactory.getLogger(BinaryStreamDataType.class);
48  
49      public BinaryStreamDataType(String name, int sqlType)
50      {
51          super(name, sqlType);
52      }
53  
54      public Object getSqlValue(int column, ResultSet resultSet)
55              throws SQLException, TypeCastException
56      {
57      	if(logger.isDebugEnabled())
58      		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
59  
60          InputStream in = resultSet.getBinaryStream(column);
61          if (in == null || resultSet.wasNull())
62          {
63              return null;
64          }
65  
66          try
67          {
68              ByteArrayOutputStream out = new ByteArrayOutputStream();
69              byte[] buffer = new byte[32];
70              int length = in.read(buffer);
71              while (length != -1)
72              {
73                  out.write(buffer, 0, length);
74                  length = in.read(buffer);
75              }
76              return out.toByteArray();
77          }
78          catch (IOException e)
79          {
80              throw new TypeCastException(e);
81          }
82      }
83  
84      /**
85       * Sets the given value on the given statement and therefore invokes 
86       * {@link BytesDataType#typeCast(Object)}.
87       * @see org.dbunit.dataset.datatype.BytesDataType#setSqlValue(java.lang.Object, int, java.sql.PreparedStatement)
88       */
89      public void setSqlValue(Object value, int column, PreparedStatement statement)
90              throws SQLException, TypeCastException
91      {
92      	if(logger.isDebugEnabled())
93      		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
94          		new Object[]{value, new Integer(column), statement} );
95  
96          byte[] bytes = (byte[])typeCast(value);
97          if(value==null || bytes==null)
98          {
99              logger.debug("Setting SQL column value to <null>");
100             statement.setNull(column, getSqlType());
101         }
102         else
103         {
104             ByteArrayInputStream in = new ByteArrayInputStream(bytes);
105             statement.setBinaryStream(column, in, bytes.length);
106         }
107         
108     }
109 
110 }