Flex / LCDS:使用BeanProxy将InputStream序列化为ByteArray

时间:2022-03-11 18:19:36

I'm trying to serialize an object which has an InputStream. I need it to arrive on the flex client as a ByteArray.

我正在尝试序列化具有InputStream的对象。我需要它作为ByteArray到达flex客户端。

Note - I can't implement IExternalizable on this class, as it's not mine.

注意 - 我不能在这个类上实现IExternalizable,因为它不是我的。

I've registered a custom BeanProxy to do the conversion, however it doesn't appear to be working:

我已经注册了一个自定义BeanProxy来进行转换,但它似乎没有工作:

public class InputStreamBeanProxy extends BeanProxy {
    @Override
    public Object getInstanceToSerialize(Object instance) {

        InputStream stream = (InputStream) instance;
        Byte[] boxOfBytes;
            try {
                byte[] bytes = IOUtils.toByteArray(stream);
                boxOfBytes = new Byte[bytes.length];
                for (int i=0; i < bytes.length; i++)
                {
                   boxOfBytes[i] = bytes[i];
                }
            } catch (IOException e) {
                logger.error("Exception serializing inputStream: ", e);
                throw new RuntimeException(e);
            }
            return boxOfBytes;
       }
}

This proxy is then registered during startup as follows:

然后在启动期间注册此代理,如下所示:

PropertyProxyRegistry.getRegistry().register(InputStream.class, new InputStreamBeanProxy());

I've set breakpoints in this code, and I see it being called as expected. However when the object arrives on the client, the input stream is typed as Object, and it contains no properties.

我在这段代码中设置了断点,我看到它按预期被调用。但是,当对象到达客户端时,输入流将被键入为Object,并且它不包含任何属性。

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


0  

Ok - solved this.

好的 - 解决了这个。

The problem is that when proxying an object, the value returned by getInstanceToSerialize is then serialized based on it's individual properties - ie serialized as an object.

问题在于,在代理对象时,getInstanceToSerialize返回的值然后根据它的各个属性进行序列化 - 即序列化为对象。

You cannot return a "primitive" type here (to quote the javadocs). By primitive, they refer to primtiive flashplayer types. Since I wanted byte[] to arrive as a ByteArray - which is a primitive type, my original approach doesn't work.

你不能在这里返回一个“原始”类型(引用javadocs)。原始的,他们指的是原始的flashplayer类型。因为我希望byte []作为ByteArray到达 - 这是一种原始类型,我的原始方法不起作用。

Instead, the solution was to proxy the property of the owning class, rather than the InputStream directly.

相反,解决方案是代理拥有类的属性,而不是直接代理InputStream。

Here's the working proxy:

这是工作代理:

public class InputStreamBeanProxy extends BeanProxy {

    @Override
    public Object getValue(Object instance, String propertyName) {
        Object value = super.getValue(instance, propertyName);
        if (value instanceof InputStream) {
            value = getByteArray((InputStream) value);
        }
        return value;
    }

    private byte[] getByteArray(InputStream stream) {

        try {
            byte[] bytes = IOUtils.toByteArray(stream);
            return bytes;
        } catch (IOException e) {
            logger.error("Exception serializing inputStream: ", e);
            throw new RuntimeException(e);
        }
    }

}

Assuming I was trying to serialize the following class:

假设我正在尝试序列化以下类:

public class MyThing
{
     InputStream myStream;
     ...
 }

This would be registered as follows:

这将注册如下:

PropertyProxyRegistry.getRegistry().register(MyThing.class, new InputStreamBeanProxy());

#1


0  

Ok - solved this.

好的 - 解决了这个。

The problem is that when proxying an object, the value returned by getInstanceToSerialize is then serialized based on it's individual properties - ie serialized as an object.

问题在于,在代理对象时,getInstanceToSerialize返回的值然后根据它的各个属性进行序列化 - 即序列化为对象。

You cannot return a "primitive" type here (to quote the javadocs). By primitive, they refer to primtiive flashplayer types. Since I wanted byte[] to arrive as a ByteArray - which is a primitive type, my original approach doesn't work.

你不能在这里返回一个“原始”类型(引用javadocs)。原始的,他们指的是原始的flashplayer类型。因为我希望byte []作为ByteArray到达 - 这是一种原始类型,我的原始方法不起作用。

Instead, the solution was to proxy the property of the owning class, rather than the InputStream directly.

相反,解决方案是代理拥有类的属性,而不是直接代理InputStream。

Here's the working proxy:

这是工作代理:

public class InputStreamBeanProxy extends BeanProxy {

    @Override
    public Object getValue(Object instance, String propertyName) {
        Object value = super.getValue(instance, propertyName);
        if (value instanceof InputStream) {
            value = getByteArray((InputStream) value);
        }
        return value;
    }

    private byte[] getByteArray(InputStream stream) {

        try {
            byte[] bytes = IOUtils.toByteArray(stream);
            return bytes;
        } catch (IOException e) {
            logger.error("Exception serializing inputStream: ", e);
            throw new RuntimeException(e);
        }
    }

}

Assuming I was trying to serialize the following class:

假设我正在尝试序列化以下类:

public class MyThing
{
     InputStream myStream;
     ...
 }

This would be registered as follows:

这将注册如下:

PropertyProxyRegistry.getRegistry().register(MyThing.class, new InputStreamBeanProxy());