AsFooConverter

Examples for creating Output/Input/ReadChars/WriteChars/etc using the asFooConverter pattern

There are two patterns for creating IO objects. One is using the Resources API. This is often the best for creating resources from closeable objects such as InputStreams. The Resource API takes a code block for constructing the resource and can there for recreate the resource when needed.

The other pattern is to convert an existing object to an Input/Ouput/ReadChars/WriteChars/Seekable object. The idea here is to import the implicit conversions contained in the target object (Input/Ouput/ReadChars/WriteChars/Seekable) and then call the asInput/asOuput/asReadChars/asWriteChars/asSeekable.

Examples of the latter pattern are described here.

Note: In all examples where a codec is required, the codec can be explicitly or implicitly passed

As Input

Convert to Input
    import scalax.io.Codec
    import scalax.io.Input._
    
    val webpage:String = new java.net.URL("http://www.scala-lang.org").asInput.slurpString(Codec.UTF8)
    val bytes:Array[Byte] = List[Byte](1,2,3,4).asInput.byteArray
  

As Output

Convert to Output
    import scalax.io.Codec
    import scalax.io.Output._

    implicit val codec = Codec.UTF8

    new java.io.File("io").asOutput.write("hi file")
  

As Seekable

Convert to Seekable
    import scalax.io.Seekable._
    
    new java.io.File("io").asSeekable.insert(2,List[Byte](1,2,3))
  

As Write Chars

Convert to WriteChars
    import scalax.io.Codec
    import scalax.io.WriteChars._
    
    new java.io.File("io").asBinaryWriteChars(Codec.UTF8).write("This is a message in UTF8")
    new StringWriter().asWriteChars
  

As Read Chars

Convert to ReadChars
    import scalax.io.Codec
    import scalax.io.ReadChars._

    val lines:Traversable[String] = new java.io.File("io").asBinaryReadChars(Codec.UTF8).lines()
    val webpage:String = new java.io.StringReader("hello").asReadChars.slurpString