Resources
Examples of using the Resource API to wrap existing Java IO objects
Create Resources
Several examples of creating Resources
import scalax.io._
import java.io._
import java.nio.channels._
import java.net.URL
// see codec examples in scala io core for details on why there is an implicit codec here
implicit val codec = scalax.io.Codec.UTF8
// get various input streams, readers an channels
val inputStream: InputStream = new URL("http://someurl.com").openStream
val in: InputStreamResource[InputStream] = Resource.fromInputStream(inputStream)
val bufferedIn: InputStreamResource[BufferedInputStream] = in.buffered
val readableChannel: Resource[ReadableByteChannel] = in.readableByteChannel
val reader: ReaderResource[Reader] = in.reader
val bufferedReader: ReaderResource[BufferedReader] = reader.buffered
// get various output streams and channels
val outputStream: FileOutputStream = new FileOutputStream("file")
val out: OutputStreamResource[OutputStream] = Resource.fromOutputStream(outputStream)
val bufferedOut: OutputStreamResource[BufferedOutputStream] = out.buffered
val writableChannel: Resource[WritableByteChannel] = out.writableByteChannel
val writer: WriterResource[Writer] = out.writer
val bufferedWriter: WriterResource[BufferedWriter] = writer.buffered
// examples getting ByteChannels
// default is a read/write/create channel
val channel: SeekableByteChannelResource[SeekableByteChannel] = Resource.fromFileString("file")
val channel2: SeekableByteChannelResource[SeekableByteChannel] =
Resource.fromRandomAccessFile(new RandomAccessFile("file","rw"))
val seekable: Seekable = channel2
val inOut: Input with Output = channel
val channel3: ByteChannelResource[FileChannel] =
Resource.fromByteChannel(new RandomAccessFile("file","rw").getChannel)
val inOut2: Input with Output = channel2
val readableByteChannel = Channels.newChannel(new FileInputStream("file"))
val readChannel : ReadableByteChannelResource[ReadableByteChannel] =
Resource.fromReadableByteChannel(readableByteChannel)
val in2:Input = readChannel
val writableByteChannel = Channels.newChannel(new FileOutputStream("file"))
val writeChannel : WritableByteChannelResource[WritableByteChannel] =
Resource.fromWritableByteChannel(writableByteChannel)
val out2:Output = writeChannel