private OverthereFile createConnectionTemporaryDirectory() { OverthereFile temporaryDirectory = getFile(temporaryDirectoryPath); Random r = new Random(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS"); String prefix = "overthere-" + dateFormat.format(new Date()); String infix = ""; String suffix = ".tmp"; for (int i = 0; i < temporaryFileCreationRetries; i++) { OverthereFile tempDir = getFileForTempFile(temporaryDirectory, prefix + infix + suffix); if (!tempDir.exists()) { tempDir.mkdir(); logger.info("Created connection temporary directory {}", tempDir); return tempDir; } infix = "-" + Long.toString(Math.abs(r.nextLong())); } throw new RuntimeIOException("Cannot create connection temporary directory on " + this); }
/** * Creates a reference to a temporary file on the host. This file has a unique name and will be * automatically removed when this connection is closed. <b>N.B.:</b> The file is not actually * created until a put method is invoked. * * @param prefix the prefix string to be used in generating the file's name; must be at least * three characters long * @param suffix the suffix string to be used in generating the file's name; may be <code>null * </code>, in which case the suffix ".tmp" will be used * @return a reference to the temporary file on the host */ @Override public final OverthereFile getTempFile(String prefix, String suffix) throws RuntimeIOException { if (prefix == null) throw new NullPointerException("prefix is null"); if (suffix == null) { suffix = ".tmp"; } Random r = new Random(); String infix = ""; for (int i = 0; i < temporaryFileCreationRetries; i++) { OverthereFile f = getFileForTempFile(getConnectionTemporaryDirectory(), prefix + infix + suffix); if (!f.exists()) { logger.debug("Created temporary file {}", f); return f; } infix = "-" + Long.toString(Math.abs(r.nextLong())); } throw new RuntimeIOException("Cannot generate a unique temporary file name on " + this); }
private void deleteConnectionTemporaryDirectory() { if (connectionTemporaryDirectory != null) { try { logger.info("Deleting connection temporary directory {}", connectionTemporaryDirectory); connectionTemporaryDirectory.deleteRecursively(); } catch (RuntimeException exc) { logger.warn( "Got exception while deleting connection temporary directory {}. Ignoring it.", connectionTemporaryDirectory, exc); } } }