From 429867e3af3fd9ec720b9360a4485851b294c1c1 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 15 Dec 2019 16:59:59 +0000 Subject: [PATCH] Convert SkipOutputStream to Kotlin This commit also changes the skipBytes logic slightly to avoid the need to modify the off/len arguments, which is not possible in Kotlin. --- .../dev/openrs2/util/io/SkipOutputStream.java | 37 ------------------- .../dev/openrs2/util/io/SkipOutputStream.kt | 23 ++++++++++++ 2 files changed, 23 insertions(+), 37 deletions(-) delete mode 100644 util/src/main/java/dev/openrs2/util/io/SkipOutputStream.java create mode 100644 util/src/main/java/dev/openrs2/util/io/SkipOutputStream.kt diff --git a/util/src/main/java/dev/openrs2/util/io/SkipOutputStream.java b/util/src/main/java/dev/openrs2/util/io/SkipOutputStream.java deleted file mode 100644 index 3ac68f4caf..0000000000 --- a/util/src/main/java/dev/openrs2/util/io/SkipOutputStream.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.openrs2.util.io; - -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.OutputStream; - -public final class SkipOutputStream extends FilterOutputStream { - private long skipBytes; - - public SkipOutputStream(OutputStream out, long skipBytes) { - super(out); - this.skipBytes = skipBytes; - } - - @Override - public void write(int b) throws IOException { - if (skipBytes == 0) { - super.write(b); - } else { - skipBytes--; - } - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - if (len >= skipBytes) { - off += skipBytes; - len -= skipBytes; - skipBytes = 0; - } else { - skipBytes -= len; - return; - } - - super.write(b, off, len); - } -} diff --git a/util/src/main/java/dev/openrs2/util/io/SkipOutputStream.kt b/util/src/main/java/dev/openrs2/util/io/SkipOutputStream.kt new file mode 100644 index 0000000000..30a39c2af7 --- /dev/null +++ b/util/src/main/java/dev/openrs2/util/io/SkipOutputStream.kt @@ -0,0 +1,23 @@ +package dev.openrs2.util.io + +import java.io.FilterOutputStream +import java.io.OutputStream + +class SkipOutputStream(out: OutputStream, private var skipBytes: Long) : FilterOutputStream(out) { + override fun write(b: Int) { + if (skipBytes == 0L) { + super.write(b) + } else { + skipBytes-- + } + } + + override fun write(b: ByteArray, off: Int, len: Int) { + if (len >= skipBytes) { + super.write(b, off + skipBytes.toInt(), len - skipBytes.toInt()) + skipBytes = 0 + } else { + skipBytes -= len + } + } +}