1 19 package org.java.plugin.tools; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.java.plugin.util.IoUtil; 30 31 34 final class Util { 35 private static Log log = LogFactory.getLog(Util.class); 36 37 private static File tempFolder; 38 private static boolean tempFolderInitialized = false; 39 40 static File getTempFolder() throws IOException { 41 if (tempFolder != null) { 42 return tempFolderInitialized ? tempFolder : null; 43 } 44 synchronized (Util.class) { 45 tempFolder = new File (System.getProperty("java.io.tmpdir"), System.currentTimeMillis() + ".jpf-tool-cache"); log.debug("libraries cache folder is " + tempFolder); File lockFile = new File (tempFolder, "lock"); if (lockFile.exists()) { 50 throw new IOException ("can't initialize temporary folder " + tempFolder + " as lock file indicates that it is " + "owned by another JPF instance"); } 54 if (tempFolder.exists()) { 55 IoUtil.emptyFolder(tempFolder); 57 } else { 58 tempFolder.mkdirs(); 59 } 60 if (!lockFile.createNewFile()) { 61 throw new IOException ("can\'t create lock file in JPF " + "tool temporary folder " + tempFolder); } 64 lockFile.deleteOnExit(); 65 tempFolder.deleteOnExit(); 66 tempFolderInitialized = true; 67 } 68 return tempFolder; 69 } 70 71 static byte[] readUrlContent(final URL url) throws IOException { 72 ByteArrayOutputStream result = new ByteArrayOutputStream (); 73 InputStream urlStrm = url.openStream(); 74 try { 75 IoUtil.copyStream(urlStrm, result, 256); 76 } finally { 77 urlStrm.close(); 78 } 79 return result.toByteArray(); 80 } 81 82 private Util() { 83 } 85 } 86 | Popular Tags |