Skip to content

Instantly share code, notes, and snippets.

@mandubian
Created August 17, 2012 09:48
Show Gist options
  • Save mandubian/3377514 to your computer and use it in GitHub Desktop.
Save mandubian/3377514 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
}
@mushtaq
Copy link

mushtaq commented Aug 18, 2012

Version without operators is so much more readable.

@mushtaq
Copy link

mushtaq commented Jan 5, 2013

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