Fix SkipOutputStream

Calling super.write(byte[], int, int) in FilterOutputStream didn't work,
as it called super.write(byte). This is inefficient and also caused us
to fiddle with skipBytes twice, causing us to skip the incorrect number
of bytes.
bzip2
Graham 5 years ago
parent f96e9225bf
commit 8d90a70b6a
  1. 15
      common/src/main/java/dev/openrs2/common/io/SkipOutputStream.kt

@ -1,12 +1,11 @@
package dev.openrs2.common.io
import java.io.FilterOutputStream
import java.io.OutputStream
class SkipOutputStream(out: OutputStream, private var skipBytes: Long) : FilterOutputStream(out) {
class SkipOutputStream(private val out: OutputStream, private var skipBytes: Long) : OutputStream() {
override fun write(b: Int) {
if (skipBytes == 0L) {
super.write(b)
out.write(b)
} else {
skipBytes--
}
@ -14,10 +13,18 @@ class SkipOutputStream(out: OutputStream, private var skipBytes: Long) : FilterO
override fun write(b: ByteArray, off: Int, len: Int) {
if (len >= skipBytes) {
super.write(b, off + skipBytes.toInt(), len - skipBytes.toInt())
out.write(b, off + skipBytes.toInt(), len - skipBytes.toInt())
skipBytes = 0
} else {
skipBytes -= len
}
}
override fun flush() {
out.flush()
}
override fun close() {
out.close()
}
}

Loading…
Cancel
Save