diff options
Diffstat (limited to 'javalib-ex/src/main/scala/java/util')
3 files changed, 172 insertions, 0 deletions
diff --git a/javalib-ex/src/main/scala/java/util/zip/InflaterInputStream.scala b/javalib-ex/src/main/scala/java/util/zip/InflaterInputStream.scala new file mode 100644 index 0000000..10aa04e --- /dev/null +++ b/javalib-ex/src/main/scala/java/util/zip/InflaterInputStream.scala @@ -0,0 +1,18 @@ +package java.util.zip + +import java.io._ + +class InflaterInputStream(in: InputStream) extends FilterInputStream(in) { + + // Not implemented: + // def this(in: InputStream, inf: Inflater) + // def this(in: InputStream, inf: Inflater, size: Int) + // protected var buf: Array[Byte] + // protected var inf: Inflater + // protected var len: Int + + override def markSupported(): Boolean = false + override def mark(readlimit: Int): Unit = {} + override def reset(): Unit = throw new IOException("Reset not supported") + +} diff --git a/javalib-ex/src/main/scala/java/util/zip/ZipEntry.scala b/javalib-ex/src/main/scala/java/util/zip/ZipEntry.scala new file mode 100644 index 0000000..89f37a3 --- /dev/null +++ b/javalib-ex/src/main/scala/java/util/zip/ZipEntry.scala @@ -0,0 +1,67 @@ +package java.util.zip + +class ZipEntry(private[this] val _name: String) extends Cloneable { + + private[this] var _comment: String = null + private[this] var _csize: Long = -1 + private[this] var _crc: Long = -1 + private[this] var _extra: Array[Byte] = null + private[this] var _method: Int = -1 + private[this] var _size: Long = -1 + private[this] var _time: Long = -1 + + def this(e: ZipEntry) = { + this(e.getName()) + setComment(e.getComment()) + setCompressedSize(e.getCompressedSize()) + setCrc(e.getCrc()) + setExtra(e.getExtra()) + setMethod(e.getMethod()) + setSize(e.getSize()) + setTime(e.getTime()) + } + + override def clone(): Object = { + val result = super.clone() + if (getExtra() != null) + setExtra(getExtra().clone().asInstanceOf[Array[Byte]]) + result + } + + def getComment(): String = _comment + def getCompressedSize(): Long = _csize + def getCrc(): Long = _crc + def getExtra(): Array[Byte] = _extra + def getMethod(): Int = _method + def getName(): String = _name + def getSize(): Long = _size + def getTime(): Long = _time + + // Strangely, the Javalib defines hashCode, but not equals. + override def hashCode(): Int = { + import scala.util.hashing.MurmurHash3._ + + var acc = 0x45322 + acc = mix(acc, getComment.##) + acc = mix(acc, getCompressedSize.##) + acc = mix(acc, getCrc.##) + acc = mix(acc, getExtra.##) + acc = mix(acc, getMethod.##) + acc = mix(acc, getName.##) + acc = mix(acc, getSize.##) + acc = mixLast(acc, getTime.##) + finalizeHash(acc, 8) + } + + def isDirectory(): Boolean = _name.endsWith("/") + + def setComment(comment: String): Unit = { _comment = comment } + def setCompressedSize(csize: Long): Unit = { _csize = csize } + def setCrc(crc: Long): Unit = { _crc = crc } + def setExtra(extra: Array[Byte]): Unit = { _extra = extra } + def setMethod(method: Int): Unit = { _method = method } + def setSize(size: Long): Unit = { _size = size } + def setTime(time: Long): Unit = { _time = time } + override def toString(): String = _name + +} diff --git a/javalib-ex/src/main/scala/java/util/zip/ZipInputStream.scala b/javalib-ex/src/main/scala/java/util/zip/ZipInputStream.scala new file mode 100644 index 0000000..082f6fc --- /dev/null +++ b/javalib-ex/src/main/scala/java/util/zip/ZipInputStream.scala @@ -0,0 +1,87 @@ +package java.util.zip + +import java.io._ + +import scala.scalajs.js +import scala.scalajs.js.typedarray._ + +class ZipInputStream(in: InputStream) extends InflaterInputStream(in) { + + // Not implemented + // - All static constant fields (zip internals) + // - protected def createZipEntry(name: String): ZipEntry + + private[this] val entryIter = { + import js.Dynamic.{global => g} + + val data = in match { + case in: ArrayBufferInputStream => + // Simulate reading all the data + while (in.skip(in.available()) > 0) {} + new Uint8Array(in.buffer, in.offset, in.length) + case _ => + val arr = new js.Array[Int] + var x = in.read() + while (x != -1) { + arr.push(x) + x = in.read() + } + new Uint8Array(arr) + } + + val zip = js.Dynamic.newInstance(g.JSZip)(data) + val entries = zip.files.asInstanceOf[js.Dictionary[js.Dynamic]] + + entries.iterator + } + + private[this] var inner: ArrayBufferInputStream = null + + override def close(): Unit = { + closeEntry() + super.close() + } + + override def available(): Int = { + if (inner == null || inner.available() <= 0) 0 + else 1 + } + + def closeEntry(): Unit = { + if (inner != null) + inner.close() + inner = null + } + + def getNextEntry(): ZipEntry = { + closeEntry() + if (entryIter.hasNext) { + val (name, jsEntry) = entryIter.next() + val res = new ZipEntry(name) + res.setTime(jsEntry.date.asInstanceOf[js.Date].getTime().toLong) + res.setComment(jsEntry.comment.asInstanceOf[String]) + + inner = new ArrayBufferInputStream( + jsEntry.asArrayBuffer().asInstanceOf[ArrayBuffer]) + + res + } else null + } + + override def read(): Int = { + if (inner == null) -1 + else inner.read() + } + + override def read(buf: Array[Byte], off: Int, len: Int): Int = { + if (len == 0) 0 + else if (inner == null) -1 + else inner.read(buf, off, len) + } + + override def skip(n: Long): Long = { + if (inner == null) 0 + else inner.skip(n) + } + +} |