Code polish/cleanup

This commit is contained in:
Robinson 2021-04-25 22:01:50 +02:00
parent 97eb299ed4
commit f3637219c3
2 changed files with 196 additions and 193 deletions

View File

@ -45,52 +45,40 @@ import java.io.InputStream
/** /**
* An [InputStream] which reads data from a [DirectBuffer]. * An [InputStream] which reads data from a [DirectBuffer].
* *
*
* A read operation against this stream will occur at the `readerIndex` * A read operation against this stream will occur at the `readerIndex`
* of its underlying buffer and the `readerIndex` will increase during * of its underlying buffer and the `readerIndex` will increase during
* the read operation. * the read operation.
* *
*
* This stream implements [DataInput] for your convenience.
* The endianness of the stream is not always big endian but depends on * The endianness of the stream is not always big endian but depends on
* the endianness of the underlying buffer. * the endianness of the underlying buffer.
* *
*
* Utility methods are provided for efficiently reading primitive types and strings. * Utility methods are provided for efficiently reading primitive types and strings.
* *
*
*
*
*
*
*
*
* Modified from KRYO ByteBufferInput to use ByteBuf instead of ByteBuffer. * Modified from KRYO ByteBufferInput to use ByteBuf instead of ByteBuffer.
*/ */
class AeronInput class AeronInput
/** Creates an uninitialized Input, [.setBuffer] must be called before the Input is used. */ /** Creates an uninitialized Input, [.setBuffer] must be called before the Input is used. */
() : Input() { () : Input() {
/** Returns the buffer. The bytes between zero and [.position] are the data that has been read. */
/** the buffer. The bytes between zero and [position] are the data that has been read. */
var internalBuffer: DirectBuffer? = null var internalBuffer: DirectBuffer? = null
private set private set
/** /**
* Creates a new Input for reading from a [DirectBuffer] which is filled with the specified bytes. * Creates a new Input for reading from a [DirectBuffer] which is filled with the specified bytes.
* @see .setBuffer *
* @see [setBuffer]
*/ */
/** Creates a new Input for reading from a byteArray which is filled with the specified bytes. */
@JvmOverloads
constructor(bytes: ByteArray, offset: Int = 0, length: Int = bytes.size) : this() { constructor(bytes: ByteArray, offset: Int = 0, length: Int = bytes.size) : this() {
setBuffer(bytes, offset, length) setBuffer(bytes, offset, length)
} }
/** /**
* Creates a new Input for reading from a [DirectBuffer] which is filled with the specified bytes. * Creates a new Input for reading from a [DirectBuffer] which is filled with the specified bytes.
* @see .setBuffer *
* @see [setBuffer]
*/ */
/**
* Creates a new Input for reading from a [DirectBuffer] which is filled with the specified bytes.
* @see .setBuffer
*/
@JvmOverloads
constructor(buffer: DirectBuffer, offset: Int = 0, length: Int = buffer.capacity()) : this() { constructor(buffer: DirectBuffer, offset: Int = 0, length: Int = buffer.capacity()) : this() {
setBuffer(buffer, offset, length) setBuffer(buffer, offset, length)
} }
@ -98,13 +86,14 @@ class AeronInput
/** /**
* Throws [UnsupportedOperationException] because this input uses a DirectBuffer, not a byte[]. * Throws [UnsupportedOperationException] because this input uses a DirectBuffer, not a byte[].
*/ */
@Deprecated("") @Deprecated("This input does not use a byte[]", ReplaceWith("internalBuffer"))
override fun getBuffer(): ByteArray { override fun getBuffer(): ByteArray {
throw UnsupportedOperationException("This input does not use a byte[], see #getInternalBuffer().") throw UnsupportedOperationException("This input does not use a byte[], see #internalBuffer.")
} }
/** Sets a new buffer. The offset is 0 and the count is the buffer's length. /** Sets a new buffer. The offset is 0 and the count is the buffer's length.
* @see .setBuffer *
* @see [setBuffer]
*/ */
override fun setBuffer(bytes: ByteArray) { override fun setBuffer(bytes: ByteArray) {
setBuffer(bytes, 0, bytes.size) setBuffer(bytes, 0, bytes.size)
@ -120,8 +109,9 @@ class AeronInput
capacity = count capacity = count
} }
@Deprecated("This input does not use an inputStream", ReplaceWith("setByteBuf(buffer)"))
override fun setInputStream(inputStream: InputStream) { override fun setInputStream(inputStream: InputStream) {
throw UnsupportedOperationException("This input does not use a inputStream, see #setByteBuf().") throw UnsupportedOperationException("This input does not use an inputStream, see setByteBuf().")
} }
/** /**
@ -188,13 +178,13 @@ class AeronInput
@Throws(KryoException::class) @Throws(KryoException::class)
override fun read(bytes: ByteArray, offset: Int, count: Int): Int { override fun read(bytes: ByteArray, offset: Int, count: Int): Int {
var count = count var newCount = count
if (position + count > limit) { if (position + newCount > limit) {
count = limit - position newCount = limit - position
} }
internalBuffer!!.getBytes(position, bytes, offset, count) internalBuffer!!.getBytes(position, bytes, offset, newCount)
position += count position += newCount
return count return newCount
} }
override fun setPosition(position: Int) { override fun setPosition(position: Int) {
@ -465,6 +455,7 @@ class AeronInput
0 -> return null 0 -> return null
1 -> return "" 1 -> return ""
} }
charCount-- // make count adjustment charCount-- // make count adjustment
readUtf8Chars(charCount) readUtf8Chars(charCount)
return String(chars, 0, charCount) return String(chars, 0, charCount)
@ -472,12 +463,14 @@ class AeronInput
override fun readStringBuilder(): StringBuilder? { override fun readStringBuilder(): StringBuilder? {
if (!readVarIntFlag()) return StringBuilder(readAsciiString()) // ASCII. if (!readVarIntFlag()) return StringBuilder(readAsciiString()) // ASCII.
// Null, empty, or UTF8. // Null, empty, or UTF8.
var charCount = readVarIntFlag(true) var charCount = readVarIntFlag(true)
when (charCount) { when (charCount) {
0 -> return null 0 -> return null
1 -> return StringBuilder("") 1 -> return StringBuilder("")
} }
charCount-- charCount--
readUtf8Chars(charCount) readUtf8Chars(charCount)
val builder = StringBuilder(charCount) val builder = StringBuilder(charCount)
@ -506,7 +499,8 @@ class AeronInput
val chars = chars val chars = chars
val byteBuf = internalBuffer val byteBuf = internalBuffer
var charCount = 0 var charCount = 0
val n = Math.min(chars.size, limit - position) val n = chars.size.coerceAtMost(limit - position)
while (charCount < n) { while (charCount < n) {
val b = byteBuf!!.getByte(position++).toInt() val b = byteBuf!!.getByte(position++).toInt()
if (b and 0x80 == 0x80) { if (b and 0x80 == 0x80) {
@ -516,45 +510,48 @@ class AeronInput
chars[charCount] = b.toChar() chars[charCount] = b.toChar()
charCount++ charCount++
} }
return readAscii_slow(charCount) return readAscii_slow(charCount)
} }
private fun readAscii_slow(charCount: Int): String { private fun readAscii_slow(charCount: Int): String {
var charCount = charCount var count = charCount
var chars = chars var chars = chars
val byteBuf = internalBuffer val byteBuf = internalBuffer
while (true) { while (true) {
val b = byteBuf!!.getByte(position++).toInt() val b = byteBuf!!.getByte(position++).toInt()
if (charCount == chars.size) { if (count == chars.size) {
val newChars = CharArray(charCount * 2) val newChars = CharArray(count * 2)
System.arraycopy(chars, 0, newChars, 0, charCount) System.arraycopy(chars, 0, newChars, 0, count)
chars = newChars chars = newChars
this.chars = newChars this.chars = newChars
} }
if (b and 0x80 == 0x80) { if (b and 0x80 == 0x80) {
chars[charCount] = (b and 0x7F).toChar() chars[count] = (b and 0x7F).toChar()
return String(chars, 0, charCount + 1) return String(chars, 0, count + 1)
} }
chars[charCount++] = b.toChar() chars[count++] = b.toChar()
} }
} }
private fun readUtf8Chars_slow(charCount: Int, charIndex: Int) { private fun readUtf8Chars_slow(charCount: Int, charIndex: Int) {
var charIndex = charIndex var index = charIndex
val byteBuf = internalBuffer val byteBuf = internalBuffer
val chars = chars val chars = chars
while (charIndex < charCount) {
while (index < charCount) {
val b: Int = byteBuf!!.getByte(position++).toInt() and 0xFF val b: Int = byteBuf!!.getByte(position++).toInt() and 0xFF
when (b shr 4) { when (b shr 4) {
0, 1, 2, 3, 4, 5, 6, 7 -> chars[charIndex] = b.toChar() 0, 1, 2, 3, 4, 5, 6, 7 -> chars[index] = b.toChar()
12, 13 -> chars[charIndex] = (b and 0x1F shl 6 or (byteBuf.getByte(position++).toInt() and 0x3F)).toChar() 12, 13 -> chars[index] = (b and 0x1F shl 6 or (byteBuf.getByte(position++).toInt() and 0x3F)).toChar()
14 -> { 14 -> {
val b2 = byteBuf.getByte(position++).toInt() val b2 = byteBuf.getByte(position++).toInt()
val b3 = byteBuf.getByte(position++).toInt() val b3 = byteBuf.getByte(position++).toInt()
chars[charIndex] = (b and 0x0F shl 12 or (b2 and 0x3F shl 6) or (b3 and 0x3F)).toChar() chars[index] = (b and 0x0F shl 12 or (b2 and 0x3F shl 6) or (b3 and 0x3F)).toChar()
} }
} }
charIndex++ index++
} }
} }

View File

@ -44,33 +44,30 @@ import java.io.OutputStream
/** /**
* An [OutputStream] which writes data to a [MutableDirectBuffer]. * An [OutputStream] which writes data to a [MutableDirectBuffer].
* *
*
* A write operation against this stream will occur at the `writerIndex` * A write operation against this stream will occur at the `writerIndex`
* of its underlying buffer and the `writerIndex` will increase during * of its underlying buffer and the `writerIndex` will increase during
* the write operation. * the write operation.
* *
*
* This stream implements [DataOutput] for your convenience.
* The endianness of the stream is not always big endian but depends on * The endianness of the stream is not always big endian but depends on
* the endianness of the underlying buffer. * the endianness of the underlying buffer.
* *
*
*
* Utility methods are provided for efficiently reading primitive types and strings. * Utility methods are provided for efficiently reading primitive types and strings.
* *
* Modified from KRYO to use ByteBuf. * Modified from KRYO to use ByteBuf.
*/ */
class AeronOutput : Output { class AeronOutput : Output {
/** Returns the buffer. The bytes between zero and [.position] are the data that has been written. */ /** Returns the buffer. The bytes between zero and [.position] are the data that has been written. */
// NOTE: capacity IS NOT USED! // NOTE: capacity IS NOT USED!
var internalBuffer: MutableDirectBuffer var internalBuffer: MutableDirectBuffer
private set private set
/** /**
* Creates a new Output for writing to a direct [MutableDirectBuffer]. * Creates a new Output for writing to a direct [MutableDirectBuffer].
* *
* @param bufferSize The size of the buffer. * @param bufferSize The size of the buffer.
*/ */
/** Creates a new Output for writing to a direct [MutableDirectBuffer]. */
@JvmOverloads @JvmOverloads
constructor(bufferSize: Int = 32) { constructor(bufferSize: Int = 32) {
require(bufferSize >= 0) { "bufferSize must be >= 0!" } require(bufferSize >= 0) { "bufferSize must be >= 0!" }
@ -80,9 +77,10 @@ class AeronOutput : Output {
/** /**
* Creates a new Output for writing to a byte[]. * Creates a new Output for writing to a byte[].
* @see .setBuffer *
* @see [setBuffer]
*/ */
constructor(buffer: ByteArray?) { constructor(buffer: ByteArray) {
internalBuffer = UnsafeBuffer(buffer) internalBuffer = UnsafeBuffer(buffer)
position = 0 position = 0
capacity = internalBuffer.capacity() capacity = internalBuffer.capacity()
@ -103,9 +101,10 @@ class AeronOutput : Output {
/** /**
* Throws [UnsupportedOperationException] because this output uses a ByteBuffer, not a byte[]. * Throws [UnsupportedOperationException] because this output uses a ByteBuffer, not a byte[].
* @see .getInternalBuffer *
* @see [internalBuffer]
*/ */
@Deprecated(" ") @Deprecated("This buffer does not used a byte[]")
override fun getBuffer(): ByteArray { override fun getBuffer(): ByteArray {
throw UnsupportedOperationException("This buffer does not used a byte[], see #getInternaleBuffer().") throw UnsupportedOperationException("This buffer does not used a byte[], see #getInternaleBuffer().")
} }
@ -131,7 +130,7 @@ class AeronOutput : Output {
/** /**
* Sets a new buffer to write to. The max size is the buffer's length. * Sets a new buffer to write to. The max size is the buffer's length.
*/ */
@Deprecated("") @Deprecated("maxBufferSize parameter is ignored", ReplaceWith("setBuffer(buffer)"))
override fun setBuffer(buffer: ByteArray, maxBufferSize: Int) { override fun setBuffer(buffer: ByteArray, maxBufferSize: Int) {
setBuffer(buffer) setBuffer(buffer)
} }
@ -215,76 +214,78 @@ class AeronOutput : Output {
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeVarInt(value: Int, optimizePositive: Boolean): Int { override fun writeVarInt(value: Int, optimizePositive: Boolean): Int {
var value = value var newValue = value
if (!optimizePositive) value = value shl 1 xor (value shr 31) if (!optimizePositive) newValue = newValue shl 1 xor (newValue shr 31)
if (value ushr 7 == 0) { if (newValue ushr 7 == 0) {
internalBuffer.putByte(position++, value.toByte()) internalBuffer.putByte(position++, newValue.toByte())
return 1 return 1
} }
if (value ushr 14 == 0) { if (newValue ushr 14 == 0) {
internalBuffer.putByte(position++, (value and 0x7F or 0x80).toByte()) internalBuffer.putByte(position++, (newValue and 0x7F or 0x80).toByte())
internalBuffer.putByte(position++, (value ushr 7).toByte()) internalBuffer.putByte(position++, (newValue ushr 7).toByte())
return 2 return 2
} }
if (value ushr 21 == 0) { if (newValue ushr 21 == 0) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14).toByte()) byteBuf.putByte(position++, (newValue ushr 14).toByte())
return 3 return 3
} }
if (value ushr 28 == 0) { if (newValue ushr 28 == 0) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21).toByte()) byteBuf.putByte(position++, (newValue ushr 21).toByte())
return 4 return 4
} }
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 21 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 28).toByte()) byteBuf.putByte(position++, (newValue ushr 28).toByte())
return 5 return 5
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeVarIntFlag(flag: Boolean, value: Int, optimizePositive: Boolean): Int { override fun writeVarIntFlag(flag: Boolean, value: Int, optimizePositive: Boolean): Int {
var value = value var newValue = value
if (!optimizePositive) value = value shl 1 xor (value shr 31)
val first = value and 0x3F or if (flag) 0x80 else 0 // Mask first 6 bits, bit 8 is the flag. if (!optimizePositive) newValue = newValue shl 1 xor (newValue shr 31)
if (value ushr 6 == 0) {
val first = newValue and 0x3F or if (flag) 0x80 else 0 // Mask first 6 bits, bit 8 is the flag.
if (newValue ushr 6 == 0) {
internalBuffer.putByte(position++, first.toByte()) internalBuffer.putByte(position++, first.toByte())
return 1 return 1
} }
if (value ushr 13 == 0) { if (newValue ushr 13 == 0) {
internalBuffer.putByte(position++, (first or 0x40).toByte()) // Set bit 7. internalBuffer.putByte(position++, (first or 0x40).toByte()) // Set bit 7.
internalBuffer.putByte(position++, (value ushr 6).toByte()) internalBuffer.putByte(position++, (newValue ushr 6).toByte())
return 2 return 2
} }
if (value ushr 20 == 0) { if (newValue ushr 20 == 0) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (first or 0x40).toByte()) // Set bit 7. byteBuf.putByte(position++, (first or 0x40).toByte()) // Set bit 7.
byteBuf.putByte(position++, (value ushr 6 or 0x80).toByte()) // Set bit 8. byteBuf.putByte(position++, (newValue ushr 6 or 0x80).toByte()) // Set bit 8.
byteBuf.putByte(position++, (value ushr 13).toByte()) byteBuf.putByte(position++, (newValue ushr 13).toByte())
return 3 return 3
} }
if (value ushr 27 == 0) { if (newValue ushr 27 == 0) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (first or 0x40).toByte()) // Set bit 7. byteBuf.putByte(position++, (first or 0x40).toByte()) // Set bit 7.
byteBuf.putByte(position++, (value ushr 6 or 0x80).toByte()) // Set bit 8. byteBuf.putByte(position++, (newValue ushr 6 or 0x80).toByte()) // Set bit 8.
byteBuf.putByte(position++, (value ushr 13 or 0x80).toByte()) // Set bit 8. byteBuf.putByte(position++, (newValue ushr 13 or 0x80).toByte()) // Set bit 8.
byteBuf.putByte(position++, (value ushr 20).toByte()) byteBuf.putByte(position++, (newValue ushr 20).toByte())
return 4 return 4
} }
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (first or 0x40).toByte()) // Set bit 7. byteBuf.putByte(position++, (first or 0x40).toByte()) // Set bit 7.
byteBuf.putByte(position++, (value ushr 6 or 0x80).toByte()) // Set bit 8. byteBuf.putByte(position++, (newValue ushr 6 or 0x80).toByte()) // Set bit 8.
byteBuf.putByte(position++, (value ushr 13 or 0x80).toByte()) // Set bit 8. byteBuf.putByte(position++, (newValue ushr 13 or 0x80).toByte()) // Set bit 8.
byteBuf.putByte(position++, (value ushr 20 or 0x80).toByte()) // Set bit 8. byteBuf.putByte(position++, (newValue ushr 20 or 0x80).toByte()) // Set bit 8.
byteBuf.putByte(position++, (value ushr 27).toByte()) byteBuf.putByte(position++, (newValue ushr 27).toByte())
return 5 return 5
} }
@ -297,84 +298,86 @@ class AeronOutput : Output {
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeVarLong(value: Long, optimizePositive: Boolean): Int { override fun writeVarLong(value: Long, optimizePositive: Boolean): Int {
var value = value var newValue = value
if (!optimizePositive) value = value shl 1 xor (value shr 63)
if (value ushr 7 == 0L) { if (!optimizePositive) newValue = newValue shl 1 xor (newValue shr 63)
internalBuffer.putByte(position++, value.toByte())
if (newValue ushr 7 == 0L) {
internalBuffer.putByte(position++, newValue.toByte())
return 1 return 1
} }
if (value ushr 14 == 0L) { if (newValue ushr 14 == 0L) {
internalBuffer.putByte(position++, (value and 0x7F or 0x80).toByte()) internalBuffer.putByte(position++, (newValue and 0x7F or 0x80).toByte())
internalBuffer.putByte(position++, (value ushr 7).toByte()) internalBuffer.putByte(position++, (newValue ushr 7).toByte())
return 2 return 2
} }
if (value ushr 21 == 0L) { if (newValue ushr 21 == 0L) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14).toByte()) byteBuf.putByte(position++, (newValue ushr 14).toByte())
return 3 return 3
} }
if (value ushr 28 == 0L) { if (newValue ushr 28 == 0L) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21).toByte()) byteBuf.putByte(position++, (newValue ushr 21).toByte())
return 4 return 4
} }
if (value ushr 35 == 0L) { if (newValue ushr 35 == 0L) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 21 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 28).toByte()) byteBuf.putByte(position++, (newValue ushr 28).toByte())
return 5 return 5
} }
if (value ushr 42 == 0L) { if (newValue ushr 42 == 0L) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 21 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 28 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 28 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 35).toByte()) byteBuf.putByte(position++, (newValue ushr 35).toByte())
return 6 return 6
} }
if (value ushr 49 == 0L) { if (newValue ushr 49 == 0L) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 21 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 28 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 28 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 35 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 35 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 42).toByte()) byteBuf.putByte(position++, (newValue ushr 42).toByte())
return 7 return 7
} }
if (value ushr 56 == 0L) { if (newValue ushr 56 == 0L) {
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 21 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 28 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 28 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 35 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 35 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 42 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 42 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 49).toByte()) byteBuf.putByte(position++, (newValue ushr 49).toByte())
return 8 return 8
} }
val byteBuf = internalBuffer val byteBuf = internalBuffer
byteBuf.putByte(position++, (value and 0x7F or 0x80).toByte()) byteBuf.putByte(position++, (newValue and 0x7F or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 7 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 7 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 14 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 14 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 21 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 21 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 28 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 28 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 35 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 35 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 42 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 42 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 49 or 0x80).toByte()) byteBuf.putByte(position++, (newValue ushr 49 or 0x80).toByte())
byteBuf.putByte(position++, (value ushr 56).toByte()) byteBuf.putByte(position++, (newValue ushr 56).toByte())
return 9 return 9
} }
@ -419,6 +422,8 @@ class AeronOutput : Output {
writeByte(0x80) // 0 means null, bit 8 means UTF8. writeByte(0x80) // 0 means null, bit 8 means UTF8.
return return
} }
val charCount = value.length val charCount = value.length
if (charCount == 0) { if (charCount == 0) {
writeByte(1 or 0x80) // 1 means empty string, bit 8 means UTF8. writeByte(1 or 0x80) // 1 means empty string, bit 8 means UTF8.
@ -427,7 +432,7 @@ class AeronOutput : Output {
// Detect ASCII, we only do this for small strings, but ONLY more than 1 char. // Detect ASCII, we only do this for small strings, but ONLY more than 1 char.
// since 1 char is used for bit-masking if we use for 1 char string, reading the string will not work! // since 1 char is used for bit-masking if we use for 1 char string, reading the string will not work!
var permitAscii = charCount > 1 && charCount <= 32 var permitAscii = charCount in 2..32
if (permitAscii) { if (permitAscii) {
for (i in 0 until charCount) { for (i in 0 until charCount) {
if (value[i].toInt() > 127) { if (value[i].toInt() > 127) {
@ -462,7 +467,7 @@ class AeronOutput : Output {
return return
} }
} }
if (charIndex < charCount) writeUtf8_slow(value, charCount, charIndex) if (charIndex < charCount) writeUtf8Slow(value, charCount, charIndex)
} }
@Throws(KryoException::class) @Throws(KryoException::class)
@ -484,13 +489,14 @@ class AeronOutput : Output {
byteBuf.putByte(position++, value[i].toByte()) byteBuf.putByte(position++, value[i].toByte())
++i ++i
} }
byteBuf.putByte(position - 1, (byteBuf.getByte(position - 1).toInt() or 0x80) as Byte) // Bit 8 means end of ASCII. byteBuf.putByte(position - 1, (byteBuf.getByte(position - 1).toInt() or 0x80).toByte()) // Bit 8 means end of ASCII.
} }
private fun writeUtf8_slow(value: String, charCount: Int, charIndex: Int) { private fun writeUtf8Slow(value: String, charCount: Int, charIndex: Int) {
var charIndex = charIndex var index = charIndex
while (charIndex < charCount) { while (index < charCount) {
val c = value[charIndex].toInt() val c = value[index].toInt()
if (c <= 0x007F) { if (c <= 0x007F) {
internalBuffer.putByte(position++, c.toByte()) internalBuffer.putByte(position++, c.toByte())
} else if (c > 0x07FF) { } else if (c > 0x07FF) {
@ -501,84 +507,84 @@ class AeronOutput : Output {
internalBuffer.putByte(position++, (0xC0 or (c shr 6 and 0x1F)).toByte()) internalBuffer.putByte(position++, (0xC0 or (c shr 6 and 0x1F)).toByte())
internalBuffer.putByte(position++, (0x80 or (c and 0x3F)).toByte()) internalBuffer.putByte(position++, (0x80 or (c and 0x3F)).toByte())
} }
charIndex++ index++
} }
} }
// Primitive arrays: // Primitive arrays:
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeInts(array: IntArray, offset: Int, count: Int) { override fun writeInts(array: IntArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
val value = array[offset] val value = array[newOffset]
writeInt(value) writeInt(value)
offset++ newOffset++
} }
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeLongs(array: LongArray, offset: Int, count: Int) { override fun writeLongs(array: LongArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
val value = array[offset] val value = array[newOffset]
writeLong(value) writeLong(value)
offset++ newOffset++
} }
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeFloats(array: FloatArray, offset: Int, count: Int) { override fun writeFloats(array: FloatArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
val value = java.lang.Float.floatToIntBits(array[offset]) val value = java.lang.Float.floatToIntBits(array[newOffset])
writeFloat(value.toFloat()) writeFloat(value.toFloat())
offset++ newOffset++
} }
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeDoubles(array: DoubleArray, offset: Int, count: Int) { override fun writeDoubles(array: DoubleArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
val value = java.lang.Double.doubleToLongBits(array[offset]) val value = java.lang.Double.doubleToLongBits(array[newOffset])
writeDouble(value.toDouble()) writeDouble(value.toDouble())
offset++ newOffset++
} }
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeShorts(array: ShortArray, offset: Int, count: Int) { override fun writeShorts(array: ShortArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
val value = array[offset].toInt() val value = array[newOffset].toInt()
writeShort(value) writeShort(value)
offset++ newOffset++
} }
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeChars(array: CharArray, offset: Int, count: Int) { override fun writeChars(array: CharArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
val value = array[offset].toInt() val value = array[newOffset].toInt()
writeChar(value.toChar()) writeChar(value.toChar())
offset++ newOffset++
} }
} }
@Throws(KryoException::class) @Throws(KryoException::class)
override fun writeBooleans(array: BooleanArray, offset: Int, count: Int) { override fun writeBooleans(array: BooleanArray, offset: Int, count: Int) {
var offset = offset var newOffset = offset
val n = offset + count val n = newOffset + count
while (offset < n) { while (newOffset < n) {
internalBuffer.putByte(position++, if (array[offset]) 1.toByte() else 0) internalBuffer.putByte(position++, if (array[newOffset]) 1.toByte() else 0)
offset++ newOffset++
} }
} }
} }