From 25e118b88e4b3b4848ef75a9cb802064268ef117 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 30 May 2018 17:00:37 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20unnecessary=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ch/dissem/bitmessage/utils/Encode.kt | 82 ++++++------------- 1 file changed, 24 insertions(+), 58 deletions(-) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Encode.kt b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Encode.kt index d69af86..e5e36f9 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Encode.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Encode.kt @@ -43,36 +43,31 @@ object Encode { } @JvmStatic - fun varInt(value: Int, buffer: ByteBuffer) = varInt(value.toLong(), buffer) - - @JvmStatic - fun varInt(value: Long, buffer: ByteBuffer) { - if (value < 0) { + fun varInt(value: Number, buffer: ByteBuffer) { + val longValue = value.toLong() + if (longValue < 0) { // This is due to the fact that Java doesn't really support unsigned values. // Please be aware that this might be an error due to a smaller negative value being cast to long. // Normally, negative values shouldn't occur within the protocol, and longs large enough for being // recognized as negatives aren't realistic. buffer.put(0xff.toByte()) - buffer.putLong(value) - } else if (value < 0xfd) { + buffer.putLong(longValue) + } else if (longValue < 0xfd) { buffer.put(value.toByte()) - } else if (value <= 0xffffL) { + } else if (longValue <= 0xffffL) { buffer.put(0xfd.toByte()) buffer.putShort(value.toShort()) - } else if (value <= 0xffffffffL) { + } else if (longValue <= 0xffffffffL) { buffer.put(0xfe.toByte()) buffer.putInt(value.toInt()) } else { buffer.put(0xff.toByte()) - buffer.putLong(value) + buffer.putLong(longValue) } } @JvmStatic - fun varInt(value: Int) = varInt(value.toLong()) - - @JvmStatic - fun varInt(value: Long): ByteArray { + fun varInt(value: Number): ByteArray { val buffer = ByteBuffer.allocate(9) varInt(value, buffer) buffer.flip() @@ -81,11 +76,7 @@ object Encode { @JvmStatic @JvmOverloads - fun varInt(value: Int, stream: OutputStream, counter: AccessCounter? = null) = varInt(value.toLong(), stream, counter) - - @JvmStatic - @JvmOverloads - fun varInt(value: Long, stream: OutputStream, counter: AccessCounter? = null) { + fun varInt(value: Number, stream: OutputStream, counter: AccessCounter? = null) { val buffer = ByteBuffer.allocate(9) varInt(value, buffer) buffer.flip() @@ -95,70 +86,45 @@ object Encode { @JvmStatic @JvmOverloads - fun int8(value: Long, stream: OutputStream, counter: AccessCounter? = null) = int8(value.toInt(), stream, counter) - - @JvmStatic - @JvmOverloads - fun int8(value: Int, stream: OutputStream, counter: AccessCounter? = null) { - stream.write(value) + fun int8(value: Number, stream: OutputStream, counter: AccessCounter? = null) { + stream.write(value.toInt()) AccessCounter.inc(counter) } @JvmStatic @JvmOverloads - fun int16(value: Long, stream: OutputStream, counter: AccessCounter? = null) = int16(value.toShort(), stream, counter) - - @JvmStatic - @JvmOverloads - fun int16(value: Int, stream: OutputStream, counter: AccessCounter? = null) = int16(value.toShort(), stream, counter) - - @JvmStatic - @JvmOverloads - fun int16(value: Short, stream: OutputStream, counter: AccessCounter? = null) { - stream.write(ByteBuffer.allocate(2).putShort(value).array()) + fun int16(value: Number, stream: OutputStream, counter: AccessCounter? = null) { + stream.write(ByteBuffer.allocate(2).putShort(value.toShort()).array()) AccessCounter.inc(counter, 2) } @JvmStatic - fun int16(value: Long, buffer: ByteBuffer) = int16(value.toShort(), buffer) - - @JvmStatic - fun int16(value: Int, buffer: ByteBuffer) = int16(value.toShort(), buffer) - - @JvmStatic - fun int16(value: Short, buffer: ByteBuffer) { - buffer.putShort(value) + fun int16(value: Number, buffer: ByteBuffer) { + buffer.putShort(value.toShort()) } @JvmStatic @JvmOverloads - fun int32(value: Long, stream: OutputStream, counter: AccessCounter? = null) = int32(value.toInt(), stream, counter) - - @JvmStatic - @JvmOverloads - fun int32(value: Int, stream: OutputStream, counter: AccessCounter? = null) { - stream.write(ByteBuffer.allocate(4).putInt(value).array()) + fun int32(value: Number, stream: OutputStream, counter: AccessCounter? = null) { + stream.write(ByteBuffer.allocate(4).putInt(value.toInt()).array()) AccessCounter.inc(counter, 4) } @JvmStatic - fun int32(value: Long, buffer: ByteBuffer) = int32(value.toInt(), buffer) - - @JvmStatic - fun int32(value: Int, buffer: ByteBuffer) { - buffer.putInt(value) + fun int32(value: Number, buffer: ByteBuffer) { + buffer.putInt(value.toInt()) } @JvmStatic @JvmOverloads - fun int64(value: Long, stream: OutputStream, counter: AccessCounter? = null) { - stream.write(ByteBuffer.allocate(8).putLong(value).array()) + fun int64(value: Number, stream: OutputStream, counter: AccessCounter? = null) { + stream.write(ByteBuffer.allocate(8).putLong(value.toLong()).array()) AccessCounter.inc(counter, 8) } @JvmStatic - fun int64(value: Long, buffer: ByteBuffer) { - buffer.putLong(value) + fun int64(value: Number, buffer: ByteBuffer) { + buffer.putLong(value.toLong()) } @JvmStatic