Example usage for java.nio.file Files createTempDirectory

List of usage examples for java.nio.file Files createTempDirectory

Introduction

In this page you can find the example usage for java.nio.file Files createTempDirectory.

Prototype

public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException 

Source Link

Document

Creates a new directory in the default temporary-file directory, using the given prefix to generate its name.

Usage

From source file:nl.salp.warcraft4j.config.PropertyWarcraft4jConfigTest.java

@BeforeClass
public static void setUpClass() throws Exception {
    testDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "w4jConfigTests");

    wowDir = testDir.resolve("wow");
    Files.createDirectory(wowDir);

    cacheDir = testDir.resolve("cache");
    Files.createDirectory(cacheDir);
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * This method will extract the appropriate Modbus master tool into the
 * temp folder so that it can be used later
 *
 * @return The temporary location of the Modbus master tool.
 * @throws Exception//from w w  w .j a  v  a  2s. c o  m
 */
public static File loadModPollTool() throws Exception {

    // Load the resource from the library

    String osName = System.getProperty("os.name");

    // Work out the correct name

    String exeName;
    if (osName.matches("(?is)windows.*")) {
        osName = "win32";
        exeName = "modpoll.exe";
    } else {
        osName = "linux";
        exeName = "modpoll";
    }

    // Copy the native modpoll library to a temporary directory in the build workspace to facilitate
    // execution on some platforms.
    File tmpDir = Files.createTempDirectory(Paths.get("."), "modpoll-").toFile();
    tmpDir.deleteOnExit();

    File nativeFile = new File(tmpDir, exeName);

    // Copy the library to the temporary folder

    InputStream in = null;
    String resourceName = String.format("/com/ghgande/j2mod/modbus/native/%s/%s", osName, exeName);

    try {
        in = SerialPort.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new Exception(String.format("Cannot find resource [%s]", resourceName));
        }
        pipeInputToOutputStream(in, nativeFile, false);
        nativeFile.deleteOnExit();

        // Set the correct privileges

        if (!nativeFile.setWritable(true, true)) {
            logger.warn("Cannot set modpoll native library to be writable");
        }
        if (!nativeFile.setReadable(true, false)) {
            logger.warn("Cannot set modpoll native library to be readable");
        }
        if (!nativeFile.setExecutable(true, false)) {
            logger.warn("Cannot set modpoll native library to be executable");
        }
    } catch (Exception e) {
        throw new Exception(
                String.format("Cannot locate modpoll native library [%s] - %s", exeName, e.getMessage()));
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error("Cannot close stream - {}", e.getMessage());
            }
        }
    }

    return nativeFile;
}

From source file:com.spectralogic.ds3client.helpers.FileObjectGetter_Test.java

@Test
public void testThatNamedPipeThrows() throws IOException, InterruptedException {
    Assume.assumeFalse(Platform.isWindows());

    final String tempPathPrefix = null;
    final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

    final String FIFO_NAME = "bFifo";

    final AtomicBoolean caughtException = new AtomicBoolean(false);

    try {//ww w  .j  a va2  s .  co  m
        Runtime.getRuntime().exec("mkfifo " + Paths.get(tempDirectory.toString(), FIFO_NAME)).waitFor();
        new FileObjectGetter(tempDirectory).buildChannel(FIFO_NAME);
    } catch (final UnrecoverableIOException e) {
        assertTrue(e.getMessage().contains(FIFO_NAME));
        caughtException.set(true);
    } finally {
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }

    assertTrue(caughtException.get());
}

From source file:com.olacabs.fabric.compute.builder.impl.HttpJarDownloader.java

public HttpJarDownloader() throws Exception {
    FileAttribute<Set<PosixFilePermission>> perms = PosixFilePermissions
            .asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x"));
    Path createdPath = Files.createTempDirectory(null, perms);
    this.tmpDirectory = createdPath.toAbsolutePath().toString();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(20);/*w w  w.jav a2  s.com*/

    httpClient = HttpClients.custom().setConnectionManager(cm).build();
}

From source file:org.apache.tika.batch.fs.FSBatchTestBase.java

@BeforeClass
public static void setUp() throws Exception {
    Path testOutput = Paths.get("target/test-classes/test-output");
    Files.createDirectories(testOutput);
    outputRoot = Files.createTempDirectory(testOutput, "tika-batch-output-root-");
}

From source file:com.spectralogic.ds3client.metadata.MetadataAccessImpl_Test.java

