Java Code Examples for java.nio.file.attribute.FileAttribute

The following examples show how to use java.nio.file.attribute.FileAttribute. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: presto   Source File: Standard.java    License: Apache License 2.0 6 votes vote down vote up
public static void enablePrestoJavaDebugger(DockerContainer container, int debugPort)
{
    try {
        FileAttribute<Set<PosixFilePermission>> rwx = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxrwxrwx"));
        Path script = Files.createTempFile("enable-java-debugger", ".sh", rwx);
        Files.writeString(
                script,
                format(
                        "#!/bin/bash\n" +
                                "printf '%%s\\n' '%s' >> '%s'\n",
                        "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:" + debugPort,
                        CONTAINER_PRESTO_JVM_CONFIG),
                UTF_8);
        container.withCopyFileToContainer(MountableFile.forHostPath(script), "/docker/presto-init.d/enable-java-debugger.sh");

        // expose debug port unconditionally when debug is enabled
        exposePort(container, debugPort);
    }
    catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 2
Source Project: aion   Source File: Aion.java    License: MIT License 6 votes vote down vote up
private static void writeKeyToFile(final String path, final String fileName, final String key)
        throws IOException {
    Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-----");
    FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);

    Path p = Paths.get(path).resolve(fileName);
    Path keyFile;
    if (!java.nio.file.Files.exists(p)) {
        keyFile = java.nio.file.Files.createFile(p, attr);
    } else {
        keyFile = p;
    }

    FileOutputStream fos = new FileOutputStream(keyFile.toString());
    fos.write(key.getBytes());
    fos.close();
}
 
Example 3
private File flushPropertiesToTempFile(Map<String, Object> configProps) throws IOException {
  FileAttribute<Set<PosixFilePermission>> attributes
      = PosixFilePermissions.asFileAttribute(new HashSet<>(
          Arrays.asList(PosixFilePermission.OWNER_WRITE,
                        PosixFilePermission.OWNER_READ)));
  File configFile = Files.createTempFile("ksqlclient", "properties", attributes).toFile();
  configFile.deleteOnExit();

  try (FileOutputStream outputStream = new FileOutputStream(configFile)) {
    Properties clientProps = new Properties();
    for (Map.Entry<String, Object> property
        : configProps.entrySet()) {
      clientProps.put(property.getKey(), property.getValue());
    }
    clientProps.store(outputStream, "Configuration properties of KSQL AdminClient");
  }
  return configFile;
}
 
Example 4
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(target, "createSymbolicLink");
    Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
 
Example 5
Source Project: incubator-tuweni   Source File: Files.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a file, if it does not already exist.
 *
 * @param path The path to the file to create.
 * @param attrs An optional list of file attributes to set atomically when creating the file.
 * @return {@code true} if the file was created.
 * @throws IOException If an I/O error occurs or the parent directory does not exist.
 */
public static boolean createFileIfMissing(Path path, FileAttribute<?>... attrs) throws IOException {
  requireNonNull(path);
  try {
    java.nio.file.Files.createFile(path, attrs);
  } catch (FileAlreadyExistsException e) {
    return false;
  }
  return true;
}
 
