Skip to content

Instantly share code, notes, and snippets.

@conorgriffin
Last active February 22, 2021 16:24
Show Gist options
  • Save conorgriffin/9eae19f8bf6e7ce5c0aaab5b8970609e to your computer and use it in GitHub Desktop.
Save conorgriffin/9eae19f8bf6e7ce5c0aaab5b8970609e to your computer and use it in GitHub Desktop.
SftpOperations - Multiple Read Requests
def retrieveFileInputStream(name: String,
handler: Handler,
offset: Long,
maxUnconfirmedReads: Int): Try[InputStream] =
Try {
val remoteFile = handler.open(name, java.util.EnumSet.of(OpenMode.READ))
val is = maxUnconfirmedReads match {
case m if m > 1 =>
new remoteFile.ReadAheadRemoteFileInputStream(m, offset) {
override def close(): Unit =
try {
super.close()
} finally {
remoteFile.close()
}
}
case _ =>
new remoteFile.RemoteFileInputStream(offset) {
override def close(): Unit =
try {
super.close()
} finally {
remoteFile.close()
}
}
}
Option(is).getOrElse {
remoteFile.close()
throw new IOException(s"$name: No such file or directory")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment