Skip to content

Instantly share code, notes, and snippets.

@ppetr
Created June 21, 2013 14:13
Show Gist options
  • Save ppetr/5831433 to your computer and use it in GitHub Desktop.
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
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