Created
June 21, 2013 14:13
-
-
Save ppetr/5831433 to your computer and use it in GitHub Desktop.
An example of using scala-conduit for reading a file util a given character is found.
See https://github.com/ppetr/scala-conduit
This file contains hidden or 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
import java.nio._ | |
import java.nio.channels.Channels | |
import java.io.{ FileInputStream, IOException } | |
import java.util.zip._ | |
import scala.util.control.Exception._ | |
import conduit._ | |
import conduit.Pipe._ | |
object CloseExample extends App { | |
/** | |
* Searches a buffer for a character. Rewinds the buffer afterwards. | |
*/ | |
def findBuf(c: Byte, buf: ByteBuffer): Option[Int] = | |
try { | |
while (buf.hasRemaining()) | |
if (buf.get() == c) | |
return Some(buf.position() - 1); | |
return None; | |
} finally { | |
buf.rewind(); | |
} | |
/** | |
* Filters buffers until a given character is found. The last buffer | |
* (truncated up to the character) is also included. | |
*/ | |
def untilPipe(c: Byte): Pipe[ByteBuffer,ByteBuffer,Unit] = { | |
// This code doesn't need to finalize anything, so we declare | |
// an empty implicit finalizer. | |
implicit val fin = Finalizer.empty | |
def loop: Pipe[ByteBuffer,ByteBuffer,Unit] = | |
requestI((buf: ByteBuffer) => | |
findBuf(c, buf) match { | |
case Some(i) => buf.limit(i); respond(buf); // don't continue | |
case None => respond(buf, loop); | |
} | |
); | |
loop | |
} | |
val file = "test.gz"; | |
// Create a new source that chunks a file as ByteBuffer's. | |
// (Note that the buffer changes on every step.) | |
val source: Source[ByteBuffer,Unit] | |
= NIO.readChannel( | |
ByteBuffer.allocate(4096), | |
Channels.newChannel(new GZIPInputStream( | |
new FileInputStream(file))) | |
); | |
// Sink that prints bytes to the standard output. | |
// You would create your own sink doing whatever you want. | |
val sink: Sink[ByteBuffer,Unit] | |
= NIO.writeChannel(Channels.newChannel(System.out)); | |
runPipe(source >-> untilPipe(-1) >-> sink); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment