Package de.esoco.coroutine.step.nio
Class FileRead
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<I,O>
-
- de.esoco.coroutine.step.nio.AsynchronousChannelStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
-
- de.esoco.coroutine.step.nio.AsynchronousFileStep
-
- de.esoco.coroutine.step.nio.FileRead
-
- All Implemented Interfaces:
de.esoco.lib.property.Fluent<CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>>,org.obrel.core.FluentRelatable<CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>>,org.obrel.core.Relatable
public class FileRead extends AsynchronousFileStep
Implements asynchronous reading from aAsynchronousFileChannel.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class de.esoco.coroutine.step.nio.AsynchronousChannelStep
AsynchronousChannelStep.ChannelCallback<V,C extends java.nio.channels.AsynchronousChannel>, AsynchronousChannelStep.ChannelOperation<C extends java.nio.channels.AsynchronousChannel>
-
-
Field Summary
-
Fields inherited from class de.esoco.coroutine.step.nio.AsynchronousFileStep
FILE_CHANNEL, FILE_POSITION
-
Fields inherited from class de.esoco.coroutine.step.nio.AsynchronousChannelStep
CHANNEL_GROUP
-
-
Constructor Summary
Constructors Constructor Description FileRead(java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> getFileChannel, java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanperformAsyncOperation(int bytesRead, java.nio.channels.AsynchronousFileChannel channel, java.nio.ByteBuffer data, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousFileChannel> callback)Implementation of the ChannelOperation functional interface method signature.protected voidperformBlockingOperation(java.nio.channels.AsynchronousFileChannel channel, java.nio.ByteBuffer data)Must be implemented for the blocking execution of a step.static AsynchronousFileStepreadFrom(java.lang.String fileName, java.nio.file.OpenOption... extraOptions)InvokesreadFrom(Function)with a function that opens a file channel with the given file name and options.static AsynchronousFileStepreadFrom(java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> getFileChannel)Suspends until a file has been read completely.AsynchronousFileStepuntil(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)Returns a new read step instance the suspends until data has been read from a file and a certain condition on that data is met or an end-of-stream signal is received.-
Methods inherited from class de.esoco.coroutine.step.nio.AsynchronousFileStep
execute, getFileChannel, getFileChannelFactory, openFileChannel, runAsync
-
Methods inherited from class de.esoco.coroutine.step.nio.AsynchronousChannelStep
getChannelGroup
-
Methods inherited from class de.esoco.coroutine.CoroutineStep
runBlocking, terminateCoroutine, toString
-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
-
-
-
-
Constructor Detail
-
FileRead
public FileRead(java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> getFileChannel, java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)
Creates a new instance.- Parameters:
getFileChannel- A function that provides the file channel from the current continuationcheckFinished- A predicate that checks whether the operation is complete by evaluating the byte buffer after reading
-
-
Method Detail
-
readFrom
public static AsynchronousFileStep readFrom(java.util.function.Function<Continuation<?>,java.nio.channels.AsynchronousFileChannel> getFileChannel)
Suspends until a file has been read completely. The data will be stored in the inputByteBufferof the step. If the capacity of the buffer is reached before the EOF signal is received the coroutine will be terminated with aCoroutineException. To stop reading when a certain condition is met a derived step can be created withuntil(BiPredicate).After the data has been fully received
ByteBuffer.flip()will be invoked on the buffer so that it can be used directly for subsequent reading from it.- Parameters:
getFileChannel- A function that provides the file channel from the current continuation- Returns:
- A new step instance
-
readFrom
public static AsynchronousFileStep readFrom(java.lang.String fileName, java.nio.file.OpenOption... extraOptions)
InvokesreadFrom(Function)with a function that opens a file channel with the given file name and options. The optionStandardOpenOption.READwill always be used and should therefore not occur in the extra options.- Parameters:
fileName- The name of the file to read fromextraOptions- Additional options to use besidesStandardOpenOption.READ- Returns:
- A new step instance
-
until
public AsynchronousFileStep until(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)
Returns a new read step instance the suspends until data has been read from a file and a certain condition on that data is met or an end-of-stream signal is received. If the capacity of the buffer is reached before the receiving is finished the coroutine will fail with an exception.- Parameters:
checkFinished- A predicate that checks whether the data has been read completely- Returns:
- A new step instance
-
performAsyncOperation
protected boolean performAsyncOperation(int bytesRead, java.nio.channels.AsynchronousFileChannel channel, java.nio.ByteBuffer data, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousFileChannel> callback) throws java.io.IOExceptionImplementation of the ChannelOperation functional interface method signature.- Specified by:
performAsyncOperationin classAsynchronousFileStep- Throws:
java.io.IOException- See Also:
AsynchronousChannelStep.ChannelOperation.execute(int, java.nio.channels.AsynchronousChannel, ByteBuffer, AsynchronousChannelStep.ChannelCallback)
-
performBlockingOperation
protected void performBlockingOperation(java.nio.channels.AsynchronousFileChannel channel, java.nio.ByteBuffer data) throws java.lang.ExceptionMust be implemented for the blocking execution of a step. It receives anAsynchronousFileChannelwhich must be accessed through the blocking API (likeFuture.get()).- Specified by:
performBlockingOperationin classAsynchronousFileStep- Parameters:
channel- The channel to perform the operation ondata- The byte buffer for the operation data- Throws:
java.lang.Exception- Any kind of exception may be thrown
-
-