Top Description Methods
io.vertx.core.file

public Interface AsyncFile

extends ReadStream<Buffer>, WriteStream<Buffer>
Known Direct Implementers
io.vertx.core.file.impl.AsyncFileImpl
Annotations
@VertxGen
Imports
io.vertx.codegen.annotations.Fluent, .Nullable, .Unstable, .VertxGen, io.vertx.core.AsyncResult, .Future, .Handler, io.vertx.core.buffer.Buffer, io.vertx.core.streams.ReadStream, .WriteStream

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

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

Author
Tim Fox

Method Summary

Modifier and TypeMethod and Description
public Future<Void>

Returns:

a future completed with the result
close
()

Close the file.

public void
close(Handler<AsyncResult<Void>>
the handler
handler
)

Close the file.

public AsyncFile
public AsyncFile
endHandler(Handler<Void> endHandler)

public AsyncFile
public AsyncFile
fetch(long amount)

Redeclares io.vertx.core.streams.ReadStream.fetch.

Fetch the specified amount of elements.

public Future<Void>

Returns:

a future completed with the result
flush
()

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

public AsyncFile
flush(Handler<AsyncResult<Void>> handler)

Same as flush but the handler will be called when the flush is complete or if an error occurs

public long

Returns:

the number of bytes that will be read when using the file as a io.vertx.core.streams.ReadStream
getReadLength
()

public long

Returns:

the current write position the file is at
getWritePos
()

public AsyncFile
public Future<AsyncFileLock>

Returns:

a future indicating the completion of this operation
lock
()

Acquire a non-shared lock on the entire file.

public void
lock(Handler<AsyncResult<AsyncFileLock>> handler)

Like lock() but the handler will be called when the operation is complete or if an error occurs.

public Future<AsyncFileLock>

Returns:

a future indicating the completion of this operation
lock
(long
where the region starts
position
,
long
the size of the region
size
,
boolean
whether the lock should be shared
shared
)

Acquire a lock on a portion of this file.

public void
lock(long position, long size, boolean shared, Handler<AsyncResult<AsyncFileLock>> handler)

Like lock(long, long, boolean) but the handler will be called when the operation is complete or if an error occurs.

public AsyncFile
pause()

Redeclares io.vertx.core.streams.ReadStream.pause.

Pause the ReadStream, it sets the buffer in fetch mode and clears the actual demand.

public AsyncFile

Returns:

