import java.nio._, file._, channels._
import com.thoughtworks.dsl.domains.task.Task
import com.thoughtworks.dsl.keywords._
import com.thoughtworks.dsl.keywords.Shift._
import com.thoughtworks.dsl.keywords.AsynchronousIo.ReadFile
import scala.collection.mutable.ArrayBuffer
import scala.io.Codec
def readAll(channel: AsynchronousFileChannel, temporaryBufferSize: Int = 4096): Task[ArrayBuffer[CharBuffer]] = Task {
val charBuffers = ArrayBuffer.empty[CharBuffer]
val decoder = Codec.UTF8.decoder
val byteBuffer = ByteBuffer.allocate(temporaryBufferSize)
var position: Long = 0L
while (!ReadFile(channel, byteBuffer, position) != -1) {
position += byteBuffer.position()
byteBuffer.flip()
charBuffers += decoder.decode(byteBuffer)
byteBuffer.clear()
}
charBuffers
}
Tasks created from !-notation can be used in for-comprehension,
and other keywords can be used together in the same for block.
For example, the following cat function contains a single for block to concatenate file contents.
It asynchronously iterates elements Seq, ArrayBuffer and String with the help of keywords.Each,
managed native resources with the help of keywords.Using,
performs previously created readAll task with the help of keywords.Shift,
and finally converts the return type as a Task[Vector[Char]].
This member is added by an implicit conversion from AsynchronousIo[Value] to
any2stringadd[AsynchronousIo[Value]] performed by method any2stringadd in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
ArrowAssoc[AsynchronousIo[Value]] performed by method ArrowAssoc in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
Ensuring[AsynchronousIo[Value]] performed by method Ensuring in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
Ensuring[AsynchronousIo[Value]] performed by method Ensuring in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
Ensuring[AsynchronousIo[Value]] performed by method Ensuring in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
Ensuring[AsynchronousIo[Value]] performed by method Ensuring in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
StringFormat[AsynchronousIo[Value]] performed by method StringFormat in scala.Predef.
This member is added by an implicit conversion from AsynchronousIo[Value] to
ArrowAssoc[AsynchronousIo[Value]] performed by method ArrowAssoc in scala.Predef.
The base keyword to perform asynchronous IO in domains.task.Tasks.
The following
readAll
is a Task to read file content with the help of AsynchronousIo.ReadFileTask
s created from !-notation can be used infor
-comprehension, and other keywords can be used together in the samefor
block. For example, the followingcat
function contains a singlefor
block to concatenate file contents. It asynchronously iterates elementsSeq
,ArrayBuffer
andString
with the help of keywords.Each, managed native resources with the help of keywords.Using, performs previously createdreadAll
task with the help of keywords.Shift, and finally converts the return type as aTask[Vector[Char]]
.Then the
cat
function is used to concatenate files from this project, as shown below: