Package io.vertx.core.file

## Interface AsyncFile

- All Superinterfaces:
    `ReadStream<Buffer>`, `StreamBase`, `WriteStream<Buffer>`

      public interface AsyncFile
      extends ReadStream<Buffer>, WriteStream<Buffer>

  Represents a file on the file-system which can be read from, or written to asynchronously.

  This class also implements `ReadStream` and `WriteStream`. This allows the data to be piped to and from other streams, e.g. an `HttpClientRequest` instance, using the `Pipe` class

  Author:
    Tim Fox

- - ### Method Summary

    All Methods Instance Methods Abstract Methods Default Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `Future<Void>` | `close()` | Close the file.
    `AsyncFile` | `drainHandler​(Handler<Void> handler)` | Set a drain handler on the stream.
    `AsyncFile` | `endHandler​(Handler<Void> endHandler)` | Set an end handler.
    `AsyncFile` | `exceptionHandler​(Handler<Throwable> handler)` | Set an exception handler on the read stream.
    `AsyncFile` | `fetch​(long amount)` | Fetch the specified `amount` of elements.
    `Future<Void>` | `flush()` | Flush any writes made to this file to underlying persistent storage.
    `long` | `getReadLength()` |
    `long` | `getWritePos()` |
    `AsyncFile` | `handler​(Handler<Buffer> handler)` | Set a data handler.
    `default Future<AsyncFileLock>` | `lock()` | Acquire a non-shared lock on the entire file.
    `Future<AsyncFileLock>` | `lock​(long position, long size, boolean shared)` | Acquire a lock on a portion of this file.
    `AsyncFile` | `pause()` | Pause the `ReadStream`, it sets the buffer in `fetch` mode and clears the actual demand.
    `Future<Buffer>` | `read​(Buffer buffer, int offset, long position, int length)` | Reads `length` bytes of data from the file at position `position` in the file, asynchronously.
    `AsyncFile` | `resume()` | Resume reading, and sets the buffer in `flowing` mode.
    `AsyncFile` | `setReadBufferSize​(int readBufferSize)` | Sets the buffer size that will be used to read the data from the file.
    `AsyncFile` | `setReadLength​(long readLength)` | Sets the number of bytes that will be read when using the file as a `ReadStream`.
    `AsyncFile` | `setReadPos​(long readPos)` | Sets the position from which data will be read from when using the file as a `ReadStream`.
    `AsyncFile` | `setWritePos​(long writePos)` | Sets the position from which data will be written when using the file as a `WriteStream`.
    `AsyncFile` | `setWriteQueueMaxSize​(int maxSize)` | Set the maximum size of the write queue to `maxSize`.
    `Future<Long>` | `size()` |
    `long` | `sizeBlocking()` | Like `size()` but blocking.
    `default AsyncFileLock` | `tryLock()` | Try to acquire a non-shared lock on the entire file.
    `AsyncFileLock` | `tryLock​(long position, long size, boolean shared)` | Try to acquire a lock on a portion of this file.
    `default <T> Future<T>` | `withLock​(long position, long size, boolean shared, java.util.function.Supplier<Future<T>> block)` | Acquire a lock on a portion of this file.
    `default <T> Future<T>` | `withLock​(java.util.function.Supplier<Future<T>> block)` | Acquire a non-shared lock on the entire file.
    `Future<Void>` | `write​(Buffer buffer, long position)` | Write a `Buffer` to the file at position `position` in the file, asynchronously.

    - ### Methods inherited from interface io.vertx.core.streams.ReadStream

      `collect, pipe, pipeTo`

    - ### Methods inherited from interface io.vertx.core.streams.WriteStream

      `end, end, write, writeQueueFull`

