sallad General User

Joined: 19 Feb 2008 Posts: 8
|
Posted: Thu Feb 28, 2008 2:36 pm Post subject: XInputStream2InputStream (java) |
|
|
Below is a small function I used to transfer data from a com.sun.star.io.XInputStream to a java.io.InputStream.
| Code: |
static final int BLOCK = 1024;
public static InputStream XInputStream2InputStream(XInputStream xin)
{
InputStream in = null;
try
{
byte[] buf = new byte[0];
byte[][] b = new byte[1][BLOCK];
int size = 0;
int res = 0;
while ((res = xin.readSomeBytes(b, BLOCK)) == BLOCK)
{
// we have just read a full block, allocate another one
byte[] b2=new byte[size+res];
System.arraycopy(buf,0,b2,0,size);
System.arraycopy(b[0],0,b2,size, res);
buf = b2;
size += res;
}
byte[] b2=new byte[size+res];
System.arraycopy(buf,0,b2,0,size);
System.arraycopy(b[0],0,b2,size, res);
buf = b2;
in = new ByteArrayInputStream(buf);
}
catch (com.sun.star.io.IOException e)
{ System.out.println("DOH!"); }
return in;
}
|
I have some legacy parsers that I'd like to use with OOo's XExtendedFilterDetection and import/export XFilter services. My old parsers, naturally, take the java default InputStream rather than OOo. I'm a novice OOo programmer, but this code seems to work. Is it a good idea to do this? Are there any downsides to this dirty hack? |
|