Class 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 a AsynchronousFileChannel.
    • 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 boolean performAsyncOperation​(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 void performBlockingOperation​(java.nio.channels.AsynchronousFileChannel channel, java.nio.ByteBuffer data)
      Must be implemented for the blocking execution of a step.
      static AsynchronousFileStep readFrom​(java.lang.String fileName, java.nio.file.OpenOption... extraOptions)
      Invokes readFrom(Function) with a function that opens a file channel with the given file name and options.
      static AsynchronousFileStep readFrom​(java.util.function.Function<Continuation<?>,​java.nio.channels.AsynchronousFileChannel> getFileChannel)
      Suspends until a file has been read completely.
      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.
      • Methods inherited from class org.obrel.core.RelatedObject

        deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface de.esoco.lib.property.Fluent

        _with
      • Methods inherited from interface org.obrel.core.FluentRelatable

        with, with, with
      • Methods inherited from interface org.obrel.core.Relatable

        deleteRelation, deleteRelation, deleteRelations, get, getAll, getOption, getRelation, getRelationCount, getRelations, getRelations, hasFlag, hasRelation, hasRelations, init, set, set, set, set, set, setOption, streamRelations, transform
    • 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 continuation
        checkFinished - 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 input ByteBuffer of the step. If the capacity of the buffer is reached before the EOF signal is received the coroutine will be terminated with a CoroutineException. To stop reading when a certain condition is met a derived step can be created with until(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)
        Invokes readFrom(Function) with a function that opens a file channel with the given file name and options. The option StandardOpenOption.READ will always be used and should therefore not occur in the extra options.
        Parameters:
        fileName - The name of the file to read from
        extraOptions - Additional options to use besides StandardOpenOption.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
      • performBlockingOperation

        protected void performBlockingOperation​(java.nio.channels.AsynchronousFileChannel channel,
                                                java.nio.ByteBuffer data)
                                         throws java.lang.Exception
        Must be implemented for the blocking execution of a step. It receives an AsynchronousFileChannel which must be accessed through the blocking API (like Future.get()).
        Specified by:
        performBlockingOperation in class AsynchronousFileStep
        Parameters:
        channel - The channel to perform the operation on
        data - The byte buffer for the operation data
        Throws:
        java.lang.Exception - Any kind of exception may be thrown