readNBytes impl
This commit is contained in:
parent
e8eed40a73
commit
64b777c11a
@ -7,6 +7,7 @@ import java.io.BufferedInputStream
|
||||
import java.io.Closeable
|
||||
import java.io.DataInputStream
|
||||
import java.io.File
|
||||
import java.io.IOError
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.RandomAccessFile
|
||||
@ -101,6 +102,25 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
|
||||
return reader.read()
|
||||
}
|
||||
|
||||
override fun readNBytes(len: Int): ByteArray {
|
||||
require(len >= 0) { "Negative length to read: $len" }
|
||||
|
||||
val readMax = len.coerceAtMost((length - innerOffset).toInt())
|
||||
|
||||
if (readMax == 0)
|
||||
return ByteArray(0)
|
||||
|
||||
val b = ByteArray(readMax)
|
||||
reader.seek(innerOffset + offset)
|
||||
val readBytes = reader.read(b)
|
||||
|
||||
if (readBytes != readMax)
|
||||
throw IOError(RuntimeException("Reading $readMax bytes returned only $readBytes bytes"))
|
||||
|
||||
innerOffset += readBytes
|
||||
return b
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray, off: Int, len: Int): Int {
|
||||
Objects.checkFromIndexSize(off, len, b.size)
|
||||
|
||||
@ -116,9 +136,8 @@ class StarboundPak(val path: File, callback: (finished: Boolean, status: String)
|
||||
reader.seek(innerOffset + offset)
|
||||
val readBytes = reader.read(b, off, readMax)
|
||||
|
||||
if (readBytes == -1) {
|
||||
if (readBytes == -1)
|
||||
throw RuntimeException("Unexpected EOF, want to read $readMax bytes from starting $offset in $path")
|
||||
}
|
||||
|
||||
innerOffset += readBytes
|
||||
return readBytes
|
||||
|
Loading…
Reference in New Issue
Block a user