Author: toad
Date: 2008-11-06 20:31:37 +0000 (Thu, 06 Nov 2008)
New Revision: 23365

Modified:
   trunk/freenet/src/freenet/support/io/TempBucketFactory.java
Log:
Fix fd leak: Don't leave a stream open in migrateToFileBucket() unless we 
actually need one to be open.
Null out os when finished so we know whether we need a stream. Keep a flag to 
know whether we've had a stream, so we can throw in getInputStream().


Modified: trunk/freenet/src/freenet/support/io/TempBucketFactory.java
===================================================================
--- trunk/freenet/src/freenet/support/io/TempBucketFactory.java 2008-11-06 
19:59:16 UTC (rev 23364)
+++ trunk/freenet/src/freenet/support/io/TempBucketFactory.java 2008-11-06 
20:31:37 UTC (rev 23365)
@@ -65,6 +65,8 @@
                private Bucket currentBucket;
                /** We have to account the size of the underlying bucket 
ourself in order to be able to access it fast */
                private long currentSize;
+               /** Has an OutputStream been opened at some point? */
+               private boolean hasWritten;
                /** A link to the "real" underlying outputStream, even if we 
migrated */
                private OutputStream os = null;
                /** All the open-streams to reset or close on migration or 
free() */
@@ -114,6 +116,12 @@
                                        os = tempFB.getOutputStream();
                                        if(currentSize > 0)
                                                BucketTools.copyTo(toMigrate, 
os, currentSize);
+                               } else {
+                                       if(currentSize > 0) {
+                                               OutputStream temp = 
tempFB.getOutputStream();
+                                               BucketTools.copyTo(toMigrate, 
temp, currentSize);
+                                               temp.close();
+                                       }
                                }
                                if(toMigrate.isReadOnly())
                                        tempFB.setReadOnly();
@@ -209,13 +217,13 @@
                                        _maybeMigrateRamBucket(currentSize);
                                        os.flush();
                                        os.close();
-                                       // DO NOT NULL os OUT HERE!
+                                       os = null;
                                }
                        }
                }

                public synchronized InputStream getInputStream() throws 
IOException {
-                       if(os == null)
+                       if(!hasWritten)
                                throw new IOException("No OutputStream has been 
openned! Why would you want an InputStream then?");
                        TempBucketInputStream is = new 
TempBucketInputStream(osIndex);
                        tbis.add(is);


Reply via email to