Code cleanup
This commit is contained in:
parent
4094c12d16
commit
dbf08c542d
|
@ -40,7 +40,7 @@ import java.nio.BufferUnderflowException
|
|||
import java.util.*
|
||||
import kotlin.experimental.and
|
||||
|
||||
/**
|
||||
/**
|
||||
* A self-growing byte array wrapper.
|
||||
*
|
||||
* Utility methods are provided for efficiently writing primitive types and strings.
|
||||
|
@ -51,7 +51,7 @@ import kotlin.experimental.and
|
|||
* @author Nathan Sweet <misc></misc>@n4te.com>
|
||||
*/
|
||||
@Suppress("unused", "DuplicatedCode", "DuplicatedCode", "MemberVisibilityCanBePrivate")
|
||||
class ByteBuffer2 {
|
||||
class ByteArrayBuffer {
|
||||
companion object {
|
||||
/**
|
||||
* Gets the version number.
|
||||
|
@ -60,7 +60,7 @@ class ByteBuffer2 {
|
|||
|
||||
init {
|
||||
// Add this project to the updates system, which verifies this class + UUID + version information
|
||||
dorkbox.updates.Updates.add(ByteBuffer2::class.java, "f176cecea06e48e1a96d59c08a6e98c3", BytesInfo.version)
|
||||
dorkbox.updates.Updates.add(ByteArrayBuffer::class.java, "f176cecea06e48e1a96d59c08a6e98c3", BytesInfo.version)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -658,8 +658,8 @@ class ByteBuffer2 {
|
|||
// string
|
||||
/**
|
||||
* Writes the length and string, or null. Short strings are checked and if ASCII they are written more efficiently,
|
||||
* else they are written as UTF8. If a string is known to be ASCII, [ByteBuffer2.writeAscii] may be used. The
|
||||
* string can be read using [ByteBuffer2.readString] or [ByteBuffer2.readStringBuilder].
|
||||
* else they are written as UTF8. If a string is known to be ASCII, [ByteArrayBuffer.writeAscii] may be used. The
|
||||
* string can be read using [ByteArrayBuffer.readString] or [ByteArrayBuffer.readStringBuilder].
|
||||
*
|
||||
* @param value
|
||||
* May be null.
|
||||
|
@ -734,8 +734,8 @@ class ByteBuffer2 {
|
|||
}
|
||||
|
||||
/**
|
||||
* Writes the length and CharSequence as UTF8, or null. The string can be read using [ByteBuffer2.readString] or
|
||||
* [ByteBuffer2.readStringBuilder].
|
||||
* Writes the length and CharSequence as UTF8, or null. The string can be read using [ByteArrayBuffer.readString] or
|
||||
* [ByteArrayBuffer.readStringBuilder].
|
||||
*
|
||||
* @param value
|
||||
* May be null.
|
||||
|
@ -778,8 +778,8 @@ class ByteBuffer2 {
|
|||
/**
|
||||
* Writes a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be
|
||||
* corrupted. Each byte is a 7 bit character with the remaining byte denoting if another character is available.
|
||||
* This is slightly more efficient than [ByteBuffer2.writeString]. The string can be read using
|
||||
* [ByteBuffer2.readString] or [ByteBuffer2.readStringBuilder].
|
||||
* This is slightly more efficient than [ByteArrayBuffer.writeString]. The string can be read using
|
||||
* [ByteArrayBuffer.readString] or [ByteArrayBuffer.readStringBuilder].
|
||||
*
|
||||
* @param value
|
||||
* May be null.
|
||||
|
@ -897,8 +897,8 @@ class ByteBuffer2 {
|
|||
|
||||
/**
|
||||
* Reads the length and string of UTF8 characters, or null. This can read strings written by
|
||||
* [ByteBuffer2.writeString] , [ByteBuffer2.writeString], and
|
||||
* [ByteBuffer2.writeAscii].
|
||||
* [ByteArrayBuffer.writeString] , [ByteArrayBuffer.writeString], and
|
||||
* [ByteArrayBuffer.writeAscii].
|
||||
*
|
||||
* @return May be null.
|
||||
*/
|
||||
|
@ -1082,8 +1082,8 @@ class ByteBuffer2 {
|
|||
|
||||
/**
|
||||
* Reads the length and string of UTF8 characters, or null. This can read strings written by
|
||||
* [ByteBuffer2.writeString] , [ByteBuffer2.writeString], and
|
||||
* [ByteBuffer2.writeAscii].
|
||||
* [ByteArrayBuffer.writeString] , [ByteArrayBuffer.writeString], and
|
||||
* [ByteArrayBuffer.writeAscii].
|
||||
*
|
||||
* @return May be null.
|
||||
*/
|
||||
|
@ -1878,7 +1878,7 @@ class ByteBuffer2 {
|
|||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return if (other !is ByteBuffer2) {
|
||||
return if (other !is ByteArrayBuffer) {
|
||||
false
|
||||
} else Arrays.equals(bytes, other.bytes)
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2021 dorkbox, llc
|
||||
* 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.
|
||||
|
@ -15,8 +15,6 @@
|
|||
*/
|
||||
package dorkbox.bytes
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Necessary to provide equals and hashcode methods on a byte arrays, if they are to be used as keys in a map/set/etc
|
||||
*/
|
||||
|
@ -25,6 +23,7 @@ class ByteArrayWrapper(
|
|||
|
||||
/**
|
||||
* if TRUE, then the byteArray is copied. if FALSE, the byte array is used as-is.
|
||||
*
|
||||
* Using FALSE IS DANGEROUS!!!! If the underlying byte array is modified, this changes as well.
|
||||
*/
|
||||
copyBytes: Boolean = true
|
||||
|
@ -62,6 +61,7 @@ class ByteArrayWrapper(
|
|||
|
||||
init {
|
||||
val length = data.size
|
||||
|
||||
if (copyBytes) {
|
||||
bytes = ByteArray(length)
|
||||
// copy so it's immutable as a key.
|
||||
|
@ -75,7 +75,7 @@ class ByteArrayWrapper(
|
|||
// might be null for a thread because it's stale. who cares, get the value again
|
||||
var hashCode = hashCode
|
||||
if (hashCode == null) {
|
||||
hashCode = Arrays.hashCode(bytes)
|
||||
hashCode = bytes.contentHashCode()
|
||||
this.hashCode = hashCode
|
||||
}
|
||||
return hashCode
|
||||
|
@ -84,12 +84,12 @@ class ByteArrayWrapper(
|
|||
override fun equals(other: Any?): Boolean {
|
||||
return if (other !is ByteArrayWrapper) {
|
||||
false
|
||||
} else Arrays.equals(bytes, other.bytes)
|
||||
} else bytes.contentEquals(other.bytes)
|
||||
|
||||
// CANNOT be null, so we don't have to null check!
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "ByteArrayWrapper " + Arrays.toString(bytes)
|
||||
return "ByteArrayWrapper " + bytes.contentToString()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2010 dorkbox, llc
|
||||
* 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.
|
||||
|
@ -12,25 +12,6 @@
|
|||
* 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 (c) 2008, Nathan Sweet
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
|
||||
* conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
|
||||
* disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package dorkbox.bytes
|
||||
|
||||
|
@ -74,14 +55,16 @@ class ByteBufInput : Input {
|
|||
}
|
||||
}
|
||||
|
||||
var byteBuf: ByteBuf? = null
|
||||
lateinit var byteBuf: ByteBuf
|
||||
private set
|
||||
|
||||
private var initialReaderIndex = 0
|
||||
private var initialWriterIndex = 0
|
||||
private var tempBuffer: ByteArray? = null
|
||||
|
||||
/** Creates a new Input for reading from a direct [ByteBuf].
|
||||
/**
|
||||
* Creates a new Input for reading from a direct [ByteBuf].
|
||||
*
|
||||
* @param bufferSize The size of the buffer. An exception is thrown if more bytes than this are read and
|
||||
* [.fill] does not supply more bytes.
|
||||
*/
|
||||
|
@ -89,35 +72,39 @@ class ByteBufInput : Input {
|
|||
capacity = bufferSize
|
||||
byteBuf = Unpooled.buffer(bufferSize)
|
||||
}
|
||||
/** Creates a new Input for reading from a [ByteBuf] which is filled with the specified bytes.
|
||||
/**
|
||||
* Creates a new Input for reading from a [ByteBuf] which is filled with the specified bytes.
|
||||
*
|
||||
* @see .setBuffer
|
||||
*/
|
||||
/** Creates a new Input for reading from a [ByteBuf] which is filled with the specified bytes. */
|
||||
constructor(bytes: ByteArray?, offset: Int = 0, count: Int = bytes!!.size) {
|
||||
requireNotNull(bytes) { "bytes cannot be null." }
|
||||
constructor(bytes: ByteArray, offset: Int = 0, count: Int = bytes.size) {
|
||||
setBuffer(Unpooled.wrappedBuffer(bytes, offset, count))
|
||||
}
|
||||
|
||||
/** Creates a new Input for reading from a ByteBuffer. */
|
||||
constructor(buffer: ByteBuf?) {
|
||||
/**
|
||||
* Creates a new Input for reading from a ByteBuffer.
|
||||
*/
|
||||
constructor(buffer: ByteBuf) {
|
||||
setBuffer(buffer)
|
||||
}
|
||||
|
||||
/** @see Input.Input
|
||||
/**
|
||||
* @see Input.Input
|
||||
*/
|
||||
constructor(inputStream: InputStream?) : this(4096) {
|
||||
requireNotNull(inputStream) { "inputStream cannot be null." }
|
||||
constructor(inputStream: InputStream) : this(4096) {
|
||||
this.inputStream = inputStream
|
||||
}
|
||||
|
||||
/** @see Input.Input
|
||||
/**
|
||||
* @see Input.Input
|
||||
*/
|
||||
constructor(inputStream: InputStream?, bufferSize: Int) : this(bufferSize) {
|
||||
requireNotNull(inputStream) { "inputStream cannot be null." }
|
||||
constructor(inputStream: InputStream, bufferSize: Int) : this(bufferSize) {
|
||||
this.inputStream = inputStream
|
||||
}
|
||||
|
||||
/** Throws [UnsupportedOperationException] because this input uses a ByteBuffer, not a byte[].
|
||||
/**
|
||||
* Throws [UnsupportedOperationException] because this input uses a ByteBuffer, not a byte[].
|
||||
*
|
||||
* @see .getByteBuf
|
||||
*/
|
||||
@Deprecated("Use getByteBuf() instead",
|
||||
|
@ -127,32 +114,40 @@ class ByteBufInput : Input {
|
|||
throw UnsupportedOperationException("This input does not used a byte[], see #getByteBuf().")
|
||||
}
|
||||
|
||||
/** Throws [UnsupportedOperationException] because this input uses a ByteBuffer, not a byte[].
|
||||
/**
|
||||
* Throws [UnsupportedOperationException] because this input uses a ByteBuffer, not a byte[].
|
||||
*
|
||||
* @see .setBuffer
|
||||
*/
|
||||
@Deprecated("Use setByteBuf() instead",
|
||||
ReplaceWith("setByteBuf(ByteBuf)")
|
||||
ReplaceWith("setBuffer(ByteBuf)")
|
||||
)
|
||||
override fun setBuffer(bytes: ByteArray) {
|
||||
throw UnsupportedOperationException("This input does not used a byte[], see #setByteBuf(ByteBuf).")
|
||||
throw UnsupportedOperationException("This input does not used a byte[], see #setBuffer(ByteBuf).")
|
||||
}
|
||||
|
||||
/** Throws [UnsupportedOperationException] because this input uses a ByteBuffer, not a byte[].
|
||||
/**
|
||||
* Throws [UnsupportedOperationException] because this input uses a ByteBuffer, not a byte[].
|
||||
*
|
||||
* @see .setBuffer
|
||||
*/
|
||||
@Deprecated(" ")
|
||||
@Deprecated("This input does not used a byte[], see #setByteBuf().",
|
||||
ReplaceWith("setBuffer(ByteBuf)")
|
||||
)
|
||||
override fun setBuffer(bytes: ByteArray, offset: Int, count: Int) {
|
||||
throw UnsupportedOperationException("This input does not used a byte[], see #setByteBuf().")
|
||||
throw UnsupportedOperationException("This input does not used a byte[], see #setBuffer().")
|
||||
}
|
||||
|
||||
/** Sets a new buffer to read from. The bytes are not copied, the old buffer is discarded and the new buffer used in its place.
|
||||
/**
|
||||
* Sets a new buffer to read from. The bytes are not copied, the old buffer is discarded and the new buffer used in its place.
|
||||
*
|
||||
* The position, limit, and capacity are set to match the specified buffer. The total is reset. The
|
||||
* [InputStream][.setInputStream] is set to null. */
|
||||
fun setBuffer(buffer: ByteBuf?) {
|
||||
requireNotNull(buffer) { "buffer cannot be null." }
|
||||
* [InputStream][.setInputStream] is set to null.
|
||||
*/
|
||||
fun setBuffer(buffer: ByteBuf) {
|
||||
byteBuf = buffer
|
||||
initialReaderIndex = byteBuf!!.readerIndex() // where the object starts...
|
||||
initialWriterIndex = byteBuf!!.writerIndex() // where the object ends...
|
||||
initialReaderIndex = byteBuf.readerIndex() // where the object starts...
|
||||
initialWriterIndex = byteBuf.writerIndex() // where the object ends...
|
||||
position = initialReaderIndex
|
||||
limit = initialWriterIndex
|
||||
capacity = buffer.capacity()
|
||||
|
@ -168,7 +163,7 @@ class ByteBufInput : Input {
|
|||
|
||||
override fun reset() {
|
||||
super.reset()
|
||||
byteBuf!!.setIndex(initialReaderIndex, initialWriterIndex)
|
||||
byteBuf.setIndex(initialReaderIndex, initialWriterIndex)
|
||||
}
|
||||
|
||||
/** Fills the buffer with more bytes. The default implementation reads from the [InputStream][.getInputStream], if set.
|
||||
|
@ -278,7 +273,7 @@ class ByteBufInput : Input {
|
|||
override fun read(): Int {
|
||||
if (optional(1) <= 0) return -1
|
||||
position++
|
||||
return byteBuf!!.readByte().toInt() and 0xFF
|
||||
return byteBuf.readByte().toInt() and 0xFF
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
|
@ -294,7 +289,7 @@ class ByteBufInput : Input {
|
|||
val startingCount = count
|
||||
var copyCount = Math.min(limit - position, count)
|
||||
while (true) {
|
||||
byteBuf!!.getBytes(position, bytes, offset, copyCount)
|
||||
byteBuf.getBytes(position, bytes, offset, copyCount)
|
||||
position += copyCount
|
||||
count -= copyCount
|
||||
if (count == 0) break
|
||||
|
@ -312,18 +307,18 @@ class ByteBufInput : Input {
|
|||
|
||||
override fun setPosition(position: Int) {
|
||||
this.position = position
|
||||
byteBuf!!.readerIndex(position)
|
||||
byteBuf.readerIndex(position)
|
||||
}
|
||||
|
||||
override fun setLimit(limit: Int) {
|
||||
this.limit = limit
|
||||
byteBuf!!.writerIndex(limit)
|
||||
byteBuf.writerIndex(limit)
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
override fun skip(count: Int) {
|
||||
super.skip(count)
|
||||
byteBuf!!.readerIndex(position)
|
||||
byteBuf.readerIndex(position)
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
|
@ -352,14 +347,14 @@ class ByteBufInput : Input {
|
|||
override fun readByte(): Byte {
|
||||
if (position == limit) require(1)
|
||||
position++
|
||||
return byteBuf!!.readByte()
|
||||
return byteBuf.readByte()
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
override fun readByteUnsigned(): Int {
|
||||
if (position == limit) require(1)
|
||||
position++
|
||||
return byteBuf!!.readByte().toInt() and 0xFF
|
||||
return byteBuf.readByte().toInt() and 0xFF
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
|
@ -376,7 +371,7 @@ class ByteBufInput : Input {
|
|||
requireNotNull(bytes) { "bytes cannot be null." }
|
||||
var copyCount = Math.min(limit - position, count)
|
||||
while (true) {
|
||||
byteBuf!!.readBytes(bytes, offset, copyCount)
|
||||
byteBuf.readBytes(bytes, offset, copyCount)
|
||||
position += copyCount
|
||||
count -= copyCount
|
||||
if (count == 0) break
|
||||
|
@ -391,13 +386,13 @@ class ByteBufInput : Input {
|
|||
override fun readInt(): Int {
|
||||
require(4)
|
||||
position += 4
|
||||
return byteBuf!!.readInt()
|
||||
return byteBuf.readInt()
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
override fun readVarInt(optimizePositive: Boolean): Int {
|
||||
if (require(1) < 5) return readVarInt_slow(optimizePositive)
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
var b = byteBuf.readByte().toInt()
|
||||
var result = b and 0x7F
|
||||
if (b and 0x80 != 0) {
|
||||
|
@ -423,7 +418,7 @@ class ByteBufInput : Input {
|
|||
private fun readVarInt_slow(optimizePositive: Boolean): Int {
|
||||
// The buffer is guaranteed to have at least 1 byte.
|
||||
position++
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
var b = byteBuf.readByte().toInt()
|
||||
var result = b and 0x7F
|
||||
if (b and 0x80 != 0) {
|
||||
|
@ -459,7 +454,7 @@ class ByteBufInput : Input {
|
|||
if (optional(5) <= 0) return false
|
||||
var p = position
|
||||
val limit = limit
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
if (byteBuf.getByte(p++).toInt() and 0x80 == 0) return true
|
||||
if (p == limit) return false
|
||||
if (byteBuf.getByte(p++).toInt() and 0x80 == 0) return true
|
||||
|
@ -474,7 +469,7 @@ class ByteBufInput : Input {
|
|||
* advance the position. */
|
||||
override fun readVarIntFlag(): Boolean {
|
||||
if (position == limit) require(1)
|
||||
return byteBuf!!.getByte(position).toInt() and 0x80 != 0
|
||||
return byteBuf.getByte(position).toInt() and 0x80 != 0
|
||||
}
|
||||
|
||||
/** Reads the 1-5 byte int part of a varint flag. The position is advanced so if the boolean part is needed it should be read
|
||||
|
@ -482,7 +477,7 @@ class ByteBufInput : Input {
|
|||
override fun readVarIntFlag(optimizePositive: Boolean): Int {
|
||||
if (require(1) < 5) return readVarIntFlag_slow(optimizePositive)
|
||||
val byteBuf = byteBuf
|
||||
var b = byteBuf!!.readByte().toInt()
|
||||
var b = byteBuf.readByte().toInt()
|
||||
var result = b and 0x3F // Mask first 6 bits.
|
||||
if (b and 0x40 != 0) { // Bit 7 means another byte, bit 8 means UTF8.
|
||||
b = byteBuf.readByte().toInt()
|
||||
|
@ -507,13 +502,13 @@ class ByteBufInput : Input {
|
|||
private fun readVarIntFlag_slow(optimizePositive: Boolean): Int {
|
||||
// The buffer is guaranteed to have at least 1 byte.
|
||||
position++
|
||||
var b = byteBuf!!.readByte().toInt()
|
||||
var b = byteBuf.readByte().toInt()
|
||||
var result = b and 0x3F
|
||||
if (b and 0x40 != 0) {
|
||||
if (position == limit) require(1)
|
||||
position++
|
||||
val byteBuf = byteBuf
|
||||
b = byteBuf!!.readByte().toInt()
|
||||
b = byteBuf.readByte().toInt()
|
||||
result = result or (b and 0x7F shl 6)
|
||||
if (b and 0x80 != 0) {
|
||||
if (position == limit) require(1)
|
||||
|
@ -542,13 +537,13 @@ class ByteBufInput : Input {
|
|||
override fun readLong(): Long {
|
||||
require(8)
|
||||
position += 8
|
||||
return byteBuf!!.readLong()
|
||||
return byteBuf.readLong()
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
override fun readVarLong(optimizePositive: Boolean): Long {
|
||||
if (require(1) < 9) return readVarLong_slow(optimizePositive)
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
var b = byteBuf.readByte().toInt()
|
||||
var result = (b and 0x7F).toLong()
|
||||
if (b and 0x80 != 0) {
|
||||
|
@ -590,7 +585,7 @@ class ByteBufInput : Input {
|
|||
private fun readVarLong_slow(optimizePositive: Boolean): Long {
|
||||
// The buffer is guaranteed to have at least 1 byte.
|
||||
position++
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
var b = byteBuf.readByte().toInt()
|
||||
var result = (b and 0x7F).toLong()
|
||||
if (b and 0x80 != 0) {
|
||||
|
@ -651,7 +646,7 @@ class ByteBufInput : Input {
|
|||
var p = position
|
||||
val limit = limit
|
||||
val byteBuf = byteBuf
|
||||
if (byteBuf!!.getByte(p++).toInt() and 0x80 == 0) return true
|
||||
if (byteBuf.getByte(p++).toInt() and 0x80 == 0) return true
|
||||
if (p == limit) return false
|
||||
if (byteBuf.getByte(p++).toInt() and 0x80 == 0) return true
|
||||
if (p == limit) return false
|
||||
|
@ -675,7 +670,7 @@ class ByteBufInput : Input {
|
|||
require(4)
|
||||
val p = position
|
||||
position = p + 4
|
||||
return byteBuf!!.readFloat()
|
||||
return byteBuf.readFloat()
|
||||
}
|
||||
|
||||
// double:
|
||||
|
@ -684,7 +679,7 @@ class ByteBufInput : Input {
|
|||
require(8)
|
||||
val p = position
|
||||
position = p + 8
|
||||
return byteBuf!!.readDouble()
|
||||
return byteBuf.readDouble()
|
||||
}
|
||||
|
||||
// boolean:
|
||||
|
@ -692,7 +687,7 @@ class ByteBufInput : Input {
|
|||
override fun readBoolean(): Boolean {
|
||||
if (position == limit) require(1)
|
||||
position++
|
||||
return if (byteBuf!!.readByte().toInt() == 1) true else false
|
||||
return if (byteBuf.readByte().toInt() == 1) true else false
|
||||
}
|
||||
|
||||
// short:
|
||||
|
@ -700,14 +695,14 @@ class ByteBufInput : Input {
|
|||
override fun readShort(): Short {
|
||||
require(2)
|
||||
position += 2
|
||||
return byteBuf!!.readShort()
|
||||
return byteBuf.readShort()
|
||||
}
|
||||
|
||||
@Throws(KryoException::class)
|
||||
override fun readShortUnsigned(): Int {
|
||||
require(2)
|
||||
position += 2
|
||||
return byteBuf!!.readUnsignedShort()
|
||||
return byteBuf.readUnsignedShort()
|
||||
}
|
||||
|
||||
// char:
|
||||
|
@ -715,7 +710,7 @@ class ByteBufInput : Input {
|
|||
override fun readChar(): Char {
|
||||
require(2)
|
||||
position += 2
|
||||
return byteBuf!!.readChar()
|
||||
return byteBuf.readChar()
|
||||
}
|
||||
|
||||
// String:
|
||||
|
@ -752,7 +747,7 @@ class ByteBufInput : Input {
|
|||
if (chars.size < charCount) chars = CharArray(charCount)
|
||||
val chars = chars
|
||||
// Try to read 7 bit ASCII chars.
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
var charIndex = 0
|
||||
val count = require(1).coerceAtMost(charCount)
|
||||
while (charIndex < count) {
|
||||
|
@ -771,7 +766,7 @@ class ByteBufInput : Input {
|
|||
|
||||
private fun readUtf8Chars_slow(charCount: Int, charIndex: Int) {
|
||||
var index = charIndex
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
val chars = chars
|
||||
|
||||
while (index < charCount) {
|
||||
|
@ -804,7 +799,7 @@ class ByteBufInput : Input {
|
|||
var charCount = 0
|
||||
val n = Math.min(chars.size, limit - position)
|
||||
while (charCount < n) {
|
||||
val b = byteBuf!!.readByte().toInt()
|
||||
val b = byteBuf.readByte().toInt()
|
||||
if (b and 0x80 == 0x80) {
|
||||
position = byteBuf.readerIndex()
|
||||
chars[charCount] = (b and 0x7F).toChar()
|
||||
|
@ -813,7 +808,7 @@ class ByteBufInput : Input {
|
|||
chars[charCount] = b.toChar()
|
||||
charCount++
|
||||
}
|
||||
position = byteBuf!!.readerIndex()
|
||||
position = byteBuf.readerIndex()
|
||||
return readAscii_slow(charCount)
|
||||
}
|
||||
|
||||
|
@ -824,7 +819,7 @@ class ByteBufInput : Input {
|
|||
while (true) {
|
||||
if (position == limit) require(1)
|
||||
position++
|
||||
val b = byteBuf!!.readByte().toInt()
|
||||
val b = byteBuf.readByte().toInt()
|
||||
if (charCount == chars.size) {
|
||||
val newChars = CharArray(charCount * 2)
|
||||
System.arraycopy(chars, 0, newChars, 0, charCount)
|
||||
|
@ -846,9 +841,9 @@ class ByteBufInput : Input {
|
|||
if (optional(length shl 2) == length shl 2) {
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) {
|
||||
array[i] = byteBuf!!.readInt()
|
||||
array[i] = byteBuf.readInt()
|
||||
}
|
||||
position = byteBuf!!.readerIndex()
|
||||
position = byteBuf.readerIndex()
|
||||
} else {
|
||||
for (i in 0 until length) array[i] = readInt()
|
||||
}
|
||||
|
@ -859,7 +854,7 @@ class ByteBufInput : Input {
|
|||
override fun readLongs(length: Int): LongArray {
|
||||
val array = LongArray(length)
|
||||
if (optional(length shl 3) == length shl 3) {
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) {
|
||||
array[i] = byteBuf.readLong()
|
||||
}
|
||||
|
@ -874,7 +869,7 @@ class ByteBufInput : Input {
|
|||
override fun readFloats(length: Int): FloatArray {
|
||||
val array = FloatArray(length)
|
||||
if (optional(length shl 2) == length shl 2) {
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) {
|
||||
array[i] = byteBuf.readFloat()
|
||||
}
|
||||
|
@ -889,7 +884,7 @@ class ByteBufInput : Input {
|
|||
override fun readDoubles(length: Int): DoubleArray {
|
||||
val array = DoubleArray(length)
|
||||
if (optional(length shl 3) == length shl 3) {
|
||||
val byteBuf = byteBuf!!
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) {
|
||||
array[i] = byteBuf.readDouble()
|
||||
}
|
||||
|
@ -905,8 +900,8 @@ class ByteBufInput : Input {
|
|||
val array = ShortArray(length)
|
||||
if (optional(length shl 1) == length shl 1) {
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) array[i] = (byteBuf!!.readByte().toInt() and 0xFF or (byteBuf.readByte().toInt() and 0xFF shl 8)).toShort()
|
||||
position = byteBuf!!.readerIndex()
|
||||
for (i in 0 until length) array[i] = (byteBuf.readByte().toInt() and 0xFF or (byteBuf.readByte().toInt() and 0xFF shl 8)).toShort()
|
||||
position = byteBuf.readerIndex()
|
||||
} else {
|
||||
for (i in 0 until length) array[i] = readShort()
|
||||
}
|
||||
|
@ -918,8 +913,8 @@ class ByteBufInput : Input {
|
|||
val array = CharArray(length)
|
||||
if (optional(length shl 1) == length shl 1) {
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) array[i] = (byteBuf!!.readByte().toInt() and 0xFF or (byteBuf.readByte().toInt() and 0xFF shl 8)).toChar()
|
||||
position = byteBuf!!.readerIndex()
|
||||
for (i in 0 until length) array[i] = (byteBuf.readByte().toInt() and 0xFF or (byteBuf.readByte().toInt() and 0xFF shl 8)).toChar()
|
||||
position = byteBuf.readerIndex()
|
||||
} else {
|
||||
for (i in 0 until length) array[i] = readChar()
|
||||
}
|
||||
|
@ -931,8 +926,8 @@ class ByteBufInput : Input {
|
|||
val array = BooleanArray(length)
|
||||
if (optional(length) == length) {
|
||||
val byteBuf = byteBuf
|
||||
for (i in 0 until length) array[i] = byteBuf!!.readByte().toInt() != 0
|
||||
position = byteBuf!!.readerIndex()
|
||||
for (i in 0 until length) array[i] = byteBuf.readByte().toInt() != 0
|
||||
position = byteBuf.readerIndex()
|
||||
} else {
|
||||
for (i in 0 until length) array[i] = readBoolean()
|
||||
}
|
||||
|
|
|
@ -63,6 +63,7 @@ import java.io.OutputStream
|
|||
*
|
||||
* Modified from KRYO to use ByteBuf.
|
||||
*/
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
class ByteBufOutput : Output {
|
||||
companion object {
|
||||
/**
|
||||
|
@ -76,7 +77,9 @@ class ByteBufOutput : 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!
|
||||
lateinit var byteBuf: ByteBuf
|
||||
private set
|
||||
|
@ -84,14 +87,19 @@ class ByteBufOutput : Output {
|
|||
private var initialReaderIndex = 0
|
||||
private var initialWriterIndex = 0
|
||||
|
||||
/** Creates an uninitialized Output, [.setBuffer] must be called before the Output is used. */
|
||||
/**
|
||||
* Creates an uninitialized Output, [.setBuffer] must be called before the Output is used.
|
||||
*/
|
||||
constructor() {}
|
||||
|
||||
/** Creates a new Output for writing to a direct ByteBuffer.
|
||||
* @param bufferSize The size of the buffer. An exception is thrown if more bytes than this are written and [.flush]
|
||||
* * does not empty the buffer.
|
||||
* @param maxBufferSize If [.flush] does not empty the buffer, the buffer is doubled as needed until it exceeds
|
||||
* maxBufferSize and an exception is thrown. Can be -1 for no maximum.
|
||||
/**
|
||||
* Creates a new Output for writing to a direct ByteBuffer.
|
||||
*
|
||||
* @param bufferSize The size of the buffer. An exception is thrown if more bytes than this are written and [.flush] does not empty
|
||||
* the buffer.
|
||||
*
|
||||
* @param maxBufferSize If [.flush] does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and
|
||||
* an exception is thrown. Can be -1 for no maximum.
|
||||
*/
|
||||
constructor(bufferSize: Int, maxBufferSize: Int = bufferSize) {
|
||||
require(maxBufferSize >= -1) { "maxBufferSize cannot be < -1: $maxBufferSize" }
|
||||
|
@ -99,26 +107,32 @@ class ByteBufOutput : Output {
|
|||
byteBuf = Unpooled.buffer(bufferSize)
|
||||
}
|
||||
|
||||
/** Creates a new Output for writing to a ByteBuffer. */
|
||||
/**
|
||||
* Creates a new Output for writing to a ByteBuffer.
|
||||
*/
|
||||
constructor(buffer: ByteBuf) {
|
||||
setBuffer(buffer)
|
||||
}
|
||||
|
||||
/** Creates a new Output for writing to a ByteBuffer.
|
||||
* @param maxBufferSize If [.flush] does not empty the buffer, the buffer is doubled as needed until it exceeds
|
||||
* maxBufferSize and an exception is thrown. Can be -1 for no maximum.
|
||||
/**
|
||||
* Creates a new Output for writing to a ByteBuffer.
|
||||
*
|
||||
* @param maxBufferSize If [.flush] does not empty the buffer, the buffer is doubled as needed until it exceeds maxBufferSize and
|
||||
* an exception is thrown. Can be -1 for no maximum.
|
||||
*/
|
||||
constructor(buffer: ByteBuf, maxBufferSize: Int) {
|
||||
setBuffer(buffer, maxBufferSize)
|
||||
}
|
||||
|
||||
/** @see Output.Output
|
||||
/**
|
||||
* @see Output.Output
|
||||
*/
|
||||
constructor(outputStream: OutputStream) : this(4096, 4096) {
|
||||
this.outputStream = outputStream
|
||||
}
|
||||
|
||||
/** @see Output.Output
|
||||
/**
|
||||
* @see Output.Output
|
||||
*/
|
||||
constructor(outputStream: OutputStream, bufferSize: Int) : this(bufferSize, bufferSize) {
|
||||
this.outputStream = outputStream
|
||||
|
@ -128,49 +142,62 @@ class ByteBufOutput : Output {
|
|||
return outputStream
|
||||
}
|
||||
|
||||
/** Throws [UnsupportedOperationException] because this output uses a ByteBuffer, not a byte[].
|
||||
/**
|
||||
* Throws [UnsupportedOperationException] because this output uses a ByteBuffer, not a byte[].
|
||||
*
|
||||
* @see .getByteBuf
|
||||
*/
|
||||
@Deprecated(" ")
|
||||
@Deprecated("This buffer does not used a byte[], see #byteBuf.",
|
||||
ReplaceWith("byteBuf")
|
||||
)
|
||||
override fun getBuffer(): ByteArray {
|
||||
throw UnsupportedOperationException("This buffer does not used a byte[], see #getByteBuffer().")
|
||||
throw UnsupportedOperationException("This buffer does not used a byte[], see #byteBuf.")
|
||||
}
|
||||
|
||||
/** Throws [UnsupportedOperationException] because this output uses a ByteBuffer, not a byte[].
|
||||
/**
|
||||
* Throws [UnsupportedOperationException] because this output uses a ByteBuffer, not a byte[].
|
||||
*
|
||||
* @see .getByteBuf
|
||||
*/
|
||||
@Deprecated(" ")
|
||||
override fun setBuffer(buffer: ByteArray) {
|
||||
setBuffer(Unpooled.wrappedBuffer(buffer))
|
||||
}
|
||||
|
||||
/** Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.
|
||||
/**
|
||||
* Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.
|
||||
*
|
||||
* @see .setBuffer
|
||||
*/
|
||||
@Deprecated("")
|
||||
override fun setBuffer(buffer: ByteArray, maxBufferSize: Int) {
|
||||
setBuffer(Unpooled.wrappedBuffer(buffer))
|
||||
}
|
||||
|
||||
/** Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.
|
||||
/**
|
||||
* Allocates a new direct ByteBuffer with the specified bytes and sets it as the new buffer.
|
||||
*
|
||||
* @see .setBuffer
|
||||
*/
|
||||
fun setBuffer(bytes: ByteArray, offset: Int, count: Int) {
|
||||
setBuffer(Unpooled.wrappedBuffer(bytes, offset, count))
|
||||
}
|
||||
|
||||
/** 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.
|
||||
*
|
||||
* @see .setBuffer
|
||||
*/
|
||||
fun setBuffer(buffer: ByteBuf) {
|
||||
setBuffer(buffer, buffer.capacity())
|
||||
}
|
||||
|
||||
/** Sets a new buffer to write to. The bytes are not copied, the old buffer is discarded and the new buffer used in its place.
|
||||
/**
|
||||
* Sets a new buffer to write to. The bytes are not copied, the old buffer is discarded and the new buffer used in its place.
|
||||
*
|
||||
* The position and capacity are set to match the specified buffer. The total is reset. The
|
||||
* [OutputStream][.setOutputStream] is set to null.
|
||||
*
|
||||
* @param maxBufferSize If [.flush] does not empty the buffer, the buffer is doubled as needed until it exceeds
|
||||
* maxBufferSize and an exception is thrown. Can be -1 for no maximum.
|
||||
* maxBufferSize and an exception is thrown. Can be -1 for no maximum.
|
||||
*/
|
||||
fun setBuffer(buffer: ByteBuf, maxBufferSize: Int) {
|
||||
require(maxBufferSize >= -1) { "maxBufferSize cannot be < -1: $maxBufferSize" }
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2021 dorkbox, llc
|
||||
* 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.
|
||||
|
@ -21,10 +21,10 @@ import java.lang.reflect.Array
|
|||
import java.nio.ByteBuffer
|
||||
import java.util.*
|
||||
|
||||
class ByteBuffer2Test {
|
||||
class ByteArrayBufferTest {
|
||||
@Test
|
||||
fun testWriteBytes() {
|
||||
val buffer = ByteBuffer2(512)
|
||||
val buffer = ByteArrayBuffer(512)
|
||||
buffer.writeBytes(byteArrayOf(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26))
|
||||
buffer.writeBytes(byteArrayOf(31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46))
|
||||
buffer.writeByte(51)
|
||||
|
@ -44,12 +44,12 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testStrings() {
|
||||
runStringTest(ByteBuffer2(4096))
|
||||
runStringTest(ByteBuffer2(897))
|
||||
val write = ByteBuffer2(21)
|
||||
runStringTest(ByteArrayBuffer(4096))
|
||||
runStringTest(ByteArrayBuffer(897))
|
||||
val write = ByteArrayBuffer(21)
|
||||
val value = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234"
|
||||
write.writeString(value)
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
assertArrayEquals(value, read.readString())
|
||||
runStringTest(127)
|
||||
runStringTest(256)
|
||||
|
@ -60,7 +60,7 @@ class ByteBuffer2Test {
|
|||
runStringTest(1024 * 1024 * 2)
|
||||
}
|
||||
|
||||
fun runStringTest(write: ByteBuffer2) {
|
||||
fun runStringTest(write: ByteArrayBuffer) {
|
||||
val value1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\rabcdefghijklmnopqrstuvwxyz\n1234567890\t\"!`?'.,;:()[]{}<>|/@\\^$-%+=#_&~*"
|
||||
val value2 = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234"
|
||||
write.writeString("")
|
||||
|
@ -78,7 +78,7 @@ class ByteBuffer2Test {
|
|||
for (i in 0..126) {
|
||||
write.writeString(i.toChar().toString() + "abc")
|
||||
}
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals("", read.readString())
|
||||
Assert.assertEquals("1", read.readString())
|
||||
Assert.assertEquals("22", read.readString())
|
||||
|
@ -113,7 +113,7 @@ class ByteBuffer2Test {
|
|||
}
|
||||
|
||||
fun runStringTest(length: Int) {
|
||||
val write = ByteBuffer2(1024, -1)
|
||||
val write = ByteArrayBuffer(1024, -1)
|
||||
val buffer = StringBuilder()
|
||||
for (i in 0 until length) {
|
||||
buffer.append(i.toChar())
|
||||
|
@ -121,20 +121,20 @@ class ByteBuffer2Test {
|
|||
val value = buffer.toString()
|
||||
write.writeString(value)
|
||||
write.writeString(value)
|
||||
var read = ByteBuffer2(write.toBytes())
|
||||
var read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(value, read.readString())
|
||||
Assert.assertEquals(value, read.readStringBuilder().toString())
|
||||
write.clear()
|
||||
write.writeString(buffer)
|
||||
write.writeString(buffer)
|
||||
read = ByteBuffer2(write.toBytes())
|
||||
read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(value, read.readStringBuilder().toString())
|
||||
Assert.assertEquals(value, read.readString())
|
||||
if (length <= 127) {
|
||||
write.clear()
|
||||
write.writeAscii(value)
|
||||
write.writeAscii(value)
|
||||
read = ByteBuffer2(write.toBytes())
|
||||
read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(value, read.readStringBuilder().toString())
|
||||
Assert.assertEquals(value, read.readString())
|
||||
}
|
||||
|
@ -142,12 +142,12 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testCanReadInt() {
|
||||
var write = ByteBuffer2()
|
||||
var read = ByteBuffer2(write.toBytes())
|
||||
var write = ByteArrayBuffer()
|
||||
var read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(false, read.canReadInt())
|
||||
write = ByteBuffer2(4)
|
||||
write = ByteArrayBuffer(4)
|
||||
write.writeInt(400, true)
|
||||
read = ByteBuffer2(write.toBytes())
|
||||
read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(true, read.canReadInt())
|
||||
read.setPosition(read.capacity())
|
||||
Assert.assertEquals(false, read.canReadInt())
|
||||
|
@ -155,10 +155,10 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testInts() {
|
||||
runIntTest(ByteBuffer2(4096))
|
||||
runIntTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runIntTest(write: ByteBuffer2) {
|
||||
private fun runIntTest(write: ByteArrayBuffer) {
|
||||
write.writeInt(0)
|
||||
write.writeInt(63)
|
||||
write.writeInt(64)
|
||||
|
@ -220,7 +220,7 @@ class ByteBuffer2Test {
|
|||
Assert.assertEquals(5, write.writeInt(-134217728, true).toLong())
|
||||
Assert.assertEquals(5, write.writeInt(-134217729, false).toLong())
|
||||
Assert.assertEquals(5, write.writeInt(-134217729, true).toLong())
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(0, read.readInt().toLong())
|
||||
Assert.assertEquals(63, read.readInt().toLong())
|
||||
Assert.assertEquals(64, read.readInt().toLong())
|
||||
|
@ -302,10 +302,10 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testLongs() {
|
||||
runLongTest(ByteBuffer2(4096))
|
||||
runLongTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runLongTest(write: ByteBuffer2) {
|
||||
private fun runLongTest(write: ByteArrayBuffer) {
|
||||
write.writeLong(0)
|
||||
write.writeLong(63)
|
||||
write.writeLong(64)
|
||||
|
@ -367,7 +367,7 @@ class ByteBuffer2Test {
|
|||
Assert.assertEquals(9, write.writeLong(-134217728, true).toLong())
|
||||
Assert.assertEquals(5, write.writeLong(-134217729, false).toLong())
|
||||
Assert.assertEquals(9, write.writeLong(-134217729, true).toLong())
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(0, read.readLong())
|
||||
Assert.assertEquals(63, read.readLong())
|
||||
Assert.assertEquals(64, read.readLong())
|
||||
|
@ -445,10 +445,10 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testShorts() {
|
||||
runShortTest(ByteBuffer2(4096))
|
||||
runShortTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runShortTest(write: ByteBuffer2) {
|
||||
private fun runShortTest(write: ByteArrayBuffer) {
|
||||
write.writeShort(0)
|
||||
write.writeShort(63)
|
||||
write.writeShort(64)
|
||||
|
@ -464,7 +464,7 @@ class ByteBuffer2Test {
|
|||
write.writeShort(-8192)
|
||||
write.writeShort(-16384)
|
||||
write.writeShort(-32768)
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(0, read.readShort().toLong())
|
||||
Assert.assertEquals(63, read.readShort().toLong())
|
||||
Assert.assertEquals(64, read.readShort().toLong())
|
||||
|
@ -484,10 +484,10 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testFloats() {
|
||||
runFloatTest(ByteBuffer2(4096))
|
||||
runFloatTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runFloatTest(write: ByteBuffer2) {
|
||||
private fun runFloatTest(write: ByteArrayBuffer) {
|
||||
write.writeFloat(0f)
|
||||
write.writeFloat(63f)
|
||||
write.writeFloat(64f)
|
||||
|
@ -530,7 +530,7 @@ class ByteBuffer2Test {
|
|||
Assert.assertEquals(4, write.writeFloat(-8192f, 1000f, false).toLong())
|
||||
Assert.assertEquals(5, write.writeFloat(-8192f, 1000f, true).toLong())
|
||||
val delta = 0.00000001f
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(read.readFloat(), 0f, delta)
|
||||
Assert.assertEquals(read.readFloat(), 63f, delta)
|
||||
Assert.assertEquals(read.readFloat(), 64f, delta)
|
||||
|
@ -576,10 +576,10 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testDoubles() {
|
||||
runDoubleTest(ByteBuffer2(4096))
|
||||
runDoubleTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runDoubleTest(write: ByteBuffer2) {
|
||||
private fun runDoubleTest(write: ByteArrayBuffer) {
|
||||
write.writeDouble(0.0)
|
||||
write.writeDouble(63.0)
|
||||
write.writeDouble(64.0)
|
||||
|
@ -623,7 +623,7 @@ class ByteBuffer2Test {
|
|||
Assert.assertEquals(9, write.writeDouble(-8192.0, 1000.0, true).toLong())
|
||||
write.writeDouble(1.23456)
|
||||
val delta = 0.00000001
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(read.readDouble(), 0.0, delta)
|
||||
Assert.assertEquals(read.readDouble(), 63.0, delta)
|
||||
Assert.assertEquals(read.readDouble(), 64.0, delta)
|
||||
|
@ -670,15 +670,15 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testBooleans() {
|
||||
runBooleanTest(ByteBuffer2(4096))
|
||||
runBooleanTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runBooleanTest(write: ByteBuffer2) {
|
||||
private fun runBooleanTest(write: ByteArrayBuffer) {
|
||||
for (i in 0..99) {
|
||||
write.writeBoolean(true)
|
||||
write.writeBoolean(false)
|
||||
}
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
for (i in 0..99) {
|
||||
Assert.assertEquals(true, read.readBoolean())
|
||||
Assert.assertEquals(false, read.readBoolean())
|
||||
|
@ -687,10 +687,10 @@ class ByteBuffer2Test {
|
|||
|
||||
@Test
|
||||
fun testChars() {
|
||||
runCharTest(ByteBuffer2(4096))
|
||||
runCharTest(ByteArrayBuffer(4096))
|
||||
}
|
||||
|
||||
private fun runCharTest(write: ByteBuffer2) {
|
||||
private fun runCharTest(write: ByteArrayBuffer) {
|
||||
write.writeChar(0.toChar())
|
||||
write.writeChar(63.toChar())
|
||||
write.writeChar(64.toChar())
|
||||
|
@ -700,7 +700,7 @@ class ByteBuffer2Test {
|
|||
write.writeChar(16384.toChar())
|
||||
write.writeChar(32767.toChar())
|
||||
write.writeChar(65535.toChar())
|
||||
val read = ByteBuffer2(write.toBytes())
|
||||
val read = ByteArrayBuffer(write.toBytes())
|
||||
Assert.assertEquals(0, read.readChar().code.toLong())
|
||||
Assert.assertEquals(63, read.readChar().code.toLong())
|
||||
Assert.assertEquals(64, read.readChar().code.toLong())
|
||||
|
@ -716,7 +716,7 @@ class ByteBuffer2Test {
|
|||
@Throws(Exception::class)
|
||||
fun testInputWithOffset() {
|
||||
val buf = ByteArray(30)
|
||||
val `in` = ByteBuffer2(buf)
|
||||
val `in` = ByteArrayBuffer(buf)
|
||||
`in`.skip(20)
|
||||
Assert.assertEquals(10, `in`.remaining().toLong())
|
||||
}
|
||||
|
@ -725,10 +725,10 @@ class ByteBuffer2Test {
|
|||
@Throws(Exception::class)
|
||||
fun testSmallBuffers() {
|
||||
val buf = ByteBuffer.allocate(1024)
|
||||
val testOutput = ByteBuffer2(buf.array())
|
||||
val testOutput = ByteArrayBuffer(buf.array())
|
||||
testOutput.writeBytes(ByteArray(512))
|
||||
testOutput.writeBytes(ByteArray(512))
|
||||
val testInputs = ByteBuffer2()
|
||||
val testInputs = ByteArrayBuffer()
|
||||
buf.flip()
|
||||
testInputs.setBuffer(buf.array())
|
||||
val toRead = ByteArray(512)
|
Loading…
Reference in New Issue