Class FileIo
- java.lang.Object
-
- ratpack.core.file.FileIo
-
public class FileIo extends 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 Promise<AsynchronousFileChannel>
open(Path file, OpenOption... options)
Creates a promise for an (open) async file channel.static Promise<AsynchronousFileChannel>
open(Path file, Set<? extends OpenOption> options, FileAttribute<?>... attrs)
Creates a promise for an (open) async file channel.static Promise<io.netty.buffer.CompositeByteBuf>
read(Promise<? extends AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize)
Read the contents of a file from the given start until the given stop.static Promise<io.netty.buffer.CompositeByteBuf>
read(Promise<? extends AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize, long start, long stop)
Read the contents of a file.static TransformablePublisher<io.netty.buffer.ByteBuf>
readStream(Promise<? extends AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize)
Streams the entire contents of a file.static TransformablePublisher<io.netty.buffer.ByteBuf>
readStream(Promise<? extends AsynchronousFileChannel> file, io.netty.buffer.ByteBufAllocator allocator, int bufferSize, long start, long stop)
Streams the contents of a file.static Operation
write(io.netty.buffer.ByteBuf bytes, long position, Promise<? extends AsynchronousFileChannel> file)
Writes the given bytes to the given file, starting at the given position.static Operation
write(io.netty.buffer.ByteBuf bytes, Promise<? extends AsynchronousFileChannel> file)
Writes the given bytes to the given file, starting at the start.static Promise<Long>
write(Publisher<? extends io.netty.buffer.ByteBuf> publisher, long position, Promise<? extends AsynchronousFileChannel> file)
Writes the bytes of the given publisher to the given file, returning the number of bytes written.static Promise<Long>
write(Publisher<? extends io.netty.buffer.ByteBuf> publisher, Promise<? extends AsynchronousFileChannel> file)
Writes the bytes of the given publisher to the given file starting at the start, returning the number of bytes written.
-
-
-
Method Detail
-
open
public static Promise<AsynchronousFileChannel> open(Path 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 createoptions
- 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 Promise<AsynchronousFileChannel> open(Path file, Set<? extends OpenOption> options, 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 createoptions
- Options specifying how the file is openedattrs
- 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 Promise<Long> write(Publisher<? extends io.netty.buffer.ByteBuf> publisher, Promise<? extends 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 writefile
- a promise for the file to write to- Returns:
- a promise for the number of bytes written
-
write
public static Promise<Long> write(Publisher<? extends io.netty.buffer.ByteBuf> publisher, long position, Promise<? extends 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 writeposition
- 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 Operation write(io.netty.buffer.ByteBuf bytes, Promise<? extends AsynchronousFileChannel> file)
Writes the given bytes to the given file, starting at the start. Useopen(Path, Set, FileAttribute[])
to create a file promise.The file channel is closed on success or failure.
- Parameters:
bytes
- the bytes to writefile
- the file to write to- Returns:
- a write operation
-
write
public static Operation write(io.netty.buffer.ByteBuf bytes, long position, Promise<? extends AsynchronousFileChannel> file)
Writes the given bytes to the given file, starting at the given position. Useopen(Path, Set, FileAttribute[])
to create a file promise.The file channel is closed on success or failure.
- Parameters:
bytes
- the bytes to writeposition
- the position in the file to start writingfile
- the file to write to- Returns:
- a write operation
-
readStream
public static TransformablePublisher<io.netty.buffer.ByteBuf> readStream(Promise<? extends 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 toallocator
- the allocator of byte bufsbufferSize
- 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 TransformablePublisher<io.netty.buffer.ByteBuf> readStream(Promise<? extends 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 toallocator
- the allocator of byte bufsbufferSize
- 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 Promise<io.netty.buffer.CompositeByteBuf> read(Promise<? extends 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 toallocator
- the allocator of byte bufsbufferSize
- 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 Promise<io.netty.buffer.CompositeByteBuf> read(Promise<? extends 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 toallocator
- the allocator of byte bufsbufferSize
- 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)
-
-