Results 1 to 4 of 4

Thread: FileInputStream

  1. #1

    Thread Starter
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144

    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)
        {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(int i=0; i<theFileInputProgressListeners.size(); i++)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((FileInputProgressListener)theFileInputProgressListeners.get(i)).bytesLoaded(n);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;}
    oh and finally my FileInputProgressListener interface is...

    Code:
    public abstract interface FileInputProgressListener
    {
    &nbsp;&nbsp;&nbsp;&nbsp;public void bytesLoaded(long number);
    }
    Any idea what i'm doing wrong?
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  3. #3

    Thread Starter
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    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!
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width