Flex / LCDS:使用 BeanProxy 将 InputStream 序列化为 ByteArray
我正在尝试序列化一个具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的 - 解决了这个问题。
问题在于,当代理对象时,
getInstanceToSerialize
返回的值会根据其单独的属性进行序列化 - 即序列化为对象。您不能在此处返回“原始”类型(引用 javadocs)。就原语而言,它们指的是原语 Flashplayer 类型。由于我希望
byte[]
作为ByteArray
到达 - 这是一种原始类型,所以我原来的方法不起作用。相反,解决方案是代理所属类的属性,而不是直接代理 InputStream。
这是工作代理:
假设我试图序列化以下类:
这将注册如下:
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 aByteArray
- 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:
Assuming I was trying to serialize the following class:
This would be registered as follows: