# SecureDirectoryStream

public interface **SecureDirectoryStream** implements [DirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/DirectoryStream.html)\<T\> A `DirectoryStream` that defines operations on files that are located
relative to an open directory. A `SecureDirectoryStream` is intended
for use by sophisticated or security sensitive applications requiring to
traverse file trees or otherwise operate on directories in a race-free manner.
Race conditions can arise when a sequence of file operations cannot be
carried out in isolation. Each of the file operations defined by this
interface specify a relative path. All access to the file is relative
to the open directory irrespective of if the directory is moved or replaced
by an attacker while the directory is open. A `SecureDirectoryStream`
may also be used as a virtual *working directory*.

A `SecureDirectoryStream` requires corresponding support from the
underlying operating system. Where an implementation supports this features
then the `DirectoryStream` returned by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#newDirectoryStream(java.nio.file.Path)` method will be a `SecureDirectoryStream` and must
be cast to that type in order to invoke the methods defined by this interface.

In the case of the default `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/spi/FileSystemProvider.html`, and a security manager is set, then the permission checks are
performed using the path obtained by resolving the given relative path
against the *original path* of the directory (irrespective of if the
directory is moved since it was opened).

### Public Method Summary

|---|---|
| abstract void | [deleteDirectory](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#deleteDirectory(T))(T path) Deletes a directory. |
| abstract void | [deleteFile](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#deleteFile(T))(T path) Deletes a file. |
| abstract \<V extends [FileAttributeView](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttributeView.html)\> V | [getFileAttributeView](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#getFileAttributeView(java.lang.Class<V>))([Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<V\> type) Returns a new file attribute view to access the file attributes of this directory. |
| abstract \<V extends [FileAttributeView](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttributeView.html)\> V | [getFileAttributeView](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#getFileAttributeView(T,%20java.lang.Class<V>,%20java.nio.file.LinkOption...))(T path, [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<V\> type, [LinkOption...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/LinkOption.html) options) Returns a new file attribute view to access the file attributes of a file in this directory. |
| abstract void | [move](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#move(T,%20java.nio.file.SecureDirectoryStream<T>,%20T))(T srcpath, [SecureDirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html)\<T\> targetdir, T targetpath) Move a file from this directory to another directory. |
| abstract [SeekableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SeekableByteChannel.html) | [newByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#newByteChannel(T,%20java.util.Set<?%20extends%20java.nio.file.OpenOption>,%20java.nio.file.attribute.FileAttribute<?>...))(T path, [Set](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html)\<? extends [OpenOption](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/OpenOption.html)\> options, [FileAttribute...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttribute.html)\<?\> attrs) Opens or creates a file in this directory, returning a seekable byte channel to access the file. |
| abstract [SecureDirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html)\<T\> | [newDirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html#newDirectoryStream(T,%20java.nio.file.LinkOption...))(T path, [LinkOption...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/LinkOption.html) options) Opens the directory identified by the given path, returning a `SecureDirectoryStream` to iterate over the entries in the directory. |

### Inherited Method Summary

From interface [java.nio.file.DirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/DirectoryStream.html)

|---|---|
| abstract [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<T\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/DirectoryStream.html#iterator())() Returns the iterator associated with this `DirectoryStream`. |

From interface [java.io.Closeable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |

From interface [java.lang.Iterable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html)

|---|---|
| void | [forEach](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html#forEach(java.util.function.Consumer<?%20super%20T>))([Consumer](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Consumer.html)\<? super T\> action) Performs the given action for each element of the `Iterable` until all elements have been processed or the action throws an exception. |
| abstract [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<T\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html#iterator())() Returns an iterator over elements of type `T`. |
| [Spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html)\<T\> | [spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html#spliterator())() Creates a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html` over the elements described by this `Iterable`. |

From interface [java.lang.AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |

## Public Methods

#### public abstract void
**deleteDirectory**
(T path)

Deletes a directory.

Unlike the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#delete(java.nio.file.Path)` method, this method
does not first examine the file to determine if the file is a directory.
Whether non-directories are deleted by this method is system dependent and
therefore not specified. When the parameter is a relative path then the
directory to delete is relative to this open directory.

##### Parameters

| path | the path of the directory to delete |
|---|---|

##### Throws

| [ClosedDirectoryStreamException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html) | if the directory stream is closed |
| [NoSuchFileException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/NoSuchFileException.html) | if the directory does not exist *(optional specific exception)* |
| [DirectoryNotEmptyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/DirectoryNotEmptyException.html) | if the directory could not otherwise be deleted because it is not empty *(optional specific exception)* |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | In the case of the default provider, and a security manager is installed, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkDelete(java.lang.String)` method is invoked to check delete access to the directory |
|---|---|

#### public abstract void
**deleteFile**
(T path)

Deletes a file.

Unlike the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#delete(java.nio.file.Path)` method, this method does
not first examine the file to determine if the file is a directory.
Whether a directory is deleted by this method is system dependent and
therefore not specified. If the file is a symbolic link, then the link
itself, not the final target of the link, is deleted. When the
parameter is a relative path then the file to delete is relative to
this open directory.

##### Parameters

| path | the path of the file to delete |
|---|---|

##### Throws

| [ClosedDirectoryStreamException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html) | if the directory stream is closed |
| [NoSuchFileException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/NoSuchFileException.html) | if the file does not exist *(optional specific exception)* |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | In the case of the default provider, and a security manager is installed, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkDelete(java.lang.String)` method is invoked to check delete access to the file |
|---|---|

#### public abstract V
**getFileAttributeView**
([Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<V\> type)

Returns a new file attribute view to access the file attributes of this
directory.

The resulting file attribute view can be used to read or update the
attributes of this (open) directory. The `type` parameter specifies
the type of the attribute view and the method returns an instance of that
type if supported. Invoking this method to obtain a `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/BasicFileAttributeView.html` always returns an instance of that class that is
bound to this open directory.

The state of resulting file attribute view is intimately connected
to this directory stream. Once the directory stream is `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close()`,
then all methods to read or update attributes will throw `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html`.

##### Parameters

| type | the `Class` object corresponding to the file attribute view |
|---|---|

##### Returns

- a new file attribute view of the specified type bound to this directory stream, or `null` if the attribute view type is not available

#### public abstract V
**getFileAttributeView**
(T path, [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<V\> type, [LinkOption...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/LinkOption.html) options)

Returns a new file attribute view to access the file attributes of a file
in this directory.

The resulting file attribute view can be used to read or update the
attributes of file in this directory. The `type` parameter specifies
the type of the attribute view and the method returns an instance of that
type if supported. Invoking this method to obtain a `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/BasicFileAttributeView.html` always returns an instance of that class that is
bound to the file in the directory.

The state of resulting file attribute view is intimately connected
to this directory stream. Once the directory stream `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close()`,
then all methods to read or update attributes will throw `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html`. The
file is not required to exist at the time that the file attribute view
is created but methods to read or update attributes of the file will
fail when invoked and the file does not exist.

##### Parameters

| path | the path of the file |
| type | the `Class` object corresponding to the file attribute view |
| options | options indicating how symbolic links are handled |
|---|---|

##### Returns

- a new file attribute view of the specified type bound to a this directory stream, or `null` if the attribute view type is not available

#### public abstract void
**move**
(T srcpath, [SecureDirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html)\<T\> targetdir, T targetpath)

Move a file from this directory to another directory.

This method works in a similar manner to `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#move(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...)`
method when the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardCopyOption.html#ATOMIC_MOVE` option
is specified. That is, this method moves a file as an atomic file system
operation. If the `srcpath` parameter is an `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html#isAbsolute()` path then it locates the source file. If the parameter is a
relative path then it is located relative to this open directory. If
the `targetpath` parameter is absolute then it locates the target
file (the `targetdir` parameter is ignored). If the parameter is
a relative path it is located relative to the open directory identified
by the `targetdir` parameter. In all cases, if the target file
exists then it is implementation specific if it is replaced or this
method fails.

##### Parameters

| srcpath | the name of the file to move |
| targetdir | the destination directory |
| targetpath | the name to give the file in the destination directory |
|---|---|

##### Throws

| [ClosedDirectoryStreamException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html) | if this or the target directory stream is closed |
| [FileAlreadyExistsException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/FileAlreadyExistsException.html) | if the file already exists in the target directory and cannot be replaced *(optional specific exception)* |
| [AtomicMoveNotSupportedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/AtomicMoveNotSupportedException.html) | if the file cannot be moved as an atomic file system operation |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | In the case of the default provider, and a security manager is installed, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)` method is invoked to check write access to both the source and target file. |
|---|---|

#### public abstract [SeekableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SeekableByteChannel.html)
**newByteChannel**
(T path, [Set](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html)\<? extends [OpenOption](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/OpenOption.html)\> options, [FileAttribute...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttribute.html)\<?\> attrs)

Opens or creates a file in this directory, returning a seekable byte
channel to access the file.

This method works in exactly the manner specified by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#newByteChannel(java.nio.file.Path,%20java.nio.file.OpenOption...)` method for the
case that the `path` parameter is an `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html#isAbsolute()`
path. When the parameter is a relative path then the file to open or
create is relative to this open directory. In addition to the options
defined by the `Files.newByteChannel` method, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/LinkOption.html#NOFOLLOW_LINKS` option may be used to
ensure that this method fails if the file is a symbolic link.

The channel, once created, is not dependent upon the directory stream
used to create it. Closing this directory stream has no effect upon the
channel.

##### Parameters

| path | the path of the file to open open or create |
| options | options specifying how the file is opened |
| attrs | an optional list of attributes to set atomically when creating the file |
|---|---|

##### Returns

- the seekable byte channel

##### Throws

| [ClosedDirectoryStreamException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html) | if the directory stream is closed |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the set contains an invalid combination of options |
| [UnsupportedOperationException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/UnsupportedOperationException.html) | if an unsupported open option is specified or the array contains attributes that cannot be set atomically when creating the file |
| [FileAlreadyExistsException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/FileAlreadyExistsException.html) | if a file of that name already exists and the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#CREATE_NEW` option is specified *(optional specific exception)* |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | In the case of the default provider, and a security manager is installed, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkRead(java.lang.String)` method is invoked to check read access to the path if the file is opened for reading. The `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)` method is invoked to check write access to the path if the file is opened for writing. |
|---|---|

#### public abstract [SecureDirectoryStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/SecureDirectoryStream.html)\<T\>
**newDirectoryStream**
(T path, [LinkOption...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/LinkOption.html) options)

Opens the directory identified by the given path, returning a `SecureDirectoryStream` to iterate over the entries in the directory.

This method works in exactly the manner specified by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#newDirectoryStream(java.nio.file.Path)` method for the case that
the `path` parameter is an `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html#isAbsolute()` path.
When the parameter is a relative path then the directory to open is
relative to this open directory. The `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/LinkOption.html#NOFOLLOW_LINKS` option may be used to
ensure that this method fails if the file is a symbolic link.

The new directory stream, once created, is not dependent upon the
directory stream used to create it. Closing this directory stream has no
effect upon newly created directory stream.

##### Parameters

| path | the path to the directory to open |
| options | options indicating how symbolic links are handled |
|---|---|

##### Returns

- a new and open `SecureDirectoryStream` object

##### Throws

| [ClosedDirectoryStreamException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/ClosedDirectoryStreamException.html) | if the directory stream is closed |
| [NotDirectoryException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/NotDirectoryException.html) | if the file could not otherwise be opened because it is not a directory *(optional specific exception)* |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | In the case of the default provider, and a security manager is installed, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkRead(java.lang.String)` method is invoked to check read access to the directory. |
|---|---|