mnimer / dphibernate

Automatically exported from code.google.com/p/dphibernate
0 stars 0 forks source link

serialize ByteArray of image #36

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
After converting to use dpHibernate, I am unable display png images of
ByteArray.  My question is whether this data is supported i.e. java byte[]
-> AS3 ByteArray.  Or I am missing something in my setup.

Thanks.
JT

Original issue reported on code.google.com by jinteck...@gmail.com on 10 Feb 2010 at 9:03

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I too am getting a similar issue with the tags\lazyoverflow release.

Here's the annotated property that is causing the problem the column is defined 
as a LongBlob in the MySQL DB):

  @Lob
  @Column
  private byte[] image;
  public byte[] getImage() { return this.image; }
  public void setImage(byte[] image) { this.image = image; }

I am getting the exception:

  java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
  at net.digitalprimates.persistence.translators.hibernate.HibernateSerializer.writeBean(Unknown Source)

I think this means that there is an attempt to cast a java byte[] to Object[]

Is it possible that the serialization of arrays of byte[] need to be handled 
differently.

I tried amending HibernateSerializer.java:writeBean() (I am using revision 157 
from http://dphibernate.googlecode.com/svn/tags/lazyoverflow/hibernateAdapter) 
as shown below so that instead of casting the byte[] to Object[] the revised 
test causes it to fall through to the ultimate else statement.

  } else if (objectToSerialize.getClass().isArray() && !(objectToSerialize instanceof byte[]))

As a result I don’t get the exception but then the object fails to 
deserialize on the Flex side with the error shown below:

  TypeError: Error #1034: Type Coercion failed: cannot convert Object@c336b79 to flash.utils.ByteArray.

Heres the message:

  [BlazeDS][DEBUG] Serializing AMF/HTTP response
  Version: 3
    (Message #0 targetURI=/25/onResult, responseURI=)
      (Externalizable Object #0 'DSK')
        (Externalizable Object #1 'flex.messaging.io.ArrayCollection')
          (Array #2)
            [0] = (Typed Object #3 'com.twoh.salt.model.FileImage')
              id = 268
              uid = "af4948a5-0813-4159-b76b-751c205a7e98"
              image = (Typed Object #18 '[B')
                uid = "639d9b78-c5af-4071-bc7d-c8a103e6aa48"
                proxyInitialized = true
              proxyKey = 268
              proxyInitialized = true

Looking again at HibernateSerializer I am now wondering if I need to write 
method something like the one below that creates a special type of object so 
that the Flex end knows to deserialize it as a ByteArray.

  private ASObject writeByteArray(Object obj, Object key)

Original comment by aaron.tr...@gmail.com on 12 Nov 2010 at 9:46

GoogleCodeExporter commented 9 years ago
OK this is without doubt a kludge as I am no Java/[dp]Hibernate wizard but this 
fixed it for me. I amended HibernateSerializer.java and inserted the lines 
below  before the test "} else if (objectToSerialize.getClass().isArray())" as 
shown below:

} else if (objectToSerialize instanceof byte[]) 
{
  cache.store(cacheKey, objectToSerialize);
  result = objectToSerialize;
} else if (objectToSerialize.getClass().isArray())

Now I get the response message:

  [BlazeDS][DEBUG] Serializing AMF/HTTP response
  Version: 3
    (Message #0 targetURI=/25/onResult, responseURI=)
      (Externalizable Object #0 'DSK')
        (Externalizable Object #1 'flex.messaging.io.ArrayCollection')
          (Array #2)
            [0] = (Typed Object #3 'com.twoh.salt.model.FileImage')
              id = 268
              uid = "af4948a5-0813-4159-b76b-751c205a7e98"
              image = (Byte Array #18, Length 25077)
              proxyKey = 268
              proxyInitialized = true

I do wonder if arrays of other primitive types need special treatment or is a 
byte[] a special case?

Original comment by aaron.tr...@gmail.com on 12 Nov 2010 at 10:32

GoogleCodeExporter commented 9 years ago
The provided patch has been committed in r242

Original comment by martypit...@gtempaccount.com on 3 Feb 2011 at 5:10