Example 6
Source Project: metastore   Source File: LocalFileStorage.java    License: Apache License 2.0 5 votes vote down vote up
private void init(RegistryInfo registryInfo, Map<String, String> config) {
  this.registryName = registryInfo.getName();
  this.path = config.get("path");
  if (path == null) {
    throw new RuntimeException("path variable not set");
  }
  if (!new File(path).isDirectory()) {
    try {
      Files.createDirectories(new File(path).toPath(), new FileAttribute[] {});
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
 
Example 7
Source Project: jdk1.8-source-analysis   Source File: TempFileHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example 8
Source Project: paintera   Source File: TmpDirectoryCreator.java    License: GNU General Public License v2.0 5 votes vote down vote up
public TmpDirectoryCreator(final Supplier<Path> dir, final String prefix, final FileAttribute<?>... attrs)
{
	super();
	LOG.debug("Creating {} with dir={} prefix={} attrs={}", this.getClass().getSimpleName(), dir, prefix, attrs);
	this.dir = dir;
	this.prefix = prefix;
	this.attrs = attrs;
}
 
Example 9
Source Project: openjdk-jdk8u   Source File: TempFileHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a temporary directory in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempDirectory(Path dir,
                                String prefix,
                                FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, null, true, attrs);
}
 
Example 10
Source Project: jdk1.8-source-analysis   Source File: Files.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Used by createDirectories to attempt to create a directory. A no-op
 * if the directory already exists.
 */
private static void createAndCheckIsDirectory(Path dir,
                                              FileAttribute<?>... attrs)
    throws IOException
{
    try {
        createDirectory(dir, attrs);
    } catch (FileAlreadyExistsException x) {
        if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
            throw x;
    }
}
 
Example 11
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example 12
/**
 * Creates a temporary directory in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempDirectory(Path dir,
                                String prefix,
                                FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, null, true, attrs);
}
 
Example 13
Source Project: dragonwell8_jdk   Source File: Files.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Used by createDirectories to attempt to create a directory. A no-op
 * if the directory already exists.
 */
private static void createAndCheckIsDirectory(Path dir,
                                              FileAttribute<?>... attrs)
    throws IOException
{
    try {
        createDirectory(dir, attrs);
    } catch (FileAlreadyExistsException x) {
        if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
            throw x;
    }
}
 
Example 14
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(target, "createSymbolicLink");
    Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
 
Example 15
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(dir, "createDirectory");
    Files.createDirectory(unwrap(dir), attrs);
}
 
Example 16
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example 17
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example 18
Source Project: TencentKona-8   Source File: TempFileHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example 19
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(target, "createSymbolicLink");
    Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
 
Example 20
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(dir, "createDirectory");
    Files.createDirectory(unwrap(dir), attrs);
}
 
Example 21
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example 22
Source Project: TencentKona-8   Source File: CustomOptions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example 23
Source Project: jdk8u60   Source File: TempFileHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example 24
Source Project: jdk8u60   Source File: TempFileHelper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a temporary directory in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempDirectory(Path dir,
                                String prefix,
                                FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, null, true, attrs);
}
 
Example 25
Source Project: jdk8u60   Source File: Files.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Used by createDirectories to attempt to create a directory. A no-op
 * if the directory already exists.
 */
private static void createAndCheckIsDirectory(Path dir,
                                              FileAttribute<?>... attrs)
    throws IOException
{
    try {
        createDirectory(dir, attrs);
    } catch (FileAlreadyExistsException x) {
        if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
            throw x;
    }
}
 
Example 26
Source Project: openjdk-jdk8u   Source File: CustomOptions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example 27
Source Project: jdk8u60   Source File: FaultyFileSystem.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example 28
Source Project: embedded-cassandra   Source File: FileUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new and empty file, if the file does not exist.
 *
 * @param file the path to the file to create
 * @param attributes an optional list of file attributes to set atomically when creating the file
 * @return the file
 * @throws IOException if an I/O error occurs or the parent directory does not exist
 * @see Files#createFile(Path, FileAttribute[])
 */
public static Path createIfNotExists(Path file, FileAttribute<?>... attributes) throws IOException {
	Objects.requireNonNull(file, "'file' must not be null");
	Objects.requireNonNull(attributes, "'attributes' must not be null");
	try {
		return Files.createFile(file, attributes);
	}
	catch (FileAlreadyExistsException ex) {
		return file;
	}
}
 
Example 29
Source Project: cava   Source File: Files.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a file, if it does not already exist.
 *
 * @param path The path to the file to create.
 * @param attrs An optional list of file attributes to set atomically when creating the file.
 * @return {@code true} if the file was created.
 * @throws IOException If an I/O error occurs or the parent directory does not exist.
 */
public static boolean createFileIfMissing(Path path, FileAttribute<?>... attrs) throws IOException {
  requireNonNull(path);
  try {
    java.nio.file.Files.createFile(path, attrs);
  } catch (FileAlreadyExistsException e) {
    return false;
  }
  return true;
}
 
Example 30
Source Project: JDKSourceCode1.8   Source File: TempFileHelper.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}