package com.cokere.util; /* this class can be used to work around Bug 4724038 in Java, which they have no intention of fixing see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 for information. basically this The only way to unmap a byte buffer in java is wait for it to be garbage collected. And the only way to 'wait' for something to become garbage collected is to sleep in a loop untill it's done. This class does that. Waits in a loop (for up to a bit more then a minute, otherwise it throws an exception) untill it's possible to do what you wanted to do, but couldn't, because the file was mapped into a MappedByteBuffer. To use it, you basically do this: //remove all refrences to MappedByteBuffer (i.e. mybuffer = null); com.cokere.util.Bug4724038.workaround(new com.cokere.util.Bug4724038.FileOp(){ public void op()throws java.io.IOException{ //do whatever. Delete, resize, etc; } }); --Chad Okere. */ public class Bug4724038 { public static void workaround(FileOp f) throws java.io.IOException{ long sleep = 1; long maxsleep = 1024*64;//A binary minute :) boolean success = false; while(!success){ try{ f.op(); success = true; } catch(java.io.IOException e){ try{ Thread.sleep(sleep); } catch(Exception ex){} if(sleep > maxsleep){ throw e; } sleep *= 2; System.gc(); System.runFinalization(); } } } public static abstract class FileOp{ public abstract void op() throws java.io.IOException; } }