Writing With Typeclasses
Details on how output is converted to bytes and how the design can be extended and used
Ints As Bytes
In Java when you write an Integer to an OuputStream that integer is treated as a byte and only the lowest value byte of the int is written.
Scala IO differs in that an integer is written as 4 bytes and one must explicitely coerce an Int to a byte. The following examples demostrate how one might do that.
import scalax.io._
import Resource._
val out = fromFileString("out")
// One of the easiest ways is to coerce the
// ints into bytes before passing them to a
// write method
out.write(List[Byte](1,2,3,4))
out.write(1.toByte)
// writeIntsAsBytes (or patchIntsAsBytes) is
// another good solution
out.writeIntsAsBytes(1,2,3,4)
out.insertIntsAsBytes(4,1,2,3)
out.patchIntsAsBytes(3,OverwriteAll,1,2,3)
out.appendIntsAsBytes(1,2,3)
// The final option is to pass in the
//(OutputConverter.IntAsByteConverter) object to the write method:
out.write(3)(OutputConverter.IntAsByteConverter)
out.write(List(1,2,3,4))(OutputConverter.TraversableIntAsByteConverter)