Version Matrix


line-break-preserving-line-splitting artifacts Build Status


  • Streams from a, so can process very large files
  • Unlike, retains the line-break at the end of the line, whether it's UNIX, Windows, etc
  • Implements the scala.collection.Iterator trait. Lines are not retained in memory after being returned by next().
  • Limits memory consumption to a single ring-buffer the lives the lifetime of the iterator, and, for lines that loop round the ring-buffer, temporary StringBuilder instances that are garbage-collected after the line is returned.
  • Reading a line using next() will return as soon as the has returned enough characters - will at most only attempt to fill the ring-buffer.

For my purposes, the first two requirements were the most important. If I hadn't wanted to retain the line-breaks, I'd probably have used


libraryDependencies += "com.madgag" %% "line-break-preserving-line-splitting" % "[version]"

Example usage

import com.madgag.linesplitting._

val splitLines = {
  val reader: = new"Foo\nBar\rBoo")

  val iter = new LineBreakPreservingIterator(reader)
// splitLines: Seq[String] = List(
//   """Foo
// """,
//   """Bar
//   "Boo"
// )"\n", "[LF]").replace("\r", "[CR]"))
// res0: Seq[String] = List("Foo[LF]", "Bar[CR]", "Boo")