blob: f7d2118846202ca7960e515b5e6acfec35b256f1 [file] [log] [blame] [raw]
package li.cil.oc.server.fs
import java.io.{FileNotFoundException, IOException, OutputStream}
import li.cil.oc.api
import li.cil.oc.api.fs.Mode
import net.minecraft.nbt.{NBTTagList, NBTTagCompound}
import scala.collection.mutable
trait OutputStreamFileSystem extends InputStreamFileSystem {
private val handles = mutable.Map.empty[Int, Handle]
// ----------------------------------------------------------------------- //
override def open(path: String, mode: Mode.Value) = mode match {
case Mode.Read => super.open(path, mode)
case _ => if (!isDirectory(path)) {
val handle = Iterator.continually((Math.random() * Int.MaxValue).toInt + 1).filterNot(handles.contains).next()
openOutputStream(path, mode) match {
case Some(stream) =>
handles += handle -> new Handle(this, handle, path, stream)
handle
case _ => throw new FileNotFoundException()
}
} else throw new FileNotFoundException()
}
override def file(handle: Int) = super.file(handle).orElse(handles.get(handle))
override def close() {
super.close()
for (handle <- handles.values)
handle.close()
handles.clear()
}
// ----------------------------------------------------------------------- //
override def readFromNBT(nbt: NBTTagCompound) {
super.readFromNBT(nbt)
val handlesNbt = nbt.getTagList("output")
(0 until handlesNbt.tagCount).map(handlesNbt.tagAt).map(_.asInstanceOf[NBTTagCompound]).foreach(handleNbt => {
val handle = handleNbt.getInteger("handle")
val path = handleNbt.getString("path")
openOutputStream(path, Mode.Append) match {
case Some(stream) =>
val fileHandle = new Handle(this, handle, path, stream)
handles += handle -> fileHandle
case _ => // The source file seems to have changed since last time.
}
})
}
override def writeToNBT(nbt: NBTTagCompound) {
super.writeToNBT(nbt)
val handlesNbt = new NBTTagList()
for (file <- handles.values) {
assert(!file.isClosed)
val handleNbt = new NBTTagCompound()
handleNbt.setInteger("handle", file.handle)
handleNbt.setString("path", file.path)
handlesNbt.appendTag(handleNbt)
}
nbt.setTag("output", handlesNbt)
}
// ----------------------------------------------------------------------- //
protected def openOutputStream(path: String, mode: Mode.Value): Option[OutputStream]
// ----------------------------------------------------------------------- //
private class Handle(val owner: OutputStreamFileSystem, val handle: Int, val path: String, val stream: OutputStream) extends api.fs.Handle {
var isClosed = false
val position = 0L
val length = 0L
def close() = if (!isClosed) {
isClosed = true
owner.handles -= handle
stream.close()
}
def read(into: Array[Byte]) = throw new IOException("bad file descriptor")
def seek(to: Long) = throw new IOException("bad file descriptor")
def write(value: Array[Byte]) {
stream.write(value)
}
}
}