a reference to this, so the API can be used fluently
read
(Buffer
the buffer to read into
buffer
,
int
the offset into the buffer where the data will be read
offset
,
long
the position in the file where to start reading
position
,
int
the number of bytes to read
length
,
Handler<AsyncResult<Buffer>>
the handler to call when the write is complete
handler
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

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

public Future<Buffer>
read(Buffer buffer, int offset, long position, int length)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Like read(Buffer, int, long, int, Handler) but returns a Future of the asynchronous result

public AsyncFile
resume()

Redeclares io.vertx.core.streams.ReadStream.resume.

Resume reading, and sets the buffer in flowing mode.

public AsyncFile

Returns:

a reference to this, so the API can be used fluently
setReadBufferSize
(int
the buffer size
readBufferSize
)

Sets the buffer size that will be used to read the data from the file.

public AsyncFile

Returns:

a reference to this, so the API can be used fluently
setReadLength
(long
the bytes that will be read from the file
readLength
)

Sets the number of bytes that will be read when using the file as a io.vertx.core.streams.ReadStream.

public AsyncFile

Returns:

a reference to this, so the API can be used fluently
setReadPos
(long
the position in the file
readPos
)

Sets the position from which data will be read from when using the file as a io.vertx.core.streams.ReadStream.

public AsyncFile

Returns:

a reference to this, so the API can be used fluently
setWritePos
(long
the position in the file
writePos
)

Sets the position from which data will be written when using the file as a io.vertx.core.streams.WriteStream.

public AsyncFile
setWriteQueueMaxSize(int
the max size of the write stream
maxSize
)

Redeclares io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

Set the maximum size of the write queue to maxSize.

public default void
size(Handler<AsyncResult<Long>> handler)

Like size() but the handler will be called when the operation is complete or if an error occurs.

public Future<Long>

Returns:

the size of the file
size
()

public long
sizeBlocking()

Like size() but blocking.

public @Nullable AsyncFileLock

Returns:

the lock if it can be acquired immediately, otherwise null
tryLock
()

Try to acquire a non-shared lock on the entire file.

public @Nullable AsyncFileLock

Returns:

the lock if it can be acquired immediately, otherwise null
tryLock
(long
where the region starts
position
,
long
the size of the region
size
,
boolean
whether the lock should be shared
shared
)

Try to acquire a lock on a portion of this file.

public void
write(Buffer
the buffer to write
buffer
,
long
the position in the file to write it at
position
,
Handler<AsyncResult<Void>>
the handler to call when the write is complete
handler
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Write a io.vertx.core.buffer.Buffer to the file at position position in the file, asynchronously.

public Future<Void>
write(Buffer buffer, long position)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Like write(Buffer, long, Handler) but returns a Future of the asynchronous result

Inherited from io.vertx.core.streams.ReadStream:
collectendHandlerexceptionHandlerhandlerpipepipeTopipeTo
Inherited from io.vertx.core.streams.WriteStream:
drainHandlerendendendendexceptionHandlerwritewritewriteQueueFull

Method Detail

closeback to summary
public Future<Void> close()

Close the file. The actual close happens asynchronously.

Returns:Future<Void>

a future completed with the result

closeback to summary
public void close(Handler<AsyncResult<Void>> handler)

Close the file. The actual close happens asynchronously. The handler will be called when the close is complete, or an error occurs.

Parameters
handler:Handler<AsyncResult<Void>>

the handler

drainHandlerback to summary
public AsyncFile drainHandler(Handler<Void> handler)
Annotations
@Override
endHandlerback to summary
public AsyncFile endHandler(Handler<Void> endHandler)
Annotations
@Override
exceptionHandlerback to summary
public AsyncFile exceptionHandler(Handler<Throwable> handler)
Annotations
@Override
fetchback to summary
public AsyncFile fetch(long amount)

Redeclares io.vertx.core.streams.ReadStream.fetch.

Doc from io.vertx.core.streams.ReadStream.fetch.

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.

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Override
flushback to summary
public 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:Future<Void>

a future completed with the result

flushback to summary
public AsyncFile flush(Handler<AsyncResult<Void>> handler)

Same as flush but the handler will be called when the flush is complete or if an error occurs

Annotations
@Fluent
getReadLengthback to summary
public long getReadLength()
Returns:long

the number of bytes that will be read when using the file as a io.vertx.core.streams.ReadStream

getWritePosback to summary
public long getWritePos()
Returns:long

the current write position the file is at

handlerback to summary
public AsyncFile handler(Handler<Buffer> handler)
Annotations
@Override
lockback to summary
public Future<AsyncFileLock> lock()

Acquire a non-shared lock on the entire file.

Returns:Future<AsyncFileLock>

a future indicating the completion of this operation

Annotations
@Unstable
lockback to summary
public void lock(Handler<AsyncResult<AsyncFileLock>> handler)

Like lock() but the handler will be called when the operation is complete or if an error occurs.

Annotations
@Unstable
lockback to summary
public Future<AsyncFileLock> lock(long position, long size, boolean shared)

Acquire a lock on a portion of this file.

Parameters
position:long

where the region starts

size:long

the size of the region

shared:boolean

whether the lock should be shared

Returns:Future<AsyncFileLock>

a future indicating the completion of this operation

Annotations
@Unstable
lockback to summary
public void lock(long position, long size, boolean shared, Handler<AsyncResult<AsyncFileLock>> handler)

Like lock(long, long, boolean) but the handler will be called when the operation is complete or if an error occurs.

Annotations
@Unstable
pauseback to summary
public AsyncFile pause()

Redeclares io.vertx.core.streams.ReadStream.pause.

Doc from io.vertx.core.streams.ReadStream.pause.

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.

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Override
readback to summary
public AsyncFile read(Buffer buffer, int offset, long position, int length, Handler<AsyncResult<Buffer>> handler)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

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.

The handler will be called when the close is complete, or if an error occurs.

Parameters
buffer:Buffer

the buffer to read into

offset:int

the offset into the buffer where the data will be read

position:long

the position in the file where to start reading

length:int

the number of bytes to read

handler:Handler<AsyncResult<Buffer>>

the handler to call when the write is complete

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Fluent
readback to summary
public Future<Buffer> read(Buffer buffer, int offset, long position, int length)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Like read(Buffer, int, long, int, Handler) but returns a Future of the asynchronous result

resumeback to summary
public AsyncFile resume()

Redeclares io.vertx.core.streams.ReadStream.resume.

Doc from io.vertx.core.streams.ReadStream.resume.

Resume reading, and sets the buffer in flowing mode.

If the ReadStream has been paused, reading will recommence on it.

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Override
setReadBufferSizeback to summary
public 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:int

the buffer size

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Fluent
setReadLengthback to summary
public AsyncFile setReadLength(long readLength)

Sets the number of bytes that will be read when using the file as a io.vertx.core.streams.ReadStream.

Parameters
readLength:long

the bytes that will be read from the file

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Fluent
setReadPosback to summary
public AsyncFile setReadPos(long readPos)

Sets the position from which data will be read from when using the file as a io.vertx.core.streams.ReadStream.

Parameters
readPos:long

the position in the file

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Fluent
setWritePosback to summary
public AsyncFile setWritePos(long writePos)

Sets the position from which data will be written when using the file as a io.vertx.core.streams.WriteStream.

Parameters
writePos:long

the position in the file

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Fluent
setWriteQueueMaxSizeback to summary
public AsyncFile setWriteQueueMaxSize(int maxSize)

Redeclares io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

Doc from io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

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 io.vertx.core.net.NetSocket, etc...

Parameters
maxSize:int

the max size of the write stream

Returns:AsyncFile

a reference to this, so the API can be used fluently

Annotations
@Override
sizeback to summary
public default void size(Handler<AsyncResult<Long>> handler)

Like size() but the handler will be called when the operation is complete or if an error occurs.

sizeback to summary
public Future<Long> size()
Returns:Future<Long>

the size of the file

sizeBlockingback to summary
public long sizeBlocking()

Like size() but blocking.

Exceptions
FileSystemException:
if an error occurs
tryLockback to summary
public @Nullable AsyncFileLock tryLock()

Try to acquire a non-shared lock on the entire file.

Returns:@Nullable AsyncFileLock

the lock if it can be acquired immediately, otherwise null

Annotations
@Unstable
tryLockback to summary
public @Nullable AsyncFileLock tryLock(long position, long size, boolean shared)

Try to acquire a lock on a portion of this file.

Parameters
position:long

where the region starts

size:long

the size of the region

shared:boolean

whether the lock should be shared

Returns:@Nullable AsyncFileLock

the lock if it can be acquired immediately, otherwise null

Annotations
@Unstable
writeback to summary
public void write(Buffer buffer, long position, Handler<AsyncResult<Void>> handler)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Write a io.vertx.core.buffer.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

The handler will be called when the write is complete, or if an error occurs.

Parameters
buffer:Buffer

the buffer to write

position:long

the position in the file to write it at

handler:Handler<AsyncResult<Void>>

the handler to call when the write is complete

writeback to summary
public Future<Void> write(Buffer buffer, long position)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Like write(Buffer, long, Handler) but returns a Future of the asynchronous result