von luial » Di, 20.03.2007 14:12
Hier meine Klasse XInputStreamWrapper
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.sun.star.io.BufferSizeExceededException;
import com.sun.star.io.NotConnectedException;
import com.sun.star.io.XInputStream;
import com.sun.star.io.XSeekable;
import com.sun.star.lang.IllegalArgumentException;
public class XInputStreamWrapper implements XInputStream, XSeekable
{
private InputStream in;
private int pos;
private byte[] buffer;
/**
*
*/
public XInputStreamWrapper(InputStream in)
{
this.in = in;
bufferStream(in);
}
/*
* (non-Javadoc)
* @see com.sun.star.io.XInputStream#readBytes(byte[][], int)
*
* [26.01.2007] alice ArrayOutOfBoundException verhindert, weil OpenOffice über die tatsächliche Dateigröße hinaus lesen will ?!
* Dieses Problem trat bei sehr kleinen (TXT-) Dateien auf, z. B. kleiner als 1 kb
*/
public int readBytes(
byte[][] b,
int length) throws NotConnectedException, com.sun.star.io.IOException
{
// System.out.println("readByte:" + length + " bytes:" + b.length);
int count = 0;
if (length > 0)
{
count = buffer.length - pos;
b[0] = new byte[length];
// nur noch den Rest lesen
if (length > count && count > 0)
{
System.arraycopy(buffer, pos, b[0], 0, count);
pos = buffer.length;
return count;
}
else if (pos < buffer.length)
{
System.arraycopy(buffer, pos, b[0], 0, length);
pos += length;
return length;
}
else
{
return 0;
}
}
else
{
b[0]= new byte[0];
}
return 0;
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#readSomeBytes(byte[][], int)
*/
public int readSomeBytes(
byte[][] arg0,
int arg1)
throws NotConnectedException, BufferSizeExceededException,
com.sun.star.io.IOException
{
//TODO check this later
// System.out.println("readSomeByte:" + arg1);
return readBytes(arg0, arg1);
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#skipBytes(int)
*/
public void skipBytes(int arg0)
throws NotConnectedException, BufferSizeExceededException,
com.sun.star.io.IOException
{
// System.out.println("skip:" + arg0);
pos += arg0;
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#available()
*/
public int available()
throws NotConnectedException, com.sun.star.io.IOException
{
return buffer.length - pos;
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#closeInput()
*/
public void closeInput()
throws NotConnectedException, com.sun.star.io.IOException
{
//help the gc
buffer = null;
}
public long getLength() throws com.sun.star.io.IOException
{
// System.out.println("getlength:" + buffer.length);
return buffer.length;
}
public long getPosition() throws com.sun.star.io.IOException
{
// System.out.println("position");
return pos;
}
public void seek(long arg0)
throws IllegalArgumentException, com.sun.star.io.IOException
{
// System.out.println("seek:" + arg0);
pos = (int) arg0;
}
private void bufferStream(InputStream in)
{
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
int current = -1;
byte[] b = new byte[2048];
// [25.02.2007] alice read(b, 0, current) durch read(b) ersetzt, weil sonst -1 zurückkommt bei Dateien < 2048 Bytes
while ((current = in.read(b)) != -1)
{
out.write(b, 0, current);
}
in.close();
in = null;
buffer = out.toByteArray();
out = null;
pos = 0;
}
catch (java.io.IOException e)
{
}
}
}
[b]Hier meine Klasse XInputStreamWrapper [/b]
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.sun.star.io.BufferSizeExceededException;
import com.sun.star.io.NotConnectedException;
import com.sun.star.io.XInputStream;
import com.sun.star.io.XSeekable;
import com.sun.star.lang.IllegalArgumentException;
public class XInputStreamWrapper implements XInputStream, XSeekable
{
private InputStream in;
private int pos;
private byte[] buffer;
/**
*
*/
public XInputStreamWrapper(InputStream in)
{
this.in = in;
bufferStream(in);
}
/*
* (non-Javadoc)
* @see com.sun.star.io.XInputStream#readBytes(byte[][], int)
*
* [26.01.2007] alice ArrayOutOfBoundException verhindert, weil OpenOffice über die tatsächliche Dateigröße hinaus lesen will ?!
* Dieses Problem trat bei sehr kleinen (TXT-) Dateien auf, z. B. kleiner als 1 kb
*/
public int readBytes(
byte[][] b,
int length) throws NotConnectedException, com.sun.star.io.IOException
{
// System.out.println("readByte:" + length + " bytes:" + b.length);
int count = 0;
if (length > 0)
{
count = buffer.length - pos;
b[0] = new byte[length];
// nur noch den Rest lesen
if (length > count && count > 0)
{
System.arraycopy(buffer, pos, b[0], 0, count);
pos = buffer.length;
return count;
}
else if (pos < buffer.length)
{
System.arraycopy(buffer, pos, b[0], 0, length);
pos += length;
return length;
}
else
{
return 0;
}
}
else
{
b[0]= new byte[0];
}
return 0;
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#readSomeBytes(byte[][], int)
*/
public int readSomeBytes(
byte[][] arg0,
int arg1)
throws NotConnectedException, BufferSizeExceededException,
com.sun.star.io.IOException
{
//TODO check this later
// System.out.println("readSomeByte:" + arg1);
return readBytes(arg0, arg1);
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#skipBytes(int)
*/
public void skipBytes(int arg0)
throws NotConnectedException, BufferSizeExceededException,
com.sun.star.io.IOException
{
// System.out.println("skip:" + arg0);
pos += arg0;
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#available()
*/
public int available()
throws NotConnectedException, com.sun.star.io.IOException
{
return buffer.length - pos;
}
/*
* (non-Javadoc)
*
* @see com.sun.star.io.XInputStream#closeInput()
*/
public void closeInput()
throws NotConnectedException, com.sun.star.io.IOException
{
//help the gc
buffer = null;
}
public long getLength() throws com.sun.star.io.IOException
{
// System.out.println("getlength:" + buffer.length);
return buffer.length;
}
public long getPosition() throws com.sun.star.io.IOException
{
// System.out.println("position");
return pos;
}
public void seek(long arg0)
throws IllegalArgumentException, com.sun.star.io.IOException
{
// System.out.println("seek:" + arg0);
pos = (int) arg0;
}
private void bufferStream(InputStream in)
{
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
int current = -1;
byte[] b = new byte[2048];
// [25.02.2007] alice read(b, 0, current) durch read(b) ersetzt, weil sonst -1 zurückkommt bei Dateien < 2048 Bytes
while ((current = in.read(b)) != -1)
{
out.write(b, 0, current);
}
in.close();
in = null;
buffer = out.toByteArray();
out = null;
pos = 0;
}
catch (java.io.IOException e)
{
}
}
}