Skip to content

Instantly share code, notes, and snippets.

@mushtaq
Forked from mandubian/gist:3377514
Created September 2, 2012 09:18
Show Gist options
  • Save mushtaq/3596158 to your computer and use it in GitHub Desktop.
Save mushtaq/3596158 to your computer and use it in GitHub Desktop.
Play2 new plugin: File NonBlocking/Async API - Copying a file
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment