Add delay-retry loop for handling temporarily locked image file writes

This commit is contained in:
Mike Primm 2011-06-20 16:58:07 -05:00
parent 6efbf3a3df
commit e584e3202b
3 changed files with 34 additions and 5 deletions

View file

@ -1,6 +1,10 @@
package org.dynmap.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.dynmap.Log;
/**
@ -102,4 +106,29 @@ public class FileLockManager {
}
//Log.info("releaseReadLock(" + f + ")");
}
private static final int MAX_WRITE_RETRIES = 6;
/**
* Wrapper for IOImage.write - implements retries for busy files
*/
public static void imageIOWrite(BufferedImage img, String type, File fname) throws IOException {
int retrycnt = 0;
boolean done = false;
while(!done) {
try {
ImageIO.write(img, type, fname);
done = true;
} catch (FileNotFoundException fnfx) { /* This seems to be what we get when file is locked by reader */
if(retrycnt < MAX_WRITE_RETRIES) {
Log.info("Image file " + fname.getPath() + " - unable to write - retry #" + retrycnt);
try { Thread.sleep(50 << retrycnt); } catch (InterruptedException ix) { throw fnfx; }
retrycnt++;
}
else {
Log.info("Image file " + fname.getPath() + " - unable to write - failed");
throw fnfx;
}
}
}
}
}