Since unmanaged resources is a less common use-case there is not a factory object like there is for normal managed Resources. Instead certain objects can be converted to unmanaged resources using the JavaConverters implicit methods as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | // JavaConverters implicit methods are required to create the // unmanaged resources scala > import scalax.io.JavaConverters. _ import scalax.io.JavaConverters. _ // now that JavaConverters are in scope we can convert // System.out to a Output object and use it as any normal // output object except that the stream will not be closed // WriteableByteChannels can also be converted scala > System.out.asUnmanagedOutput.write( "Not closing right?" ) Not closing right? // See, still not closed scala > System.out.asUnmanagedOutput.write( "Still not closed?" ) Still not closed? scala > import java.io. _ import java.io. _ // another demonstration of converting an output stream to a // unmanaged Output object. This is frowned upon unless // unavoidable. scala > val fout = new FileOutputStream( "somefile.txt" ) fout : java.io.FileOutputStream = java.io.FileOutputStream @ 23987721 scala > val foutOutput = fout.asUnmanagedOutput foutOutput : scalax.io.Output = scalax.io.unmanaged.WritableByteChannelResource @ 36 b 54 a 77 scala > foutOutput.write( "Hello " ) scala > foutOutput.write( "World!" ) scala > fout.close // see the output object is broken now because the stream is closed scala > foutOutput.write( "boom" ) No Main Exception --- class java.nio.channels.ClosedChannelException( null ) ... // The mirror image is converting an input stream to an Input object scala > val fin = new FileInputStream( "somefile.txt" ) fin : java.io.FileInputStream = java.io.FileInputStream @ 4 fcbc 4 de scala > val chars = fin.asUnmanagedInput.chars chars : scalax.io.LongTraversable[Char] = LongTraversable(...) // normally a LongTraversable will close the resource // but this LongTraversable is obtained from a unmanagedInput // so can be used multiple times without closing the resource scala > chars.head res 19 : Char = H scala > chars.head res 20 : Char = e scala > chars.head res 21 : Char = l scala > chars.head res 22 : Char = l // don't forget to close scala > fin.close // quick demo of using channels // the following is a major anti-pattern and is // here mainly for completeness scala > val fchannel = new RandomAccessFile( "somefile.txt" , "rw" ).getChannel fchannel : java.nio.channels.FileChannel = sun.nio.ch.FileChannelImpl @ 1 e 10 cb 60 scala > val fInput 2 = fchannel.asUnmanagedInput fInput 2 : scalax.io.Input = scalax.io.unmanaged.ReadableByteChannelResource @ 679 a 339 e scala > println(fInput 2 .string) Hello World! scala > fchannel.isOpen res 13 : Boolean = true scala > val fOutput = fchannel.asUnmanagedOutput fOutput : scalax.io.Output = scalax.io.unmanaged.WritableByteChannelResource @ 9 cc 8 b 91 scala > fOutput.write( "hi there" ) scala > println(fInput 2 .string) hi thererld! // don't forget to close scala > fchannel.close |