/** * Creates the lock file. * * @throws IOException if we cannot create the file */ private void createLock() throws IOException { synchronized (LockableFileWriter.class) { if (!lockFile.createNewFile()) { throw new IOException("Can't write file, lock " + lockFile.getAbsolutePath() + " exists"); } lockFile.deleteOnExit(); } }
public static File createTestFile(File testFolder, String path) throws Exception { File subfile = new File(testFolder, path); subfile.createNewFile(); subfile.deleteOnExit(); return subfile; }
public static File createTempFile(String suffix) { File temp; try { temp = File.createTempFile("jadx-tmp-", System.nanoTime() + "-" + suffix); temp.deleteOnExit(); } catch (IOException e) { throw new JadxRuntimeException("Failed to create temp file with suffix: " + suffix); } return temp; }
private static File copyResourceToFile(String resourcePath) throws IOException { if (tempDir == null){ File tempFile = File.createTempFile("prefix", "suffix"); tempFile.deleteOnExit(); tempDir = tempFile.getParentFile(); } InputStream jarIn = SdkStore.class.getClassLoader().getResourceAsStream(resourcePath); File outFile = new File(tempDir, new File(resourcePath).getName()); outFile.deleteOnExit(); try (FileOutputStream jarOut = new FileOutputStream(outFile)) { byte[] buffer = new byte[4096]; int len; while ((len = jarIn.read(buffer)) != -1) { jarOut.write(buffer, 0, len); } } return outFile; }
private static File createFileInTmp(String prefix, String suffix, String reason, boolean isDirectory) throws IOException { File file = File.createTempFile(prefix, suffix); if (isDirectory && (!file.delete() || !file.mkdirs())) { // TODO: or we could just generate a name ourselves and not do this? throw new IOException("Cannot recreate " + file + " as directory"); } file.deleteOnExit(); LOG.info(reason + "; created a tmp file: " + file.getAbsolutePath()); return file; }
public static File tmpDir() throws IOException { String tmpDirStr = System.getProperty(TmpDirTestCase.TEST_TMP_SYSTEM_PROPERTY_NAME); tmpDirStr = (tmpDirStr == null)? TmpDirTestCase.DEFAULT_TEST_TMP_DIR: tmpDirStr; File tmpDir = new File(tmpDirStr); FileUtils.ensureWriteableDirectory(tmpDir); if (!tmpDir.canWrite()) { throw new IOException(tmpDir.getAbsolutePath() + " is unwriteable."); } tmpDir.deleteOnExit(); return tmpDir; }
File tmp = new File(rootDir.getParentFile(),'.'+rootDir.getName()); if (tmp.exists()) { Util.deleteRecursive(tmp); if(tmp.exists()) tmp.deleteOnExit();
public static boolean assembleSmaliFile(InputStream is,DexBuilder dexBuilder, boolean verboseErrors, boolean printTokens, File smaliFile) throws IOException, RecognitionException { // copy our filestream into a tmp file, so we don't overwrite File tmp = File.createTempFile("BRUT",".bak"); tmp.deleteOnExit(); OutputStream os = new FileOutputStream(tmp); IOUtils.copy(is, os); os.close(); return assembleSmaliFile(tmp,dexBuilder, verboseErrors, printTokens); }
@Test(groups = {LDAP, LDAP_CLI, PROFILE_SPECIFIC_TESTS}, timeOut = TIMEOUT) public void shouldRunQueryFromFileWithLdap() throws IOException { File temporayFile = File.createTempFile("test-sql", null); temporayFile.deleteOnExit(); Files.write("select * from hive.default.nation;\n", temporayFile, UTF_8); launchPrestoCliWithServerArgument("--file", temporayFile.getAbsolutePath()); assertThat(trimLines(presto.readRemainingOutputLines())).containsAll(nationTableBatchLines); }
public void doLoad() { if (loaded) { return; } final long startTime = System.currentTimeMillis(); File tempDir = Files.createTempDir(); tempDir.deleteOnExit(); File extractedLibraryPath = new File(tempDir, getLibName()); try (FileOutputStream outputStream = new FileOutputStream(extractedLibraryPath)) { getLibraryByteSource().copyTo(outputStream); } catch (IOException e) { throw new RuntimeException("Cannot extract SQLite library into " + extractedLibraryPath, e); } loadFromDirectory(tempDir); logWithTime("SQLite natives prepared in", startTime); }
public static File resourceAsFile(String path) throws URISyntaxException, IOException { ClassLoader cl = TestUtils.class.getClassLoader(); URI uri = cl.getResource(path).toURI(); if (uri.isAbsolute() && !uri.isOpaque()) { return new File(uri); } else { File tmpFile = File.createTempFile("tmpfile-", ".data", TMP_DIR); tmpFile.deleteOnExit(); try (InputStream is = cl.getResourceAsStream(path)) { FileUtils.copyInputStreamToFile(is, tmpFile); return tmpFile; } } }
private void setupOutputFileStreams() throws IOException { parentDir = FileUtils.createLocalDirsTempFile(spillLocalDirs, "bytes-container", "", true); parentDir.deleteOnExit(); tmpFile = File.createTempFile("BytesContainer", ".tmp", parentDir); LOG.debug("BytesContainer created temp file " + tmpFile.getAbsolutePath()); tmpFile.deleteOnExit(); fileOutputStream = new FileOutputStream(tmpFile); }
/** * {@inheritDoc} */ public File inject(File jar) throws IOException { File temporary = doInject(jar, File.createTempFile(jar.getName(), TEMP_SUFFIX)); boolean delete = true; try { delete = DISPATCHER.copy(temporary, jar); } finally { if (delete && !temporary.delete()) { temporary.deleteOnExit(); } } return jar; }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
public static final boolean safeDeleteFile(File file) { if (file == null) { return true; } if (file.exists()) { Log.i(TAG, "safeDeleteFile, try to delete path: " + file.getPath()); boolean deleted = file.delete(); if (!deleted) { Log.e(TAG, "Failed to delete file, try to delete when exit. path: " + file.getPath()); file.deleteOnExit(); } return deleted; } return true; }
private boolean delete(File file) { boolean deleted = file.delete(); if (!deleted) { file.deleteOnExit(); logError("Could not delete " + file.getAbsolutePath()); } return deleted; }
private void createTempFiles(byte[] contents, File... files) throws IOException { for (File child : files) { child.deleteOnExit(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(child)); try { out.write(contents); } finally { out.close(); } } }
@After public void tearDown() { if (!testFile.delete()) { testFile.deleteOnExit(); } }
/** * Get ivy settings file from classpath */ public static File getIvySettingsFile() throws IOException { URL settingsUrl = Thread.currentThread().getContextClassLoader().getResource(IVY_SETTINGS_FILE_NAME); if (settingsUrl == null) { throw new IOException("Failed to find " + IVY_SETTINGS_FILE_NAME + " from class path"); } // Check if settingsUrl is file on classpath File ivySettingsFile = new File(settingsUrl.getFile()); if (ivySettingsFile.exists()) { // can access settingsUrl as a file return ivySettingsFile; } // Create temporary Ivy settings file. ivySettingsFile = File.createTempFile("ivy.settings", ".xml"); ivySettingsFile.deleteOnExit(); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(ivySettingsFile))) { Resources.copy(settingsUrl, os); } return ivySettingsFile; } }
public List<ScoredObject<Tree>> getKBestParses(List<? extends HasWord> sentence, int k, boolean deleteTempFiles) { try { File inFile = File.createTempFile("charniak.", ".in"); if (deleteTempFiles) inFile.deleteOnExit(); File outFile = File.createTempFile("charniak.", ".out"); if (deleteTempFiles) outFile.deleteOnExit(); File errFile = File.createTempFile("charniak.", ".err"); if (deleteTempFiles) errFile.deleteOnExit(); printSentence(sentence, inFile.getAbsolutePath()); runCharniak(k, inFile.getAbsolutePath(), outFile.getAbsolutePath(), errFile.getAbsolutePath()); Iterable<List<ScoredObject<Tree>>> iter = CharniakScoredParsesReaderWriter.readScoredTrees(outFile.getAbsolutePath()); if (deleteTempFiles) { inFile.delete(); outFile.delete(); errFile.delete(); } return iter.iterator().next(); } catch (IOException ex) { throw new RuntimeException(ex); } }