#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    BufferedInputStream skip() method


    I am getting an exception when trying to use the skip() method, maybe my approach to this is wrong.

    The purpose of the following method is to find how big the file is for files that return 0 bytes because of the OS (windows) not properly reporting the size to java.

    Code:
        private int CalculateMediaSize(File file)
        {
            int chunks=0;
            long chunkSize = 1000000;
            try
            {
                BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
                while(true)
                {
                    long tmpSkip = br.skip(chunkSize);
                    int tmp = br.read();
                    if(tmp != -1)
                    {
                        chunks++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch(Exception ex){System.out.println(ex.getMessage() + "\r\n" + ex.toString());};
            return chunks;
        }
    Exception
    java.io.IOException: The parameter is incorrect
    The exception is definately happening when the br.skip() is called, if I take out that line, this method will read the file to the end with the br.read().

    Is there another way to accomplish the skip I'm looking for?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    86
    Rep Power
    11
    U might want to look into this
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0
    Originally Posted by Fruitbisqit
    U might want to look into this
    which part? if you don't mind
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    86
    Rep Power
    11
    The part it directs you to ;-) File.length()
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0
    Originally Posted by Fruitbisqit
    The part it directs you to ;-) File.length()
    I was assuming thats what you meant. If windows thinks that the file is 0 bytes (like when its a linux partition at UNC level "\\.\d:") then 0 is returned by all the easy methods.

    That's why I'm trying for an alternative approach to file length, sort of brute force.

    Thank you for the response though, wish this was that simple
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    86
    Rep Power
    11
    Well, in that case you can try looping read() from the FileInputStream until it returns -1 (wich means end of file) and count the times you looped. Or use a read with a buffer and do practicly the same but not per byte.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0
    Originally Posted by Fruitbisqit
    Well, in that case you can try looping read() from the FileInputStream until it returns -1 (wich means end of file) and count the times you looped. Or use a read with a buffer and do practicly the same but not per byte.
    This is an acceptable solution to the problem, I'll accept that and if people have similar issue they can use this. (I didn't give any big picture details so thats my fault).

    A zoom out to the bigger picture, I want to use this file size for progress when reading. I do a loop that you described later on but it can take around 10minutes per gb read, and no chance at providing progress since I don't know how big the file is. So I was hoping to spend around 5seconds counting how many mb's I can skip through in the file and keep progress that way. but that silly skip() method isn't working for me.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    86
    Rep Power
    11
    FileInputStream also provides a skip method (better even, bufferedinputstreams skip extends that method) might want to give that a try
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0
    Originally Posted by Fruitbisqit
    FileInputStream also provides a skip method (better even, bufferedinputstreams skip extends that method) might want to give that a try
    you didn't read my post at all did you lol, just the subject?

    actually, not even the subject hehe

IMN logo majestic logo threadwatch logo seochat tools logo