public abstract class AsynchronousFileStep extends AsynchronousChannelStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
AsynchronousFileChannel
. The channel will be opened and connected as
necessary. It may also be provided before the step is invoked in a state
relation with the type FILE_CHANNEL
. If the channel is opened by
this step it will have the MetaTypes.MANAGED
flag so that it will be
automatically closed when the CoroutineScope
finishes.AsynchronousChannelStep.ChannelCallback<V,C extends java.nio.channels.AsynchronousChannel>, AsynchronousChannelStep.ChannelOperation<C extends java.nio.channels.AsynchronousChannel>
Modifier and Type | Field and Description |
---|---|
static org.obrel.core.RelationType<java.nio.channels.AsynchronousFileChannel> |
FILE_CHANNEL
State: the
AsynchronousFileChannel that the steps in a coroutine
operate on. |
static org.obrel.core.RelationType<java.lang.Long> |
FILE_POSITION
The current position when reading from or writing to a file channel.
|
CHANNEL_GROUP
Constructor and Description |
---|
AsynchronousFileStep(java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> fGetFileChannel)
Creates a new instance that opens a file channel with the given factory.
|
Modifier and Type | Method and Description |
---|---|
protected java.nio.ByteBuffer |
execute(java.nio.ByteBuffer rData,
Continuation<?> rContinuation)
This method must be implemented by subclasses to provide the actual
functionality of this step.
|
protected java.nio.channels.AsynchronousFileChannel |
getFileChannel(Continuation<?> rContinuation)
Returns the channel to be used by this step.
|
protected java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> |
getFileChannelFactory()
Returns the file channel factory of this step.
|
protected static java.nio.channels.AsynchronousFileChannel |
openFileChannel(java.lang.String sFileName,
java.nio.file.OpenOption rMode,
java.nio.file.OpenOption... rExtraOptions)
A helper function that opens a file channel for a certain file name and
open options.
|
protected abstract boolean |
performAsyncOperation(int nBytesProcessed,
java.nio.channels.AsynchronousFileChannel rChannel,
java.nio.ByteBuffer rData,
AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousFileChannel> rCallback)
Implementation of the ChannelOperation functional interface method
signature.
|
protected abstract void |
performBlockingOperation(java.nio.channels.AsynchronousFileChannel aChannel,
java.nio.ByteBuffer rData)
Must be implemented for the blocking execution of a step.
|
void |
runAsync(java.util.concurrent.CompletableFuture<java.nio.ByteBuffer> fPreviousExecution,
CoroutineStep<java.nio.ByteBuffer,?> rNextStep,
Continuation<?> rContinuation)
Runs this execution step asynchronously as a continuation of a previous
code execution in a
CompletableFuture and proceeds to the next
step afterwards. |
getChannelGroup
fail, runBlocking, terminateCoroutine, toString
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public static final org.obrel.core.RelationType<java.nio.channels.AsynchronousFileChannel> FILE_CHANNEL
AsynchronousFileChannel
that the steps in a coroutine
operate on.public static final org.obrel.core.RelationType<java.lang.Long> FILE_POSITION
public AsynchronousFileStep(java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> fGetFileChannel)
FILE_CHANNEL
.fGetFileChannel
- A function that opens a file channel for the
current continuationprotected static java.nio.channels.AsynchronousFileChannel openFileChannel(java.lang.String sFileName, java.nio.file.OpenOption rMode, java.nio.file.OpenOption... rExtraOptions)
sFileName
- The file namerMode
- The open option for the file access mode (e.g.
READ, WRITE)rExtraOptions
- Optional extra file open optionspublic void runAsync(java.util.concurrent.CompletableFuture<java.nio.ByteBuffer> fPreviousExecution, CoroutineStep<java.nio.ByteBuffer,?> rNextStep, Continuation<?> rContinuation)
CompletableFuture
and proceeds to the next
step afterwards.
Subclasses that need to suspend the invocation of the next step until
some condition is met (e.g. sending or receiving data has finished) need
to override this method and create a Suspension
by invoking
Continuation.suspend(CoroutineStep, CoroutineStep)
on the next
step. If the condition that caused the suspension resolves the coroutine
execution can be resumed by calling Suspension.resume(Object)
.
Subclasses that override this method also need to handle errors by
terminating any further execution (i.e. not resuming a suspension if such
exists) and forwarding the causing exception to Continuation.fail(Throwable)
.
runAsync
in class CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
fPreviousExecution
- The future of the previous code executionrNextStep
- The next step to execute or NULL for nonerContinuation
- The continuation of the executionprotected abstract boolean performAsyncOperation(int nBytesProcessed, java.nio.channels.AsynchronousFileChannel rChannel, java.nio.ByteBuffer rData, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousFileChannel> rCallback) throws java.lang.Exception
java.lang.Exception
AsynchronousChannelStep.ChannelOperation.execute(int,java.nio.channels.AsynchronousChannel,
ByteBuffer, AsynchronousChannelStep.ChannelCallback)
protected abstract void performBlockingOperation(java.nio.channels.AsynchronousFileChannel aChannel, java.nio.ByteBuffer rData) throws java.lang.Exception
AsynchronousFileChannel
which must be accessed through the
blocking API (like Future.get()
).aChannel
- The channel to perform the operation onrData
- The byte buffer for the operation datajava.lang.Exception
- Any kind of exception may be thrownprotected java.nio.ByteBuffer execute(java.nio.ByteBuffer rData, Continuation<?> rContinuation)
execute
in class CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
rData
- The input valuerContinuation
- The continuation of the executionprotected java.nio.channels.AsynchronousFileChannel getFileChannel(Continuation<?> rContinuation) throws java.io.IOException
FILE_CHANNEL
relation. If that doesn't exists or the
channel is closed a new AsynchronousFileChannel
will be opened
and stored in the coroutine relation. Using the coroutine to store the
channel allows coroutines to be structured so that multiple subroutines
perform communication on different channels.rContinuation
- The continuation to query for an existing channeljava.io.IOException
- If opening the channel failsprotected java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> getFileChannelFactory()