mirror of https://github.com/sbt/sbt.git
Add more neurotic resource management, close()ing of streams.
This commit is contained in:
parent
20ca549612
commit
315fbec370
|
|
@ -204,25 +204,50 @@ class GigahorseUrlHandler(http: OkHttpClient) extends AbstractURLHandler {
|
||||||
private val ErrorBodyTruncateLen = 512 // in case some bad service returns files rather than messages in error bodies
|
private val ErrorBodyTruncateLen = 512 // in case some bad service returns files rather than messages in error bodies
|
||||||
private val DefaultErrorCharset = java.nio.charset.StandardCharsets.UTF_8
|
private val DefaultErrorCharset = java.nio.charset.StandardCharsets.UTF_8
|
||||||
|
|
||||||
// this is perhaps overly cautious, but oh well
|
// neurotic resource managemement...
|
||||||
private def readTruncated(is: InputStream): Option[(Array[Byte], Boolean)] = {
|
// we could use this elsewhere in the class too
|
||||||
val os = new ByteArrayOutputStream(ErrorBodyTruncateLen)
|
private def borrow[S <: AutoCloseable, T](rsrc: => S)(op: S => T): T = {
|
||||||
var count = 0
|
val r = rsrc
|
||||||
var b = is.read()
|
val out = {
|
||||||
var truncated = false
|
try {
|
||||||
while (!truncated && b >= 0) {
|
op(r)
|
||||||
if (count >= ErrorBodyTruncateLen) {
|
} catch {
|
||||||
truncated = true
|
case NonFatal(t) => {
|
||||||
} else {
|
try {
|
||||||
os.write(b)
|
r.close()
|
||||||
count += 1
|
} catch {
|
||||||
b = is.read()
|
case NonFatal(ct) => t.addSuppressed(ct)
|
||||||
|
}
|
||||||
|
throw t
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count > 0) {
|
r.close()
|
||||||
Some((os.toByteArray, truncated))
|
out
|
||||||
} else {
|
}
|
||||||
None
|
|
||||||
|
// this is perhaps overly cautious, but oh well
|
||||||
|
private def readTruncated(byteStream: InputStream): Option[(Array[Byte], Boolean)] = {
|
||||||
|
borrow(byteStream) { is =>
|
||||||
|
borrow(new ByteArrayOutputStream(ErrorBodyTruncateLen)) { os =>
|
||||||
|
var count = 0
|
||||||
|
var b = is.read()
|
||||||
|
var truncated = false
|
||||||
|
while (!truncated && b >= 0) {
|
||||||
|
if (count >= ErrorBodyTruncateLen) {
|
||||||
|
truncated = true
|
||||||
|
} else {
|
||||||
|
os.write(b)
|
||||||
|
count += 1
|
||||||
|
b = is.read()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count > 0) {
|
||||||
|
Some((os.toByteArray, truncated))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue