BinaryStreamDataType.java

  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. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.sql.PreparedStatement;
  27. import java.sql.ResultSet;
  28. import java.sql.SQLException;

  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * @author fede
  33.  * @author Last changed by: $Author$
  34.  * @version $Revision$ $Date$
  35.  * @since Sep 12, 2004 (pre 2.3)
  36.  */
  37. public class BinaryStreamDataType extends BytesDataType
  38. {
  39.     private static final Logger logger =
  40.             LoggerFactory.getLogger(BinaryStreamDataType.class);

  41.     public BinaryStreamDataType(final String name, final int sqlType)
  42.     {
  43.         super(name, sqlType);
  44.     }

  45.     @Override
  46.     public Object getSqlValue(final int column, final ResultSet resultSet)
  47.             throws SQLException, TypeCastException
  48.     {
  49.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
  50.                 resultSet);
  51.         final InputStream in = resultSet.getBinaryStream(column);
  52.         final Object value = resultSet.wasNull() ? null : readValue(in);
  53.         logger.debug("getSqlValue: column={}, value={}", column, value);
  54.         return value;
  55.     }

  56.     private Object readValue(final InputStream in) throws TypeCastException
  57.     {
  58.         try
  59.         {
  60.             final ByteArrayOutputStream out = new ByteArrayOutputStream();
  61.             final byte[] buffer = new byte[32];
  62.             int length = in.read(buffer);
  63.             while (length != -1)
  64.             {
  65.                 out.write(buffer, 0, length);
  66.                 length = in.read(buffer);
  67.             }
  68.             return out.toByteArray();
  69.         } catch (final IOException e)
  70.         {
  71.             throw new TypeCastException(e);
  72.         }
  73.     }

  74.     /**
  75.      * Sets the given value on the given statement and therefore invokes
  76.      * {@link BytesDataType#typeCast(Object)}.
  77.      *
  78.      * @see org.dbunit.dataset.datatype.BytesDataType#setSqlValue(java.lang.Object,
  79.      *      int, java.sql.PreparedStatement)
  80.      */
  81.     @Override
  82.     public void setSqlValue(final Object value, final int column,
  83.             final PreparedStatement statement)
  84.             throws SQLException, TypeCastException
  85.     {
  86.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  87.                 value, column, statement);

  88.         final byte[] bytes = (byte[]) typeCast(value);
  89.         if (value == null || bytes == null)
  90.         {
  91.             logger.debug("Setting SQL column value to <null>");
  92.             statement.setNull(column, getSqlType());
  93.         } else
  94.         {
  95.             final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
  96.             statement.setBinaryStream(column, in, bytes.length);
  97.         }
  98.     }
  99. }