Package ratpack.file

Class FileIo


  • public class FileIo
    extends java.lang.Object
    Utilities for streaming to and from files.
    Since:
    1.5
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static ratpack.exec.Promise<java.nio.channels.AsynchronousFileChannel> open​(java.nio.file.Path file, java.nio.file.OpenOption... options)
      Creates a promise for an (open) async file channel.
      static ratpack.exec.Promise<java.nio.channels.AsynchronousFileChannel> open​(java.nio.file.Path file, java.util.Set<? extends java.nio.file.OpenOption> options, java.nio.file.attribute.FileAttribute<?>... attrs)
      Creates a promise for an (open) async file channel.
      static ratpack.exec.Promise<io.netty.buffer.CompositeByteBuf> read​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize)
      Read the contents of a file from the given start until the given stop.
      static ratpack.exec.Promise<io.netty.buffer.CompositeByteBuf> read​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize, long start, long stop)
      Read the contents of a file.
      static ratpack.stream.TransformablePublisher<io.netty.buffer.ByteBuf> readStream​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize)
      Streams the entire contents of a file.
      static ratpack.stream.TransformablePublisher<io.netty.buffer.ByteBuf> readStream​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize, long start, long stop)
      Streams the contents of a file.
      static ratpack.exec.Operation write​(io.netty.buffer.ByteBuf bytes, long position, ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
      Writes the given bytes to the given file, starting at the given position.
      static ratpack.exec.Operation write​(io.netty.buffer.ByteBuf bytes, ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
      Writes the given bytes to the given file, starting at the start.
      static ratpack.exec.Promise<java.lang.Long> write​(Publisher<? extends io.netty.buffer.ByteBuf> publisher, long position, ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
      Writes the bytes of the given publisher to the given file, returning the number of bytes written.
      static ratpack.exec.Promise<java.lang.Long> write​(Publisher<? extends io.netty.buffer.ByteBuf> publisher, ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
      Writes the bytes of the given publisher to the given file starting at the start, returning the number of bytes written.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • open

        public static ratpack.exec.Promise<java.nio.channels.AsynchronousFileChannel> open​(java.nio.file.Path file,
                                                                                           java.nio.file.OpenOption... options)
        Creates a promise for an (open) async file channel.

        Uses AsynchronousFileChannel.open(Path, Set, ExecutorService, FileAttribute[]), but uses the current execution's event loop as the executor service and no file attributes.

        Parameters:
        file - The path of the file to open or create
        options - Options specifying how the file is opened
        Returns:
        a promise for an open async file channel
        See Also:
        AsynchronousFileChannel.open(Path, Set, ExecutorService, FileAttribute[]), open(Path, Set, FileAttribute[])
      • open

        public static ratpack.exec.Promise<java.nio.channels.AsynchronousFileChannel> open​(java.nio.file.Path file,
                                                                                           java.util.Set<? extends java.nio.file.OpenOption> options,
                                                                                           java.nio.file.attribute.FileAttribute<?>... attrs)
        Creates a promise for an (open) async file channel.

        Uses AsynchronousFileChannel.open(Path, Set, ExecutorService, FileAttribute[]), but uses the current execution's event loop as the executor service.

        Parameters:
        file - The path of the file to open or create
        options - Options specifying how the file is opened
        attrs - An optional list of file attributes to set atomically when creating the file
        Returns:
        a promise for an open async file channel
        See Also:
        AsynchronousFileChannel.open(Path, Set, ExecutorService, FileAttribute[]), open(Path, OpenOption...)
      • write

        public static ratpack.exec.Promise<java.lang.Long> write​(Publisher<? extends io.netty.buffer.ByteBuf> publisher,
                                                                 ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
        Writes the bytes of the given publisher to the given file starting at the start, returning the number of bytes written.

        Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        As file system writes are expensive, you may want to consider using ByteBufStreams.buffer(Publisher, long, int, ByteBufAllocator) to “buffer” the data in memory before writing to disk.

        Parameters:
        publisher - the bytes to write
        file - a promise for the file to write to
        Returns:
        a promise for the number of bytes written
      • write

        public static ratpack.exec.Promise<java.lang.Long> write​(Publisher<? extends io.netty.buffer.ByteBuf> publisher,
                                                                 long position,
                                                                 ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
        Writes the bytes of the given publisher to the given file, returning the number of bytes written.

        Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        As file system writes are expensive, you may want to consider using ByteBufStreams.buffer(Publisher, long, int, ByteBufAllocator) to “buffer” the data in memory before writing to disk.

        Parameters:
        publisher - the bytes to write
        position - the position in the file to start writing (must be >= 0)
        file - a promise for the file to write to
        Returns:
        a promise for the number of bytes written
      • write

        public static ratpack.exec.Operation write​(io.netty.buffer.ByteBuf bytes,
                                                   ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
        Writes the given bytes to the given file, starting at the start. Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        Parameters:
        bytes - the bytes to write
        file - the file to write to
        Returns:
        a write operation
      • write

        public static ratpack.exec.Operation write​(io.netty.buffer.ByteBuf bytes,
                                                   long position,
                                                   ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file)
        Writes the given bytes to the given file, starting at the given position. Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        Parameters:
        bytes - the bytes to write
        position - the position in the file to start writing
        file - the file to write to
        Returns:
        a write operation
      • readStream

        public static ratpack.stream.TransformablePublisher<io.netty.buffer.ByteBuf> readStream​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file,
                                                                                                io.netty.buffer.ByteBufAllocator allocator,
                                                                                                int bufferSize,
                                                                                                long start,
                                                                                                long stop)
        Streams the contents of a file.

        Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        Parameters:
        file - a promise for the file to write to
        allocator - the allocator of byte bufs
        bufferSize - the read buffer size (i.e. the size of each emitted buffer)
        start - the position in the file to start reading from (must be >= 0)
        stop - the position in the file to read up to (any value < 1 is treated as EOF)
        Returns:
        a publisher of the byte bufs
        See Also:
        readStream(Promise, ByteBufAllocator, int)
      • readStream

        public static ratpack.stream.TransformablePublisher<io.netty.buffer.ByteBuf> readStream​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file,
                                                                                                io.netty.buffer.ByteBufAllocator allocator,
                                                                                                int bufferSize)
        Streams the entire contents of a file.

        Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        Parameters:
        file - a promise for the file to write to
        allocator - the allocator of byte bufs
        bufferSize - the read buffer size (i.e. the size of each emitted buffer)
        Returns:
        a publisher of the byte bufs
        See Also:
        readStream(Promise, ByteBufAllocator, int, long, long)
      • read

        public static ratpack.exec.Promise<io.netty.buffer.CompositeByteBuf> read​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file,
                                                                                  io.netty.buffer.ByteBufAllocator allocator,
                                                                                  int bufferSize,
                                                                                  long start,
                                                                                  long stop)
        Read the contents of a file.

        Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        Parameters:
        file - a promise for the file to write to
        allocator - the allocator of byte bufs
        bufferSize - the read buffer size (i.e. the size of buffer used for each read operation)
        Returns:
        a publisher of the byte bufs
        See Also:
        readStream(Promise, ByteBufAllocator, int), read(Promise, ByteBufAllocator, int)
      • read

        public static ratpack.exec.Promise<io.netty.buffer.CompositeByteBuf> read​(ratpack.exec.Promise<? extends java.nio.channels.AsynchronousFileChannel> file,
                                                                                  io.netty.buffer.ByteBufAllocator allocator,
                                                                                  int bufferSize)
        Read the contents of a file from the given start until the given stop.

        Use open(Path, Set, FileAttribute[]) to create a file promise.

        The file channel is closed on success or failure.

        Parameters:
        file - a promise for the file to write to
        allocator - the allocator of byte bufs
        bufferSize - the read buffer size (i.e. the size of buffer used for each read operation)
        Returns:
        a publisher of the byte bufs
        See Also:
        readStream(Promise, ByteBufAllocator, int, long, long), read(Promise, ByteBufAllocator, int, long, long)