@Test
public void testGettingMetadata() throws IOException, InterruptedException {
    final String tempPathPrefix = null;
    final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

    final String fileName = "Gracie.txt";

    try {//w w  w .  jav a2 s .c o m
        final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));

        if (!Platform.isWindows()) {
            final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class);
            final Set<PosixFilePermission> permissions = attributes.permissions();
            permissions.clear();
            permissions.add(PosixFilePermission.OWNER_READ);
            permissions.add(PosixFilePermission.OWNER_WRITE);
            Files.setPosixFilePermissions(filePath, permissions);
        }

        final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
        fileMapper.put(filePath.toString(), filePath);
        final Map<String, String> metadata = new MetadataAccessImpl(fileMapper.build())
                .getMetadataValue(filePath.toString());

        if (Platform.isWindows()) {
            assertEquals(metadata.size(), 13);
        } else {
            assertEquals(metadata.size(), 10);
        }

        if (Platform.isMac()) {
            assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS)
                    .toLowerCase().startsWith(Platform.MAC_SYSTEM_NAME));
        } else if (Platform.isLinux()) {
            assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS)
                    .toLowerCase().startsWith(Platform.LINUX_SYSTEM_NAME));
        } else if (Platform.isWindows()) {
            assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS)
                    .toLowerCase().startsWith(Platform.WINDOWS_SYSTEM_NAME));
        }

        if (Platform.isWindows()) {
            assertEquals("A",
                    metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_FLAGS));
        } else {
            assertEquals("100600",
                    metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_MODE));
            assertEquals("600(rw-------)",
                    metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_PERMISSION));
        }
    } finally {
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.neustar.data.NeustarDatabaseUpdater.java

private File createTmpDir(File directory) {
    try {/* w  w  w  . j a  v  a  2 s  .  c om*/
        return Files.createTempDirectory(directory.toPath(), "neustar-").toFile();
    } catch (IOException e) {
        System.out.println("Failed to create temporary directory in " + directory.getAbsolutePath() + ": "
                + e.getMessage());
    }

    return null;
}

From source file:com.spectralogic.ds3client.metadata.MetadataReceivedListenerImpl_Test.java

@Test
public void testGettingMetadata() throws IOException, InterruptedException {
    final String tempPathPrefix = null;
    final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

    final String fileName = "Gracie.txt";

    final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));

    try {/*from  www . java 2  s .c o  m*/
        // set permissions
        if (!Platform.isWindows()) {
            final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class);
            final Set<PosixFilePermission> permissions = attributes.permissions();
            permissions.clear();
            permissions.add(PosixFilePermission.OWNER_READ);
            permissions.add(PosixFilePermission.OWNER_WRITE);
            Files.setPosixFilePermissions(filePath, permissions);
        }

        // get permissions
        final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
        fileMapper.put(filePath.toString(), filePath);
        final ImmutableMap<String, Path> immutableFileMapper = fileMapper.build();
        final Map<String, String> metadataFromFile = new MetadataAccessImpl(immutableFileMapper)
                .getMetadataValue(filePath.toString());

        // change permissions
        if (Platform.isWindows()) {
            Runtime.getRuntime().exec("attrib -A " + filePath.toString()).waitFor();
        } else {
            final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class);
            final Set<PosixFilePermission> permissions = attributes.permissions();
            permissions.clear();
            permissions.add(PosixFilePermission.OWNER_READ);
            permissions.add(PosixFilePermission.OWNER_WRITE);
            permissions.add(PosixFilePermission.OWNER_EXECUTE);
            Files.setPosixFilePermissions(filePath, permissions);
        }

        // put old permissions back
        final Metadata metadata = new MetadataImpl(new MockedHeadersReturningKeys(metadataFromFile));

        new MetadataReceivedListenerImpl(tempDirectory.toString()).metadataReceived(fileName, metadata);

        // see that we put back the original metadata
        fileMapper.put(filePath.toString(), filePath);
        final Map<String, String> metadataFromUpdatedFile = new MetadataAccessImpl(immutableFileMapper)
                .getMetadataValue(filePath.toString());

        if (Platform.isWindows()) {
            assertEquals("A", metadataFromUpdatedFile
                    .get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_FLAGS));
        } else {
            assertEquals("100600", metadataFromUpdatedFile
                    .get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_MODE));
            assertEquals("600(rw-------)", metadataFromUpdatedFile
                    .get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_PERMISSION));
        }

    } finally {
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }
}

From source file:objective.taskboard.followup.FollowUpTemplateStorage.java

@Override
public String storeTemplate(InputStream stream, FollowUpTemplateValidator validator) throws IOException {
    if (!getTemplateRoot().toFile().exists())
        Files.createDirectories(getTemplateRoot());

    Path pathFollowup = Files.createTempDirectory(getTemplateRoot(), "Followup");
    Path tempFolder = pathFollowup.resolve("temp");
    unzip(stream, tempFolder);//ww  w .  j  av  a 2s .  c om
    try {
        validator.validate(tempFolder);
        zip(tempFolder, pathFollowup.resolve("Followup-template.xlsm"));
        deleteQuietly(tempFolder.toFile());
    } catch (Exception e) {
        deleteQuietly(pathFollowup.toFile());
        throw e;
    }
    return getTemplateRoot().relativize(pathFollowup).toString();
}

From source file:at.ac.tuwien.infosys.util.ImageUtil.java

@PostConstruct
public void init() throws IOException {
    Path repo = Paths.get(workingRepo);
    Files.createDirectories(repo);
    this.imageTempDir = Files.createTempDirectory(Paths.get(repo.toString()), "image-");
    logger.info("Using temporary-folder: " + imageTempDir);
}