Skip to content

Instantly share code, notes, and snippets.

@RichardBradley
Created December 8, 2016 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBradley/bcd1a5e61fcc83e4e59f8b9b0bc2301c to your computer and use it in GitHub Desktop.
Save RichardBradley/bcd1a5e61fcc83e4e59f8b9b0bc2301c to your computer and use it in GitHub Desktop.
import java.io.{FileInputStream, IOException, InputStream}
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
import scala.io.Source
object q5221524 {
def main(args: Array[String]): Unit = {
// Setup
Files.write(
Paths.get("test.txt"),
"test".getBytes(StandardCharsets.UTF_8))
def isClosed(s: InputStream): Boolean =
try {
s.available()
false
} catch {
case e: IOException
if e.getMessage.contains("Stream Closed") =>
true
}
// Tests:
val is = new FileInputStream("test.txt")
assert(!isClosed(is))
println(Source.fromInputStream(is).mkString(""))
assert(!isClosed(is))
is.close()
assert(isClosed(is))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment