Author: toad
Date: 2008-04-05 13:30:29 +0000 (Sat, 05 Apr 2008)
New Revision: 19020

Modified:
   trunk/freenet/src/freenet/support/io/FileUtil.java
Log:
Implement skipFully and use it. Fixes #2227.

Modified: trunk/freenet/src/freenet/support/io/FileUtil.java
===================================================================
--- trunk/freenet/src/freenet/support/io/FileUtil.java  2008-04-05 13:25:41 UTC 
(rev 19019)
+++ trunk/freenet/src/freenet/support/io/FileUtil.java  2008-04-05 13:30:29 UTC 
(rev 19020)
@@ -92,7 +92,7 @@

                try {
                        fis = new FileInputStream(file);
-                       fis.skip(offset);
+                       skipFully(fis, offset);
                        bis = new BufferedInputStream(fis);
                        isr = new InputStreamReader(bis, "UTF-8");

@@ -112,7 +112,19 @@
                }
                return result.toString();
        }
-       
+
+       /**
+        * Reliably skip a number of bytes or throw.
+        */
+       public static void skipFully(InputStream is, long skip) throws 
IOException {
+               long skipped = 0;
+               while(skipped < skip) {
+                       long x = is.skip(skip - skipped);
+                       if(x <= 0) throw new IOException("Unable to skip 
"+(skip - skipped)+" bytes");
+                       skipped += x;
+               }
+       }
+
        public static boolean writeTo(InputStream input, File target) throws 
FileNotFoundException, IOException {
                DataInputStream dis = null;
                FileOutputStream fos = null;


Reply via email to