diff --git a/src/dorkbox/bytes/Base58.kt b/src/dorkbox/bytes/Base58.kt index 72685ef..8a5f424 100644 --- a/src/dorkbox/bytes/Base58.kt +++ b/src/dorkbox/bytes/Base58.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2023 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /* * Copyright 2011 Google Inc. * Copyright 2018 Andreas Schildbach @@ -151,9 +167,9 @@ fun String.decodeBase58(): ByteArray { // Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits). val input58 = ByteArray(length) - for (i in 0 until length) { + for (i in indices) { val c = this[i] - val digit = if (c.code < 128) Base58.alphabetIndices[c.toInt()] else -1 + val digit = if (c.code < 128) Base58.alphabetIndices[c.code] else -1 if (digit < 0) { throw NumberFormatException("Illegal character $c at position $i") } diff --git a/src/dorkbox/bytes/ByteArrayBuffer.kt b/src/dorkbox/bytes/ByteArrayBuffer.kt index f99556e..b5961ff 100644 --- a/src/dorkbox/bytes/ByteArrayBuffer.kt +++ b/src/dorkbox/bytes/ByteArrayBuffer.kt @@ -535,34 +535,7 @@ class ByteArrayBuffer { */ private fun readVarInt(optimizePositive: Boolean): Int { val buffer = bytes - if (capacity - position < 5) { - return readInt_slow(optimizePositive) - } - var b = buffer[position++].toInt() - var result = b and 0x7F - if (b and 0x80 != 0) { - b = buffer[position++].toInt() - result = result or (b and 0x7F shl 7) - if (b and 0x80 != 0) { - b = buffer[position++].toInt() - result = result or (b and 0x7F shl 14) - if (b and 0x80 != 0) { - b = buffer[position++].toInt() - result = result or (b and 0x7F shl 21) - if (b and 0x80 != 0) { - b = buffer[position++].toInt() - result = result or (b and 0x7F shl 28) - } - } - } - } - return if (optimizePositive) result else result ushr 1 xor -(result and 1) - } - - private fun readInt_slow(optimizePositive: Boolean): Int { - val buffer = bytes - - // The buffer is guaranteed to have at least 1 byte. + var b = buffer[position++].toInt() var result = b and 0x7F if (b and 0x80 != 0) { @@ -1056,7 +1029,7 @@ class ByteArrayBuffer { val n = capacity while (i < n) { - chars[ii] = buffer[i].toChar() + chars[ii] = buffer[i].toInt().toChar() i++ ii++ } diff --git a/src/dorkbox/bytes/ByteBufInput.kt b/src/dorkbox/bytes/ByteBufInput.kt index 0e7818b..d2544c6 100644 --- a/src/dorkbox/bytes/ByteBufInput.kt +++ b/src/dorkbox/bytes/ByteBufInput.kt @@ -42,6 +42,7 @@ import java.io.InputStream * * Modified from KRYO ByteBufferInput to use ByteBuf instead of ByteBuffer. */ +@Suppress("NAME_SHADOWING") class ByteBufInput : Input { companion object { /** diff --git a/src/dorkbox/bytes/ByteBufOutput.kt b/src/dorkbox/bytes/ByteBufOutput.kt index ad0f17c..2ad256b 100644 --- a/src/dorkbox/bytes/ByteBufOutput.kt +++ b/src/dorkbox/bytes/ByteBufOutput.kt @@ -63,7 +63,7 @@ import java.io.OutputStream * * Modified from KRYO to use ByteBuf. */ -@Suppress("MemberVisibilityCanBePrivate") +@Suppress("MemberVisibilityCanBePrivate", "NAME_SHADOWING") class ByteBufOutput : Output { companion object { /** @@ -806,7 +806,7 @@ class ByteBufOutput : Output { require(count shl 1) val n = offset + count while (offset < n) { - val value = array[offset].toInt() + val value = array[offset].code byteBuf.writeByte(value.toByte().toInt()) byteBuf.writeByte((value ushr 8).toByte().toInt()) offset++