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
readAllis a Task to read file content with the help of AsynchronousIo.ReadFileTasks created from !-notation can be used infor-comprehension, and other keywords can be used together in the sameforblock. For example, the followingcatfunction contains a singleforblock to concatenate file contents. It asynchronously iterates elementsSeq,ArrayBufferandStringwith the help of keywords.Each, managed native resources with the help of keywords.Using, performs previously createdreadAlltask with the help of keywords.Shift, and finally converts the return type as aTask[Vector[Char]].Then the
catfunction is used to concatenate files from this project, as shown below:Task.toFuture(Task { (!cat(Paths.get(".sbtopts"), Paths.get(".scalafmt.conf"))).mkString should be( "-J-XX:MaxMetaspaceSize=512M\n-J-Xmx5G\n-J-Xss6M\nversion = \"1.5.1\"\nmaxColumn = 120" ) })