in facebook-core/src/main/java/com/facebook/internal/FileLruCache.kt [352:404]
fun readHeader(stream: InputStream): JSONObject? {
val version = stream.read()
if (version != HEADER_VERSION) {
return null
}
var headerSize = 0
repeat(3) {
val b = stream.read()
if (b == -1) {
log(
LoggingBehavior.CACHE,
TAG,
"readHeader: stream.read returned -1 while reading header size")
return null
}
headerSize = headerSize shl 8
headerSize += b and 0xff
}
val headerBytes = ByteArray(headerSize)
var count = 0
while (count < headerBytes.size) {
val readCount = stream.read(headerBytes, count, headerBytes.size - count)
if (readCount < 1) {
log(
LoggingBehavior.CACHE,
TAG,
"readHeader: stream.read stopped at " +
Integer.valueOf(count) +
" when expected " +
headerBytes.size)
return null
}
count += readCount
}
val headerString = String(headerBytes)
val header: JSONObject
val tokener = JSONTokener(headerString)
header =
try {
val parsed = tokener.nextValue()
if (parsed !is JSONObject) {
log(
LoggingBehavior.CACHE,
TAG,
"readHeader: expected JSONObject, got " + parsed.javaClass.canonicalName)
return null
}
parsed
} catch (e: JSONException) {
throw IOException(e.message)
}
return header
}