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

发布于 2024-10-10 06:45:08 字数 1136 浏览 0 评论 0原文

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

注意 - 我无法在此类上实现 IExternalizable,因为它不是我的。

我已经注册了一个自定义 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;
       }
}

然后在启动过程中注册该代理,如下所示:

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

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

我做错了什么?

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

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

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

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.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2024-10-17 06:45:09

好的 - 解决了这个问题。

问题在于,当代理对象时,getInstanceToSerialize 返回的值会根据其单独的属性进行序列化 - 即序列化为对象。

您不能在此处返回“原始”类型(引用 javadocs)。就原语而言,它们指的是原语 Flashplayer 类型。由于我希望 byte[] 作为 ByteArray 到达 - 这是一种原始类型,所以我原来的方法不起作用。

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

这是工作代理:

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);
        }
    }

}

假设我试图序列化以下类:

public class MyThing
{
     InputStream myStream;
     ...
 }

这将注册如下:

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

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.

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.

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文