- - ### Method Detail

    - #### handler

          AsyncFile handler​(Handler<Buffer> handler)

      Description copied from interface: `ReadStream`
      Set a data handler. As data is read, the handler will be called with the data.
      Specified by:
        `handler` in interface `ReadStream<Buffer>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### pause

          AsyncFile pause()

      Description copied from interface: `ReadStream`
      Pause the `ReadStream`, it sets the buffer in `fetch` mode and clears the actual demand.

      While it's paused, no data will be sent to the data `handler`.

      Specified by:
        `pause` in interface `ReadStream<Buffer>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### resume

          AsyncFile resume()

      Description copied from interface: `ReadStream`
      Resume reading, and sets the buffer in `flowing` mode.

      If the `ReadStream` has been paused, reading will recommence on it.
      Specified by:
        `resume` in interface `ReadStream<Buffer>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### endHandler

          AsyncFile endHandler​(Handler<Void> endHandler)

      Description copied from interface: `ReadStream`
      Set an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.
      Specified by:
        `endHandler` in interface `ReadStream<Buffer>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### setWriteQueueMaxSize

          AsyncFile setWriteQueueMaxSize​(int maxSize)

      Description copied from interface: `WriteStream`
      Set the maximum size of the write queue to `maxSize`. You will still be able to write to the stream even if there is more than `maxSize` items in the write queue. This is used as an indicator by classes such as `Pipe` to provide flow control.

      The value is defined by the implementation of the stream, e.g in bytes for a `NetSocket`, etc...
      Specified by:
        `setWriteQueueMaxSize` in interface `WriteStream<Buffer>`
      Parameters:
        `maxSize` - the max size of the write stream
      Returns:
        a reference to this, so the API can be used fluently

    - #### drainHandler

          AsyncFile drainHandler​(Handler<Void> handler)

      Description copied from interface: `WriteStream`
      Set a drain handler on the stream. If the write queue is full, then the handler will be called when the write queue is ready to accept buffers again. See `Pipe` for an example of this being used.

      The stream implementation defines when the drain handler, for example it could be when the queue size has been reduced to `maxSize / 2`.

      Specified by:
        `drainHandler` in interface `WriteStream<Buffer>`
      Parameters:
        `handler` - the handler
      Returns:
        a reference to this, so the API can be used fluently

    - #### exceptionHandler

          AsyncFile exceptionHandler​(Handler<Throwable> handler)

      Description copied from interface: `ReadStream`
      Set an exception handler on the read stream.
      Specified by:
        `exceptionHandler` in interface `ReadStream<Buffer>`
      Specified by:
        `exceptionHandler` in interface `StreamBase`
      Specified by:
        `exceptionHandler` in interface `WriteStream<Buffer>`
      Parameters:
        `handler` - the exception handler
      Returns:
        a reference to this, so the API can be used fluently

    - #### fetch

          AsyncFile fetch​(long amount)

      Description copied from interface: `ReadStream`
      Fetch the specified `amount` of elements. If the `ReadStream` has been paused, reading will recommence with the specified `amount` of items, otherwise the specified `amount` will be added to the current stream demand.
      Specified by:
        `fetch` in interface `ReadStream<Buffer>`
      Returns:
        a reference to this, so the API can be used fluently

    - #### close

          Future<Void> close()

      Close the file. The actual close happens asynchronously.
      Returns:
        a future completed with the result

    - #### write

          Future<Void> write​(Buffer buffer,
                             long position)

      Write a `Buffer` to the file at position `position` in the file, asynchronously.

      If `position` lies outside of the current size of the file, the file will be enlarged to encompass it.

      When multiple writes are invoked on the same file there are no guarantees as to order in which those writes actually occur

      Parameters:
        `buffer` - the buffer to write
        `position` - the position in the file to write it at
      Returns:
        a future notified when the write is complete

    - #### read

          Future<Buffer> read​(Buffer buffer,
                              int offset,
                              long position,
                              int length)

      Reads `length` bytes of data from the file at position `position` in the file, asynchronously.

      The read data will be written into the specified `Buffer buffer` at position `offset`.

      If data is read past the end of the file then zero bytes will be read.

      When multiple reads are invoked on the same file there are no guarantees as to order in which those reads actually occur.

      Parameters:
        `buffer` - the buffer to read into
        `offset` - the offset into the buffer where the data will be read
        `position` - the position in the file where to start reading
        `length` - the number of bytes to read
      Returns:
        a future notified when the write is complete

    - #### flush

          Future<Void> flush()

      Flush any writes made to this file to underlying persistent storage.

      If the file was opened with `flush` set to `true` then calling this method will have no effect.

      The actual flush will happen asynchronously.

      Returns:
        a future completed with the result

    - #### setReadPos

          AsyncFile setReadPos​(long readPos)

      Sets the position from which data will be read from when using the file as a `ReadStream`.
      Parameters:
        `readPos` - the position in the file
      Returns:
        a reference to this, so the API can be used fluently

    - #### setReadLength

          AsyncFile setReadLength​(long readLength)

      Sets the number of bytes that will be read when using the file as a `ReadStream`.
      Parameters:
        `readLength` - the bytes that will be read from the file
      Returns:
        a reference to this, so the API can be used fluently

    - #### getReadLength

          long getReadLength()

      Returns:
        the number of bytes that will be read when using the file as a `ReadStream`

    - #### setWritePos

          AsyncFile setWritePos​(long writePos)

      Sets the position from which data will be written when using the file as a `WriteStream`.
      Parameters:
        `writePos` - the position in the file
      Returns:
        a reference to this, so the API can be used fluently

    - #### getWritePos

          long getWritePos()

      Returns:
        the current write position the file is at

    - #### setReadBufferSize

          AsyncFile setReadBufferSize​(int readBufferSize)

      Sets the buffer size that will be used to read the data from the file. Changing this value will impact how much the data will be read at a time from the file system.
      Parameters:
        `readBufferSize` - the buffer size
      Returns:
        a reference to this, so the API can be used fluently

    - #### sizeBlocking

          long sizeBlocking()

      Like `size()` but blocking.
      Throws:
        `FileSystemException` - if an error occurs

    - #### size

          Future<Long> size()

      Returns:
        the size of the file

    - #### tryLock

          default AsyncFileLock tryLock()

      Try to acquire a non-shared lock on the entire file.
      Returns:
        the lock if it can be acquired immediately, otherwise `null`

    - #### tryLock

          AsyncFileLock tryLock​(long position,
                                long size,
                                boolean shared)

      Try to acquire a lock on a portion of this file.
      Parameters:
        `position` - where the region starts
        `size` - the size of the region
        `shared` - whether the lock should be shared
      Returns:
        the lock if it can be acquired immediately, otherwise `null`

    - #### lock

          default Future<AsyncFileLock> lock()

      Acquire a non-shared lock on the entire file.
      Returns:
        a future indicating the completion of this operation

    - #### lock

          Future<AsyncFileLock> lock​(long position,
                                     long size,
                                     boolean shared)

      Acquire a lock on a portion of this file.
      Parameters:
        `position` - where the region starts
        `size` - the size of the region
        `shared` - whether the lock should be shared
      Returns:
        a future indicating the completion of this operation

    - #### withLock

          default <T> Future<T> withLock​(java.util.function.Supplier<Future<T>> block)

      Acquire a non-shared lock on the entire file.

      When the `block` is called, the lock is already acquired, it will be released when the `Future<T>` returned by the block completes.

      When the `block` fails, the lock is released and the returned future is failed with the cause of the failure.

      Parameters:
        `block` - the code block called after lock acquisition
      Returns:
        the future returned by the `block`

    - #### withLock

          default <T> Future<T> withLock​(long position,
                                         long size,
                                         boolean shared,
                                         java.util.function.Supplier<Future<T>> block)

      Acquire a lock on a portion of this file.

      When the `block` is called, the lock is already acquired , it will be released when the `Future<T>` returned by the block completes.

      When the `block` fails, the lock is released and the returned future is failed with the cause of the failure.

      Parameters:
        `position` - where the region starts
        `size` - the size of the region
        `shared` - whether the lock should be shared
        `block` - the code block called after lock acquisition
      Returns:
        the future returned by the `block`