Created
December 8, 2011 13:34
-
-
Save makotan/1447000 to your computer and use it in GitHub Desktop.
ScalaなのにJavaっぽくVFSを使ってファイルを書き込んでみた 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testcase.vfs | |
import org.scalatest.FunSuite | |
import org.junit.runner.RunWith | |
import org.scalatest.junit.JUnitRunner | |
import org.apache.commons.vfs2.VFS | |
import java.io.BufferedOutputStream | |
import java.io.OutputStreamWriter | |
import java.io.BufferedWriter | |
import org.apache.commons.vfs2.FileObject | |
import org.apache.commons.vfs2.FileContent | |
import java.io.OutputStream | |
import scala.util.control.Exception._ | |
import java.io.IOException | |
@RunWith(classOf[JUnitRunner]) | |
class VFSSample extends FunSuite { | |
val testFileName = "file://" + System.getProperties().getProperty("user.dir") + "/test/hoge" + System.currentTimeMillis() + ".txt" | |
val fsManager = VFS.getManager() | |
test("vfs write test 01") { | |
val file = fsManager.resolveFile(testFileName) | |
val content = file.getContent() | |
val os = content.getOutputStream(true) | |
val osw = new OutputStreamWriter(os) | |
val bw = new BufferedWriter(osw) | |
bw.write("test data\n") | |
bw.flush() | |
bw.close | |
osw.close | |
os.close | |
content.close | |
file.close | |
} | |
test("vfs write test 02") { | |
val fw = catching(classOf[IOException]) . | |
either { TextFileWriter(testFileName) } | |
fw match { | |
case Left(e) => throw e | |
case Right(fw) => { | |
try { | |
fw.write("test data 02") | |
} finally { | |
fw.close() | |
} | |
} | |
} | |
} | |
test("vfs write test list 03") { | |
val fw = catching(classOf[IOException]) either TextFileWriter(testFileName) | |
fw match { | |
case Left(e) => throw e | |
case Right(fw) => { | |
List("data03-1", "data03-2").map(fw.writer()) | |
fw.close | |
} | |
} | |
List("data03-3", "data03-4").foldLeft(TextFileWriter(testFileName))((w, txt) => w.write(txt)).close() | |
} | |
object TextFileWriter { | |
def apply(fileName:String , isAppend: Boolean) = { | |
new TextFileWriter(fileName, isAppend) | |
} | |
def apply(fileName:String) = { | |
new TextFileWriter(fileName, true) | |
} | |
} | |
class TextFileWriter(fileName:String , isAppend: Boolean) { | |
def toEither[U,R](arg: Either[Throwable, U] , p : (U) => R) : Either[Throwable, R] = { | |
eith(arg,{o:U => catching(classOf[IOException]) either p(o)}) | |
} | |
def eith[U,R](arg: Either[Throwable, U] , f : (U) => R) : R = { | |
arg match { | |
case Left(e) => throw e | |
case Right(o) => f(o) | |
} | |
} | |
val file = catching(classOf[IOException]) either fsManager.resolveFile(testFileName) | |
val content = toEither( file , {f:FileObject => f.getContent()} ) | |
val os = toEither(content , {c:FileContent => c.getOutputStream(isAppend)}) | |
val osw = toEither(os , {o:OutputStream => new OutputStreamWriter(o)}) | |
val bw = toEither(osw , {o:OutputStreamWriter => new BufferedWriter(o)}) | |
def write(text: String) : TextFileWriter = { | |
eith(bw, | |
{ bw: BufferedWriter => | |
{ | |
bw.write(text) | |
bw.write("\n") | |
bw.flush() | |
this | |
} | |
}) | |
} | |
def writer() : (String) => Unit = { | |
write | |
} | |
def close() : Unit = { | |
def close(clo: Either[Throwable, { def close(): Unit }]): Unit = { | |
eith(clo , {clo :{ def close(): Unit } => clo.close}) | |
} | |
close(bw) | |
close(osw) | |
close(os) | |
close(content) | |
close(file) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment