/** * Creates a new file within the state location of this plugin and writes the given content to it. * If the file already exists, it is overwritten.<br> * The naming scheme for the temporary file is "<em>baseName</em>.<em>type</em>". * * @param fileName filename for the temporary file, must not contain any path segments, never * null. * @param content the content to write to the file, never null. * @return <em>true</em> on success, <em>false</em> on failure. */ public static boolean storeTemporaryFile(String fileName, String type, String content) { if (log.isTraceEnabled()) log.trace( "storeTemporaryFile() - fileName: " + fileName + ", type:" + type + ", content: " + CoreStringUtils.truncateString(content)); assert (fileName != null && content != null && type != null); IPath stateLoc = CPCStoreRemoteLMIPlugin.getDefault().getStateLocation().makeAbsolute(); stateLoc.append("temp"); File tmpFile = new File(stateLoc.toString(), fileName + "." + type); try { BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile)); bw.write(content); bw.close(); } catch (IOException e) { log.error( "storeTemporaryFile() - unable to write file - file: " + tmpFile + ", content: " + CoreStringUtils.truncateString(content) + " - " + e, e); return false; } return true; }
/** * Reads the content of a temporary file within the state location of this plugin.<br> * The naming scheme for the temporary file is "<em>baseName</em>.<em>type</em>". * * @param fileName filename of the temporary file, must not contain any path segments, never null. * @return the content of the temporary file or NULL if the file doesn't exist or couldn't be * read. */ public static String readTemporaryFile(String fileName, String type) { if (log.isTraceEnabled()) log.trace("readTemporaryFile() - fileName: " + fileName + ", type:" + type); assert (fileName != null && type != null); IPath stateLoc = CPCStoreRemoteLMIPlugin.getDefault().getStateLocation().makeAbsolute(); stateLoc.append("temp"); File tmpFile = new File(stateLoc.toString(), fileName + "." + type); String result = null; if (tmpFile.exists()) { if (tmpFile.length() == 0) { result = ""; } else { try { char[] buf = new char[(int) tmpFile.length()]; BufferedReader br = new BufferedReader(new FileReader(tmpFile)); int read = br.read(buf); br.close(); result = new String(buf, 0, read); } catch (IOException e) { log.error("readTemporaryFile() - unable to read file - file: " + tmpFile + " - " + e, e); result = null; } } } if (log.isTraceEnabled()) log.trace("readTemporaryFile() - result: " + CoreStringUtils.truncateString(result)); return result; }