Skip to content

Instantly share code, notes, and snippets.

@sadache
Forked from mandubian/gist:3377514
Created August 17, 2012 19:58
Show Gist options
  • Save sadache/3382059 to your computer and use it in GitHub Desktop.
Save sadache/3382059 to your computer and use it in GitHub Desktop.
Play2 new plugin: File NonBlocking/Async API - Copying a file
"copy file" in {
val fileGenerator = Enumerator.repeat(new java.util.Date.getTime.toString + "\n").through(Enumeratee.take(1000))
//val fileGenerator = Enumerator.repeat(new java.util.Date.getTime.toString + "\n") &>> Enumeratee.take(1000)
val f = FileChannel("/tmp/testwrite.txt").delete.writing.create
val f2 = FileChannel("/tmp/testwrite2.txt").delete.writing.create
fileGenerator // generates data
.through(RichEnumeratee.binarize()) // binarizes data to write into File
.run(f.writer()) // writes into file
.flatMap{ sz => f.reader().run(f2.writer()) } // when written, reads file and writes into dest file
.onSuccess{ case sz: Long => println("wrote %d".format(sz)) } // when finished, tells how many bytes were written
// Same with operators
fileGenerator // generates data
&> RichEnumeratee.binarize() // binarizes data to write into File
|>>> f.writer() // writes into file
flatMap { sz => f.reader() |>>> (f2.writer()) } // when written, reads file and writes into dest file
onSuccess { case sz: Long => println("wrote %d".format(sz)) } // when finished, tells how many bytes were written
success
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment