-
FileInputStream
I'm trying to create my own ProgressFileInputStream
It has Listeners that get informed whenever a read is done with the number of bytes read.
I've extended FileInputStream and rewritten all the read methods and the skip method. After each read i inform the listeners of the number of bytes read.
Unfortunately when i add up all the bytes received by the listener it equals 362. When i do fileObject.length() i get 1121. Where have the other 759 bytes gone!!!
I know the filObject.length() is right as windows reports the size as 1,121 bytes.
This is my code in the ProgressFileInputStream class...
Code:
public int read() throws IOException
{
int value = super.read();
readNBytes(1);
return value;
}
public int read(byte[] b) throws IOException
{
int value = super.read(b);
readNBytes(value);
return value;
}
public int read(byte[] b, int off, int len) throws IOException
{
int value = super.read(b, off, len);
readNBytes(value);
return value;
}
public long skip(long n) throws IOException
{
long value = super.skip(n);
readNBytes(value);
return value;
}
and my readNBytes just does this...
Code:
private void readNBytes(long n)
{
for(int i=0; i<theFileInputProgressListeners.size(); i++)
{
((FileInputProgressListener)theFileInputProgressListeners.get(i)).bytesLoaded(n);
}
}
oh and finally my FileInputProgressListener interface is...
Code:
public abstract interface FileInputProgressListener
{
public void bytesLoaded(long number);
}
Any idea what i'm doing wrong?
-
Looking at your code i can't see a reason why you
would be getting an incorrect reading.
The bytes read should match what the
public long length() method is returning.
What about where you are reading? Maybe the
problem is there. :confused: :p
-
I've discovered what the problem was. My class' had changed since i last serilised them so it was getting 1/2 way through, realised they didn't match and threw an exception!
Doh! Such an easy mistake!
-