Created
August 17, 2012 09:48
-
-
Save mandubian/3377514 to your computer and use it in GitHub Desktop.
Play2 new plugin: File NonBlocking/Async API - Copying a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"copy file" in { | |
var i = 0 | |
val fileGenerator = Enumerator.fromCallback( () => | |
if(i<1000){ i+=1; Future.successful(Some((new java.util.Date).getTime.toString + "\n")) } else Future(None) | |
) | |
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 | |
} |
Is this plugin available somewhere? Google search did not help.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version without operators is so much more readable.