HibernateByteBlobType.java

  1. // license-header java merge-point
  2. //
  3. // Attention: Generated code! Do not modify by hand!
  4. // Generated by: hibernate/usertypes/HibernateByteBlobType.vsl in andromda-hibernate-cartridge.
  5. //
  6. package org.andromda.persistence.hibernate.usertypes;

  7. import java.io.ByteArrayInputStream;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.Serializable;
  12. import java.sql.Blob;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Types;
  17. import java.util.Arrays;
  18. import org.hibernate.HibernateException;
  19. import org.hibernate.engine.SessionImplementor;
  20. import org.hibernate.usertype.UserType;

  21. /**
  22.  * <p>
  23.  * A hibernate user type which converts a Blob into a byte[] and back again.
  24.  * </p>
  25.  */
  26. public class HibernateByteBlobType
  27.     implements UserType, Serializable
  28. {
  29.     /**
  30.      * The serial version UID of this class. Needed for serialization.
  31.      */
  32.     private static final long serialVersionUID = 4850231348151909385L;

  33.     /**
  34.      * @see org.hibernate.usertype.UserType#sqlTypes()
  35.      */
  36.     @Override
  37.     public int[] sqlTypes()
  38.     {
  39.         return new int[]
  40.         {
  41.             Types.BLOB
  42.         };
  43.     }

  44.     /**
  45.      * @see org.hibernate.usertype.UserType#returnedClass()
  46.      */
  47.     @Override
  48.     public Class<?> returnedClass()
  49.     {
  50.         return byte[].class;
  51.     }

  52.     /**
  53.      * @see org.hibernate.usertype.UserType#equals(Object, Object)
  54.      */
  55.     @Override
  56.     public boolean equals(Object x, Object y)
  57.     {
  58.         return (x == y)
  59.             || (x != null && y != null && Arrays.equals(
  60.                 (byte[])x,
  61.                 (byte[])y));
  62.     }

  63.     /**
  64.      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, String[], Object)
  65.      */
  66.     @Override
  67.     public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException
  68.     {
  69.         final Object object;

  70.         final InputStream inputStream = resultSet.getBinaryStream(names[0]);
  71.         if (inputStream == null)
  72.         {
  73.             object = null;
  74.         }
  75.         else
  76.         {
  77.             final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

  78.             try
  79.             {
  80.                 final byte[] buffer = new byte[65536];
  81.                 int read = -1;

  82.                 while ((read = inputStream.read(buffer)) > -1)
  83.                 {
  84.                     outputStream.write(buffer, 0, read);
  85.                 }
  86.                 outputStream.close();
  87.             }
  88.             catch (IOException exception)
  89.             {
  90.                 throw new HibernateException("Unable to read blob " + names[0], exception);
  91.             }
  92.             object = outputStream.toByteArray();
  93.         }

  94.         return object;
  95.     }

  96.     /**
  97.      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, Object, int)
  98.      */
  99.     @Override
  100.     public void nullSafeSet(PreparedStatement statement, Object value, int index) throws SQLException
  101.     {
  102.         final byte[] bytes = (byte[])value;
  103.         if (bytes == null)
  104.         {
  105.             try
  106.             {
  107.                 statement.setBinaryStream(index, null, 0);
  108.             }
  109.             catch (SQLException exception)
  110.             {
  111.                 Blob nullBlob = null;
  112.                 statement.setBlob(index, nullBlob);
  113.             }
  114.         }
  115.         else
  116.         {
  117.             statement.setBinaryStream(index, new ByteArrayInputStream(bytes), bytes.length);
  118.         }
  119.     }

  120.     /**
  121.      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, String[], Object)
  122.      */
  123.     public Object nullSafeGet(ResultSet resultSet, String[] names,
  124.         SessionImplementor session, Object owner) throws HibernateException, SQLException
  125.     {
  126.         return this.nullSafeGet(resultSet, names, owner);
  127.     }

  128.     /**
  129.      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, Object, int)
  130.      */
  131.     public void nullSafeSet(
  132.         PreparedStatement preparedStatement,
  133.         Object data,
  134.         int index,
  135.         SessionImplementor session)
  136.         throws HibernateException, SQLException
  137.     {
  138.         this.nullSafeSet(preparedStatement, data, index);
  139.     }

  140.     /**
  141.      * @see org.hibernate.usertype.UserType#deepCopy(Object)
  142.      */
  143.     @Override
  144.     public Object deepCopy(Object value)
  145.     {
  146.         if (value == null)
  147.             return null;

  148.         byte[] bytes = (byte[])value;
  149.         byte[] result = new byte[bytes.length];
  150.         System.arraycopy(bytes, 0, result, 0, bytes.length);

  151.         return result;
  152.     }

  153.     /**
  154.      * @see org.hibernate.usertype.UserType#isMutable()
  155.      */
  156.     @Override
  157.     public boolean isMutable()
  158.     {
  159.         return true;
  160.     }

  161.     /**
  162.      * @see org.hibernate.usertype.UserType#replace(Object, Object, Object)
  163.      */
  164.     @Override
  165.     public Object replace(Object original, Object target, Object owner)
  166.     {
  167.         return original;
  168.     }

  169.     /**
  170.      * @see org.hibernate.usertype.UserType#assemble(Serializable, Object)
  171.      */
  172.     @Override
  173.     public Object assemble(Serializable cached, Object owner)
  174.     {
  175.         return cached;
  176.     }

  177.     /**
  178.      * @see org.hibernate.usertype.UserType#disassemble(Object)
  179.      */
  180.     @Override
  181.     public Serializable disassemble(Object value)
  182.     {
  183.         return (Serializable)value;
  184.     }

  185.     /**
  186.      * @param x
  187.      * @return x.hashCode()
  188.      * @see Object#hashCode()
  189.      */
  190.     @Override
  191.     public int hashCode(Object x)
  192.     {
  193.         return x.hashCode();
  194.     }
  195. }