Skip to content

Instantly share code, notes, and snippets.

@ppurang
Forked from sadache/gist:3382059
Created August 18, 2012 09:51
Show Gist options
  • Save ppurang/3385731 to your computer and use it in GitHub Desktop.
Save ppurang/3385731 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