Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala 2.13 #87

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions scala/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "webknossos-wrap"

scalaVersion := "2.12.7"
scalaVersion := "2.13.11"

javaOptions in test ++= Seq("-Xmx512m")

Expand Down Expand Up @@ -55,13 +55,12 @@ pomExtra := (
)

libraryDependencies ++= Seq(
"com.google.guava" % "guava" % "21.0",
"com.jsuereth" %% "scala-arm" % "2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was that and why isn't it needed anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic Resource Management. Scala 2.13 has that in the core library (the whole Releasable/Using stuff), compare jsuereth/scala-arm#79

"net.jpountz.lz4" % "lz4" % "1.3.0",
"net.liftweb" % "lift-common_2.10" % "2.6-M3",
"net.liftweb" % "lift-util_2.10" % "3.0-M1",
"org.apache.commons" % "commons-lang3" % "3.1",
"commons-io" % "commons-io" % "2.9.0",
"com.google.guava" % "guava" % "23.0",
"org.lz4" % "lz4-java" % "1.8.0",
"net.liftweb" %% "lift-common" % "3.5.0",
"net.liftweb" %% "lift-util" % "3.5.0",
"org.apache.commons" % "commons-lang3" % "3.13.0",
"commons-io" % "commons-io" % "2.13.0",
)

releasePublishArtifactsAction := PgpKeys.publishSigned.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.nio.channels.FileChannel
import java.nio.file.{Files, Paths, StandardCopyOption}

import org.apache.commons.io.IOUtils
import com.google.common.io.{LittleEndianDataInputStream => DataInputStream}
import com.google.common.io.LittleEndianDataInputStream
import com.scalableminds.webknossos.wrap.util.ExtendedMappedByteBuffer
import com.scalableminds.webknossos.wrap.util.{BoxImplicits, ResourceBox}
import net.jpountz.lz4.LZ4Factory
Expand Down Expand Up @@ -207,7 +207,7 @@ class WKWFile(val header: WKWHeader, fileMode: FileMode.Value, underlyingFile: R
val sourceBlockLengths = if (header.isCompressed) {
header.jumpTable.sliding(2).map(a => (a(1) - a(0)).toInt)
} else {
Array.fill(header.numBlocksPerCube)(header.numBytesPerBlock).toIterator
Array.fill(header.numBlocksPerCube)(header.numBytesPerBlock).iterator
}

val targetBlockLengths = sourceBlockLengths.foldLeft[Box[Seq[Int]]](Full(Seq.empty)) {
Expand Down Expand Up @@ -279,7 +279,7 @@ object WKWFile extends WKWCompressionHelper {
}

def read[T](is: InputStream)(f: (WKWHeader, Iterator[Array[Byte]]) => T): Box[T] = {
ResourceBox.manage(new DataInputStream(is)) { dataStream =>
ResourceBox.manage(new LittleEndianDataInputStream(is)) { dataStream =>
for {
header <- WKWHeader(dataStream, readJumpTable = true)
} yield {
Expand All @@ -297,7 +297,7 @@ object WKWFile extends WKWCompressionHelper {
(0 until header.numBlocksPerCube).foldLeft[Box[Array[Int]]](Full(Array.emptyIntArray)) {
case (Full(blockLengths), _) =>
if (blocks.hasNext) {
val data = blocks.next
val data = blocks.next()
for {
_ <- (data.length == header.numBytesPerBlock) ?~! error("Unexpected block size", header.numBytesPerBlock, data.length)
compressedBlock <- if (header.isCompressed) compressBlock(header.blockType)(data) else Full(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package com.scalableminds.webknossos.wrap.util

import net.liftweb.common.{Box, Failure}
import net.liftweb.common.Box
import net.liftweb.common.{Failure => BoxFailure}
import net.liftweb.util.Helpers.tryo
import resource._

import scala.util.Using.Releasable
import scala.util.{Success, Using, Failure => TryFailure}


object ResourceBox {
def apply[R : Resource](resource: => R): Box[R] = {
def apply[R : Releasable](resource: => R): Box[R] = {
tryo(resource) ~> "Exception during resource creation"
}

def manage[R : Resource, T](resource: => R)(f: R => Box[T]): Box[T] = {
def manage[R : Releasable, T](resource: => R)(f: R => Box[T]): Box[T] = {
for {
r <- ResourceBox(resource)
result <- managed(r).map(f).either.either match {
case Left(ex) =>
Failure(s"Exception during resource management: ${ex.toString}")
case Right(result) =>
result
}
} yield {
result
}
result <- Using.Manager { use =>
f(use(r))
} match {
case TryFailure(ex) =>
BoxFailure(s"Exception during resource management: ${ex.toString}")
case Success(result) =>
result
}
} yield result
}
}