Attiej Attiej - 2 months ago 18
Ajax Question

byte[] to iresourcestream in Wicket

I would like to implement the example of this page.

I'm stuck at the

getResourceStream()
method. My application has a
byte[]
(generated XML file), that users should be able to download. The problem is that I don't know how to convert a
byte[]
to
IResourceStream
.

This is what I have:

final AJAXDownload download = new AJAXDownload()
{
private static final long serialVersionUID = 1L;

@Override
protected IResourceStream getResourceStream()
{
ByteArrayResource bar = new ByteArrayResource("TEXT??", xmlFileInBytes);
return (IResourceStream) bar;
}
};


The code above gives a
ClassCastException
. Can someone explain how can I solve this problem?

Answer

You can use the class StringResourceStream which is a child of IResourceStream and to use the byte array:

You can do something like this:

  byte byteArray[] = new byte[1024]   //this is the byte array containing the xml which you want to use.
  StringResourceStream srs = new StringResourceStream(new String(byteArray));

So in your case the method will look something like below:

 final AJAXDownload download = new AJAXDownload()
 {
     private static final long serialVersionUID = 1L;

     @Override
     protected IResourceStream getResourceStream()
     {
         StringResourceStream bar = new StringResourceStream (new String(xmlFileInBytes) );
                 return (IResourceStream) bar;
         }
     };