Skip to content

Commit

Permalink
Work on autoClose
Browse files Browse the repository at this point in the history
  • Loading branch information
kwalcock committed Jun 9, 2022
1 parent 0f3cf7d commit dc42ff6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ object WordEmbeddingMapPool {
def loadEmbedding(name: String, fileLocation: String, resourceLocation: String, compact: Boolean): WordEmbeddingMap = {
val StreamResult(inputStream, _, format) = inputStreamer.stream(name, fileLocation, resourceLocation)
.getOrElse(throw new RuntimeException(s"WordEmbeddingMap $name could not be opened."))
val wordEmbeddingMap = inputStream.autoClose { inputStream =>
val binary = format == InputStreamer.Format.Bin
val wordEmbeddingMap = {
// This is intentionally not using autoClose because the inputStream might be a
// JarURLConnection#JarURLInputStream which can't be autoClosed in newer (> 1.8)
// versions of Java. See further explanation in InputStreamer.
try {
val binary = format == InputStreamer.Format.Bin

if (compact) CompactWordEmbeddingMap(inputStream, binary)
else ExplicitWordEmbeddingMap(inputStream, binary)
if (compact) CompactWordEmbeddingMap(inputStream, binary)
else ExplicitWordEmbeddingMap(inputStream, binary)
}
finally {
inputStream.close()
}
}

wordEmbeddingMap
Expand Down
5 changes: 4 additions & 1 deletion main/src/main/scala/org/clulab/utils/InputStreamer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import scala.util.Failure
import scala.util.Try

class PublicCloseInputStream(inputStream: InputStream) extends InputStream {

override def read(): Int = inputStream.read()

// This can be reflected upon.
Expand Down Expand Up @@ -35,7 +36,9 @@ class InputStreamer(val provider: AnyRef = InputStreamer, direct: Boolean = true
// sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream.close() throws
// java.io.IOException accessible: module java.base does not "opens sun.net.www.protocol.jar"
// to unnamed module @5ffead27.
new PublicCloseInputStream(inputStream)
// Update: Use of PublicCloseInputStream was found the be incredibly slow, so the use of
// autoClose in WordEmbeddingMapPool was replaced by a try and finally on the raw inputStream.
// new PublicCloseInputStream(inputStream)
}

protected def getInputStream(name: String, fileLocation: String, resourceLocation: String,
Expand Down

0 comments on commit dc42ff6

Please sign in to comment.