Renamed InputStreams from in and is to input, so it doesn't look strange in kotlin

This commit is contained in:
Christian Basler 2017-11-24 07:37:04 +01:00
parent 8cbdce6eac
commit a5c78fd8cf
25 changed files with 187 additions and 209 deletions

View File

@ -116,15 +116,15 @@ class BitmessageAddress : Serializable {
constructor(address: String) { constructor(address: String) {
this.address = address this.address = address
val bytes = Base58.decode(address.substring(3)) val bytes = Base58.decode(address.substring(3))
val `in` = ByteArrayInputStream(bytes) val input = ByteArrayInputStream(bytes)
val counter = AccessCounter() val counter = AccessCounter()
this.version = varInt(`in`, counter) this.version = varInt(input, counter)
this.stream = varInt(`in`, counter) this.stream = varInt(input, counter)
this.ripe = Bytes.expand(bytes(`in`, bytes.size - counter.length() - 4), 20) this.ripe = Bytes.expand(bytes(input, bytes.size - counter.length() - 4), 20)
// test checksum // test checksum
var checksum = cryptography().doubleSha512(bytes, bytes.size - 4) var checksum = cryptography().doubleSha512(bytes, bytes.size - 4)
val expectedChecksum = bytes(`in`, 4) val expectedChecksum = bytes(input, 4)
for (i in 0..3) { for (i in 0..3) {
if (expectedChecksum[i] != checksum[i]) if (expectedChecksum[i] != checksum[i])
throw IllegalArgumentException("Checksum of address failed") throw IllegalArgumentException("Checksum of address failed")

View File

@ -75,9 +75,9 @@ open class CustomMessage(val customCommand: String, private val data: ByteArray?
val COMMAND_ERROR = "ERROR" val COMMAND_ERROR = "ERROR"
@JvmStatic @JvmStatic
fun read(`in`: InputStream, length: Int): CustomMessage { fun read(input: InputStream, length: Int): CustomMessage {
val counter = AccessCounter() val counter = AccessCounter()
return CustomMessage(varString(`in`, counter), bytes(`in`, length - counter.length())) return CustomMessage(varString(input, counter), bytes(input, length - counter.length()))
} }
@JvmStatic @JvmStatic

View File

@ -751,28 +751,28 @@ class Plaintext private constructor(
companion object { companion object {
@JvmStatic @JvmStatic
fun read(type: Type, `in`: InputStream): Plaintext { fun read(type: Type, input: InputStream): Plaintext {
return readWithoutSignature(type, `in`) return readWithoutSignature(type, input)
.signature(Decode.varBytes(`in`)) .signature(Decode.varBytes(input))
.received(UnixTime.now) .received(UnixTime.now)
.build() .build()
} }
@JvmStatic @JvmStatic
fun readWithoutSignature(type: Type, `in`: InputStream): Plaintext.Builder { fun readWithoutSignature(type: Type, input: InputStream): Plaintext.Builder {
val version = Decode.varInt(`in`) val version = Decode.varInt(input)
return Builder(type) return Builder(type)
.addressVersion(version) .addressVersion(version)
.stream(Decode.varInt(`in`)) .stream(Decode.varInt(input))
.behaviorBitfield(Decode.int32(`in`)) .behaviorBitfield(Decode.int32(input))
.publicSigningKey(Decode.bytes(`in`, 64)) .publicSigningKey(Decode.bytes(input, 64))
.publicEncryptionKey(Decode.bytes(`in`, 64)) .publicEncryptionKey(Decode.bytes(input, 64))
.nonceTrialsPerByte(if (version >= 3) Decode.varInt(`in`) else 0) .nonceTrialsPerByte(if (version >= 3) Decode.varInt(input) else 0)
.extraBytes(if (version >= 3) Decode.varInt(`in`) else 0) .extraBytes(if (version >= 3) Decode.varInt(input) else 0)
.destinationRipe(if (type == MSG) Decode.bytes(`in`, 20) else null) .destinationRipe(if (type == MSG) Decode.bytes(input, 20) else null)
.encoding(Decode.varInt(`in`)) .encoding(Decode.varInt(input))
.message(Decode.varBytes(`in`)) .message(Decode.varBytes(input))
.ackMessage(if (type == MSG) Decode.varBytes(`in`) else null) .ackMessage(if (type == MSG) Decode.varBytes(input) else null)
} }
} }
} }

View File

@ -65,7 +65,7 @@ class GenericPayload(version: Long, override val stream: Long, val data: ByteArr
companion object { companion object {
@JvmStatic @JvmStatic
fun read(version: Long, stream: Long, `is`: InputStream, length: Int) = fun read(version: Long, stream: Long, input: InputStream, length: Int) =
GenericPayload(version, stream, Decode.bytes(`is`, length)) GenericPayload(version, stream, Decode.bytes(input, length))
} }
} }

View File

@ -68,8 +68,8 @@ class GetPubkey : ObjectPayload {
companion object { companion object {
@JvmStatic @JvmStatic
fun read(`is`: InputStream, stream: Long, length: Int, version: Long): GetPubkey { fun read(input: InputStream, stream: Long, length: Int, version: Long): GetPubkey {
return GetPubkey(version, stream, Decode.bytes(`is`, length)) return GetPubkey(version, stream, Decode.bytes(input, length))
} }
} }
} }

View File

@ -16,8 +16,11 @@
package ch.dissem.bitmessage.entity.payload package ch.dissem.bitmessage.entity.payload
import ch.dissem.bitmessage.entity.* import ch.dissem.bitmessage.entity.Encrypted
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.bitmessage.entity.Plaintext.Type.MSG import ch.dissem.bitmessage.entity.Plaintext.Type.MSG
import ch.dissem.bitmessage.entity.PlaintextHolder
import ch.dissem.bitmessage.entity.SignedStreamableWriter
import ch.dissem.bitmessage.exception.DecryptionFailedException import ch.dissem.bitmessage.exception.DecryptionFailedException
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
@ -103,8 +106,6 @@ class Msg : ObjectPayload, Encrypted, PlaintextHolder {
val ACK_LENGTH = 32 val ACK_LENGTH = 32
@JvmStatic @JvmStatic
fun read(`in`: InputStream, stream: Long, length: Int): Msg { fun read(input: InputStream, stream: Long, length: Int) = Msg(stream, CryptoBox.read(input, length))
return Msg(stream, CryptoBox.read(`in`, length))
}
} }
} }

View File

@ -107,13 +107,13 @@ open class V2Pubkey constructor(
} }
companion object { companion object {
@JvmStatic fun read(`in`: InputStream, stream: Long): V2Pubkey { @JvmStatic fun read(input: InputStream, stream: Long): V2Pubkey {
return V2Pubkey( return V2Pubkey(
version = 2, version = 2,
stream = stream, stream = stream,
behaviorBitfield = Decode.uint32(`in`).toInt(), behaviorBitfield = Decode.uint32(input).toInt(),
signingKey = Decode.bytes(`in`, 64), signingKey = Decode.bytes(input, 64),
encryptionKey = Decode.bytes(`in`, 64) encryptionKey = Decode.bytes(input, 64)
) )
} }
} }

View File

@ -17,8 +17,6 @@
package ch.dissem.bitmessage.entity.payload package ch.dissem.bitmessage.entity.payload
import ch.dissem.bitmessage.entity.EncryptedStreamableWriter import ch.dissem.bitmessage.entity.EncryptedStreamableWriter
import ch.dissem.bitmessage.entity.SignedStreamableWriter
import ch.dissem.bitmessage.entity.StreamableWriter
import ch.dissem.bitmessage.utils.Decode import ch.dissem.bitmessage.utils.Decode
import ch.dissem.bitmessage.utils.Encode import ch.dissem.bitmessage.utils.Encode
import java.io.InputStream import java.io.InputStream
@ -144,16 +142,16 @@ class V3Pubkey protected constructor(
} }
companion object { companion object {
@JvmStatic fun read(`is`: InputStream, stream: Long): V3Pubkey { @JvmStatic fun read(input: InputStream, stream: Long): V3Pubkey {
return V3Pubkey( return V3Pubkey(
version = 3, version = 3,
stream = stream, stream = stream,
behaviorBitfield = Decode.int32(`is`), behaviorBitfield = Decode.int32(input),
signingKey = Decode.bytes(`is`, 64), signingKey = Decode.bytes(input, 64),
encryptionKey = Decode.bytes(`is`, 64), encryptionKey = Decode.bytes(input, 64),
nonceTrialsPerByte = Decode.varInt(`is`), nonceTrialsPerByte = Decode.varInt(input),
extraBytes = Decode.varInt(`is`), extraBytes = Decode.varInt(input),
signature = Decode.varBytes(`is`) signature = Decode.varBytes(input)
) )
} }
} }

View File

@ -59,8 +59,7 @@ open class V4Broadcast : Broadcast {
companion object { companion object {
@JvmStatic @JvmStatic
fun read(`in`: InputStream, stream: Long, length: Int): V4Broadcast { fun read(input: InputStream, stream: Long, length: Int) =
return V4Broadcast(4, stream, CryptoBox.read(`in`, length), null) V4Broadcast(4, stream, CryptoBox.read(input, length), null)
}
} }
} }

View File

@ -137,13 +137,12 @@ class V4Pubkey : Pubkey, Encrypted {
companion object { companion object {
@JvmStatic @JvmStatic
fun read(`in`: InputStream, stream: Long, length: Int, encrypted: Boolean): V4Pubkey { fun read(input: InputStream, stream: Long, length: Int, encrypted: Boolean) = if (encrypted) {
if (encrypted) V4Pubkey(stream,
return V4Pubkey(stream, Decode.bytes(input, 32),
Decode.bytes(`in`, 32), CryptoBox.read(input, length - 32))
CryptoBox.read(`in`, length - 32)) } else {
else V4Pubkey(V3Pubkey.read(input, stream))
return V4Pubkey(V3Pubkey.read(`in`, stream))
} }
} }
} }

View File

@ -61,8 +61,7 @@ class V5Broadcast : V4Broadcast {
companion object { companion object {
@JvmStatic @JvmStatic
fun read(`is`: InputStream, stream: Long, length: Int): V5Broadcast { fun read(input: InputStream, stream: Long, length: Int) =
return V5Broadcast(stream, Decode.bytes(`is`, 32), CryptoBox.read(`is`, length - 32)) V5Broadcast(stream, Decode.bytes(input, 32), CryptoBox.read(input, length - 32))
}
} }
} }

View File

@ -191,13 +191,13 @@ data class PrivateKey(
} }
@JvmStatic @JvmStatic
fun read(`is`: InputStream): PrivateKey { fun read(input: InputStream): PrivateKey {
val version = Decode.varInt(`is`).toInt() val version = Decode.varInt(input).toInt()
val stream = Decode.varInt(`is`) val stream = Decode.varInt(input)
val len = Decode.varInt(`is`).toInt() val len = Decode.varInt(input).toInt()
val pubkey = Factory.readPubkey(version.toLong(), stream, `is`, len, false) ?: throw ApplicationException("Unknown pubkey version encountered") val pubkey = Factory.readPubkey(version.toLong(), stream, input, len, false) ?: throw ApplicationException("Unknown pubkey version encountered")
val signingKey = Decode.varBytes(`is`) val signingKey = Decode.varBytes(input)
val encryptionKey = Decode.varBytes(`is`) val encryptionKey = Decode.varBytes(input)
return PrivateKey(signingKey, encryptionKey, pubkey) return PrivateKey(signingKey, encryptionKey, pubkey)
} }
} }

View File

@ -156,11 +156,11 @@ object Factory {
return GetPubkey.read(stream, streamNumber, length, version) return GetPubkey.read(stream, streamNumber, length, version)
} }
@JvmStatic fun readPubkey(version: Long, stream: Long, `is`: InputStream, length: Int, encrypted: Boolean): Pubkey? { @JvmStatic fun readPubkey(version: Long, stream: Long, input: InputStream, length: Int, encrypted: Boolean): Pubkey? {
when (version.toInt()) { when (version.toInt()) {
2 -> return V2Pubkey.read(`is`, stream) 2 -> return V2Pubkey.read(input, stream)
3 -> return V3Pubkey.read(`is`, stream) 3 -> return V3Pubkey.read(input, stream)
4 -> return V4Pubkey.read(`is`, stream, length, encrypted) 4 -> return V4Pubkey.read(input, stream, length, encrypted)
} }
LOG.debug("Unexpected pubkey version $version, handling as generic payload object") LOG.debug("Unexpected pubkey version $version, handling as generic payload object")
return null return null

View File

@ -39,59 +39,52 @@ object V3MessageFactory {
private val LOG = LoggerFactory.getLogger(V3MessageFactory::class.java) private val LOG = LoggerFactory.getLogger(V3MessageFactory::class.java)
@JvmStatic @JvmStatic
fun read(`in`: InputStream): NetworkMessage? { fun read(input: InputStream): NetworkMessage? {
findMagic(`in`) findMagic(input)
val command = getCommand(`in`) val command = getCommand(input)
val length = Decode.uint32(`in`).toInt() val length = Decode.uint32(input).toInt()
if (length > 1600003) { if (length > 1600003) {
throw NodeException("Payload of $length bytes received, no more than 1600003 was expected.") throw NodeException("Payload of $length bytes received, no more than 1600003 was expected.")
} }
val checksum = Decode.bytes(`in`, 4) val checksum = Decode.bytes(input, 4)
val payloadBytes = Decode.bytes(`in`, length) val payloadBytes = Decode.bytes(input, length)
if (testChecksum(checksum, payloadBytes)) { if (testChecksum(checksum, payloadBytes)) {
val payload = getPayload(command, ByteArrayInputStream(payloadBytes), length) val payload = getPayload(command, ByteArrayInputStream(payloadBytes), length)
if (payload != null) return payload?.let { NetworkMessage(payload) }
return NetworkMessage(payload)
else
return null
} else { } else {
throw IOException("Checksum failed for message '$command'") throw IOException("Checksum failed for message '$command'")
} }
} }
@JvmStatic @JvmStatic
fun getPayload(command: String, stream: InputStream, length: Int): MessagePayload? { fun getPayload(command: String, stream: InputStream, length: Int): MessagePayload? = when (command) {
when (command) { "version" -> parseVersion(stream)
"version" -> return parseVersion(stream) "verack" -> VerAck()
"verack" -> return VerAck() "addr" -> parseAddr(stream)
"addr" -> return parseAddr(stream) "inv" -> parseInv(stream)
"inv" -> return parseInv(stream) "getdata" -> parseGetData(stream)
"getdata" -> return parseGetData(stream) "object" -> readObject(stream, length)
"object" -> return readObject(stream, length) "custom" -> readCustom(stream, length)
"custom" -> return readCustom(stream, length) else -> {
else -> { LOG.debug("Unknown command: " + command)
LOG.debug("Unknown command: " + command) null
return null
}
} }
} }
private fun readCustom(`in`: InputStream, length: Int): MessagePayload { private fun readCustom(input: InputStream, length: Int): MessagePayload = CustomMessage.read(input, length)
return CustomMessage.read(`in`, length)
}
@JvmStatic @JvmStatic
fun readObject(`in`: InputStream, length: Int): ObjectMessage { fun readObject(input: InputStream, length: Int): ObjectMessage {
val counter = AccessCounter() val counter = AccessCounter()
val nonce = Decode.bytes(`in`, 8, counter) val nonce = Decode.bytes(input, 8, counter)
val expiresTime = Decode.int64(`in`, counter) val expiresTime = Decode.int64(input, counter)
val objectType = Decode.uint32(`in`, counter) val objectType = Decode.uint32(input, counter)
val version = Decode.varInt(`in`, counter) val version = Decode.varInt(input, counter)
val stream = Decode.varInt(`in`, counter) val stream = Decode.varInt(input, counter)
val data = Decode.bytes(`in`, length - counter.length()) val data = Decode.bytes(input, length - counter.length())
var payload: ObjectPayload var payload: ObjectPayload
try { try {
val dataStream = ByteArrayInputStream(data) val dataStream = ByteArrayInputStream(data)
@ -116,7 +109,7 @@ object V3MessageFactory {
private fun parseGetData(stream: InputStream): GetData { private fun parseGetData(stream: InputStream): GetData {
val count = Decode.varInt(stream) val count = Decode.varInt(stream)
val inventoryVectors = LinkedList<InventoryVector>() val inventoryVectors = LinkedList<InventoryVector>()
for (i in 0..count - 1) { for (i in 0 until count) {
inventoryVectors.add(parseInventoryVector(stream)) inventoryVectors.add(parseInventoryVector(stream))
} }
return GetData(inventoryVectors) return GetData(inventoryVectors)
@ -125,7 +118,7 @@ object V3MessageFactory {
private fun parseInv(stream: InputStream): Inv { private fun parseInv(stream: InputStream): Inv {
val count = Decode.varInt(stream) val count = Decode.varInt(stream)
val inventoryVectors = LinkedList<InventoryVector>() val inventoryVectors = LinkedList<InventoryVector>()
for (i in 0..count - 1) { for (i in 0 until count) {
inventoryVectors.add(parseInventoryVector(stream)) inventoryVectors.add(parseInventoryVector(stream))
} }
return Inv(inventoryVectors) return Inv(inventoryVectors)
@ -134,7 +127,7 @@ object V3MessageFactory {
private fun parseAddr(stream: InputStream): Addr { private fun parseAddr(stream: InputStream): Addr {
val count = Decode.varInt(stream) val count = Decode.varInt(stream)
val networkAddresses = LinkedList<NetworkAddress>() val networkAddresses = LinkedList<NetworkAddress>()
for (i in 0..count - 1) { for (i in 0 until count) {
networkAddresses.add(parseAddress(stream, false)) networkAddresses.add(parseAddress(stream, false))
} }
return Addr(networkAddresses) return Addr(networkAddresses)
@ -160,9 +153,7 @@ object V3MessageFactory {
.streams(*streamNumbers).build() .streams(*streamNumbers).build()
} }
private fun parseInventoryVector(stream: InputStream): InventoryVector { private fun parseInventoryVector(stream: InputStream) = InventoryVector(Decode.bytes(stream, 32))
return InventoryVector(Decode.bytes(stream, 32))
}
private fun parseAddress(stream: InputStream, light: Boolean): NetworkAddress { private fun parseAddress(stream: InputStream, light: Boolean): NetworkAddress {
val time: Long val time: Long
@ -188,12 +179,7 @@ object V3MessageFactory {
private fun testChecksum(checksum: ByteArray, payload: ByteArray): Boolean { private fun testChecksum(checksum: ByteArray, payload: ByteArray): Boolean {
val payloadChecksum = cryptography().sha512(payload) val payloadChecksum = cryptography().sha512(payload)
for (i in checksum.indices) { return checksum.indices.none { checksum[it] != payloadChecksum[it] }
if (checksum[i] != payloadChecksum[i]) {
return false
}
}
return true
} }
private fun getCommand(stream: InputStream): String { private fun getCommand(stream: InputStream): String {
@ -210,10 +196,10 @@ object V3MessageFactory {
return String(bytes, 0, end, Charsets.US_ASCII) return String(bytes, 0, end, Charsets.US_ASCII)
} }
private fun findMagic(`in`: InputStream) { private fun findMagic(input: InputStream) {
var pos = 0 var pos = 0
for (i in 0..1619999) { for (i in 0..1619999) {
val b = `in`.read().toByte() val b = input.read().toByte()
if (b == NetworkMessage.MAGIC_BYTES[pos]) { if (b == NetworkMessage.MAGIC_BYTES[pos]) {
if (pos + 1 == NetworkMessage.MAGIC_BYTES.size) { if (pos + 1 == NetworkMessage.MAGIC_BYTES.size) {
return return

View File

@ -31,8 +31,8 @@ object NodeRegistryHelper {
@JvmStatic @JvmStatic
fun loadStableNodes(): Map<Long, Set<NetworkAddress>> { fun loadStableNodes(): Map<Long, Set<NetworkAddress>> {
javaClass.classLoader.getResourceAsStream("nodes.txt").use { `in` -> javaClass.classLoader.getResourceAsStream("nodes.txt").use { input ->
val scanner = Scanner(`in`) val scanner = Scanner(input)
var stream: Long = 0 var stream: Long = 0
val result = HashMap<Long, Set<NetworkAddress>>() val result = HashMap<Long, Set<NetworkAddress>>()
var streamSet: MutableSet<NetworkAddress>? = null var streamSet: MutableSet<NetworkAddress>? = null

View File

@ -25,21 +25,21 @@ import java.nio.ByteBuffer
* https://bitmessage.org/wiki/Protocol_specification#Common_structures * https://bitmessage.org/wiki/Protocol_specification#Common_structures
*/ */
object Decode { object Decode {
@JvmStatic fun shortVarBytes(`in`: InputStream, counter: AccessCounter): ByteArray { @JvmStatic fun shortVarBytes(input: InputStream, counter: AccessCounter): ByteArray {
val length = uint16(`in`, counter) val length = uint16(input, counter)
return bytes(`in`, length, counter) return bytes(input, length, counter)
} }
@JvmStatic @JvmOverloads fun varBytes(`in`: InputStream, counter: AccessCounter? = null): ByteArray { @JvmStatic @JvmOverloads fun varBytes(input: InputStream, counter: AccessCounter? = null): ByteArray {
val length = varInt(`in`, counter).toInt() val length = varInt(input, counter).toInt()
return bytes(`in`, length, counter) return bytes(input, length, counter)
} }
@JvmStatic @JvmOverloads fun bytes(`in`: InputStream, count: Int, counter: AccessCounter? = null): ByteArray { @JvmStatic @JvmOverloads fun bytes(input: InputStream, count: Int, counter: AccessCounter? = null): ByteArray {
val result = ByteArray(count) val result = ByteArray(count)
var off = 0 var off = 0
while (off < count) { while (off < count) {
val read = `in`.read(result, off, count - off) val read = input.read(result, off, count - off)
if (read < 0) { if (read < 0) {
throw IOException("Unexpected end of stream, wanted to read $count bytes but only got $off") throw IOException("Unexpected end of stream, wanted to read $count bytes but only got $off")
} }
@ -49,60 +49,58 @@ object Decode {
return result return result
} }
@JvmStatic fun varIntList(`in`: InputStream): LongArray { @JvmStatic fun varIntList(input: InputStream): LongArray {
val length = varInt(`in`).toInt() val length = varInt(input).toInt()
val result = LongArray(length) val result = LongArray(length)
for (i in 0..length - 1) { for (i in 0 until length) {
result[i] = varInt(`in`) result[i] = varInt(input)
} }
return result return result
} }
@JvmStatic @JvmOverloads fun varInt(`in`: InputStream, counter: AccessCounter? = null): Long { @JvmStatic @JvmOverloads fun varInt(input: InputStream, counter: AccessCounter? = null): Long {
val first = `in`.read() val first = input.read()
AccessCounter.inc(counter) AccessCounter.inc(counter)
when (first) { when (first) {
0xfd -> return uint16(`in`, counter).toLong() 0xfd -> return uint16(input, counter).toLong()
0xfe -> return uint32(`in`, counter) 0xfe -> return uint32(input, counter)
0xff -> return int64(`in`, counter) 0xff -> return int64(input, counter)
else -> return first.toLong() else -> return first.toLong()
} }
} }
@JvmStatic fun uint8(`in`: InputStream): Int { @JvmStatic fun uint8(input: InputStream): Int = input.read()
return `in`.read()
}
@JvmStatic @JvmOverloads fun uint16(`in`: InputStream, counter: AccessCounter? = null): Int { @JvmStatic @JvmOverloads fun uint16(input: InputStream, counter: AccessCounter? = null): Int {
AccessCounter.inc(counter, 2) AccessCounter.inc(counter, 2)
return `in`.read() shl 8 or `in`.read() return input.read() shl 8 or input.read()
} }
@JvmStatic @JvmOverloads fun uint32(`in`: InputStream, counter: AccessCounter? = null): Long { @JvmStatic @JvmOverloads fun uint32(input: InputStream, counter: AccessCounter? = null): Long {
AccessCounter.inc(counter, 4) AccessCounter.inc(counter, 4)
return (`in`.read() shl 24 or (`in`.read() shl 16) or (`in`.read() shl 8) or `in`.read()).toLong() return (input.read() shl 24 or (input.read() shl 16) or (input.read() shl 8) or input.read()).toLong()
} }
@JvmStatic fun uint32(`in`: ByteBuffer): Long { @JvmStatic fun uint32(input: ByteBuffer): Long {
return (u(`in`.get()) shl 24 or (u(`in`.get()) shl 16) or (u(`in`.get()) shl 8) or u(`in`.get())).toLong() return (u(input.get()) shl 24 or (u(input.get()) shl 16) or (u(input.get()) shl 8) or u(input.get())).toLong()
} }
@JvmStatic @JvmOverloads fun int32(`in`: InputStream, counter: AccessCounter? = null): Int { @JvmStatic @JvmOverloads fun int32(input: InputStream, counter: AccessCounter? = null): Int {
AccessCounter.inc(counter, 4) AccessCounter.inc(counter, 4)
return ByteBuffer.wrap(bytes(`in`, 4)).int return ByteBuffer.wrap(bytes(input, 4)).int
} }
@JvmStatic @JvmOverloads fun int64(`in`: InputStream, counter: AccessCounter? = null): Long { @JvmStatic @JvmOverloads fun int64(input: InputStream, counter: AccessCounter? = null): Long {
AccessCounter.inc(counter, 8) AccessCounter.inc(counter, 8)
return ByteBuffer.wrap(bytes(`in`, 8)).long return ByteBuffer.wrap(bytes(input, 8)).long
} }
@JvmStatic @JvmOverloads fun varString(`in`: InputStream, counter: AccessCounter? = null): String { @JvmStatic @JvmOverloads fun varString(input: InputStream, counter: AccessCounter? = null): String {
val length = varInt(`in`, counter).toInt() val length = varInt(input, counter).toInt()
// technically, it says the length in characters, but I think this one might be correct // technically, it says the length in characters, but I think this one might be correct
// otherwise it will get complicated, as we'll need to read UTF-8 char by char... // otherwise it will get complicated, as we'll need to read UTF-8 char by char...
return String(bytes(`in`, length, counter)) return String(bytes(input, length, counter))
} }
/** /**

View File

@ -44,20 +44,20 @@ class ExtendedEncodingTest {
@Test @Test
fun `ensure simple message is encoded`() { fun `ensure simple message is encoded`() {
val `in` = Message.Builder() val input = Message.Builder()
.subject("Test sübject") .subject("Test sübject")
.body("test bödy") .body("test bödy")
.build() .build()
assertThat(`in`.zip(), notNullValue()) assertThat(input.zip(), notNullValue())
val out = ExtendedEncodingFactory.unzip(`in`.zip()) val out = ExtendedEncodingFactory.unzip(input.zip())
assertThat<ExtendedEncoding>(out, `is`(`in`)) assertThat<ExtendedEncoding>(out, `is`(input))
} }
@Test @Test
fun `ensure complete message is encoded and decoded`() { fun `ensure complete message is encoded and decoded`() {
val `in` = Message.Builder() val input = Message.Builder()
.addParent(TestUtils.randomInventoryVector()) .addParent(TestUtils.randomInventoryVector())
.addParent(TestUtils.randomInventoryVector()) .addParent(TestUtils.randomInventoryVector())
.subject("Test sübject") .subject("Test sübject")
@ -72,22 +72,22 @@ class ExtendedEncodingTest {
) )
.build() .build()
assertThat(`in`.zip(), notNullValue()) assertThat(input.zip(), notNullValue())
val out = ExtendedEncodingFactory.unzip(`in`.zip()) val out = ExtendedEncodingFactory.unzip(input.zip())
assertThat<ExtendedEncoding>(out, `is`(`in`)) assertThat<ExtendedEncoding>(out, `is`(input))
} }
@Test @Test
fun `ensure vote is encoded and decoded`() { fun `ensure vote is encoded and decoded`() {
val `in` = Vote.Builder() val input = Vote.Builder()
.msgId(TestUtils.randomInventoryVector()) .msgId(TestUtils.randomInventoryVector())
.vote("+1") .vote("+1")
.build() .build()
assertThat(`in`.zip(), notNullValue()) assertThat(input.zip(), notNullValue())
val out = ExtendedEncodingFactory.unzip(`in`.zip()) val out = ExtendedEncodingFactory.unzip(input.zip())
assertThat<ExtendedEncoding>(out, `is`(`in`)) assertThat<ExtendedEncoding>(out, `is`(input))
} }
} }

View File

@ -87,8 +87,8 @@ class SerializationTest : TestBase() {
.build() .build()
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
expected.writer().write(out) expected.writer().write(out)
val `in` = ByteArrayInputStream(out.toByteArray()) val input = ByteArrayInputStream(out.toByteArray())
val actual = Plaintext.read(MSG, `in`) val actual = Plaintext.read(MSG, input)
// Received is automatically set on deserialization, so we'll need to set it to null // Received is automatically set on deserialization, so we'll need to set it to null
val received = Plaintext::class.java.getDeclaredField("received") val received = Plaintext::class.java.getDeclaredField("received")
@ -112,8 +112,8 @@ class SerializationTest : TestBase() {
.build() .build()
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
expected.writer().write(out) expected.writer().write(out)
val `in` = ByteArrayInputStream(out.toByteArray()) val input = ByteArrayInputStream(out.toByteArray())
val actual = Plaintext.read(MSG, `in`) val actual = Plaintext.read(MSG, input)
// Received is automatically set on deserialization, so we'll need to set it to null // Received is automatically set on deserialization, so we'll need to set it to null
val received = Plaintext::class.java.getDeclaredField("received") val received = Plaintext::class.java.getDeclaredField("received")
@ -137,8 +137,8 @@ class SerializationTest : TestBase() {
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
expected.writer().write(out) expected.writer().write(out)
val `in` = ByteArrayInputStream(out.toByteArray()) val input = ByteArrayInputStream(out.toByteArray())
val actual = Plaintext.read(MSG, `in`) val actual = Plaintext.read(MSG, input)
// Received is automatically set on deserialization, so we'll need to set it to null // Received is automatically set on deserialization, so we'll need to set it to null
val received = Plaintext::class.java.getDeclaredField("received") val received = Plaintext::class.java.getDeclaredField("received")
@ -169,8 +169,8 @@ class SerializationTest : TestBase() {
private fun doTest(resourceName: String, version: Int, expectedPayloadType: Class<*>) { private fun doTest(resourceName: String, version: Int, expectedPayloadType: Class<*>) {
val data = TestUtils.getBytes(resourceName) val data = TestUtils.getBytes(resourceName)
val `in` = ByteArrayInputStream(data) val input = ByteArrayInputStream(data)
val objectMessage = Factory.getObjectMessage(version, `in`, data.size) val objectMessage = Factory.getObjectMessage(version, input, data.size)
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
assertNotNull(objectMessage) assertNotNull(objectMessage)
objectMessage!!.writer().write(out) objectMessage!!.writer().write(out)
@ -190,8 +190,8 @@ class SerializationTest : TestBase() {
val oos = ObjectOutputStream(out) val oos = ObjectOutputStream(out)
oos.writeObject(plaintext) oos.writeObject(plaintext)
val `in` = ByteArrayInputStream(out.toByteArray()) val input = ByteArrayInputStream(out.toByteArray())
val ois = ObjectInputStream(`in`) val ois = ObjectInputStream(input)
assertEquals(plaintext, ois.readObject()) assertEquals(plaintext, ois.readObject())
} }
} }

View File

@ -34,8 +34,8 @@ class DecodeTest {
} }
private fun testCodec(number: Long) { private fun testCodec(number: Long) {
val `is` = ByteArrayOutputStream() val out = ByteArrayOutputStream()
Encode.varInt(number, `is`) Encode.varInt(number, out)
assertEquals(number, Decode.varInt(ByteArrayInputStream(`is`.toByteArray()))) assertEquals(number, Decode.varInt(ByteArrayInputStream(out.toByteArray())))
} }
} }

View File

@ -49,18 +49,18 @@ object TestUtils {
@JvmStatic fun loadObjectMessage(version: Int, resourceName: String): ObjectMessage { @JvmStatic fun loadObjectMessage(version: Int, resourceName: String): ObjectMessage {
val data = getBytes(resourceName) val data = getBytes(resourceName)
val `in` = ByteArrayInputStream(data) val input = ByteArrayInputStream(data)
return Factory.getObjectMessage(version, `in`, data.size) ?: throw NoSuchElementException("error loading object message") return Factory.getObjectMessage(version, input, data.size) ?: throw NoSuchElementException("error loading object message")
} }
@JvmStatic fun getBytes(resourceName: String): ByteArray { @JvmStatic fun getBytes(resourceName: String): ByteArray {
val `in` = javaClass.classLoader.getResourceAsStream(resourceName) val input = javaClass.classLoader.getResourceAsStream(resourceName)
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
val buffer = ByteArray(1024) val buffer = ByteArray(1024)
var len = `in`.read(buffer) var len = input.read(buffer)
while (len != -1) { while (len != -1) {
out.write(buffer, 0, len) out.write(buffer, 0, len)
len = `in`.read(buffer) len = input.read(buffer)
} }
return out.toByteArray() return out.toByteArray()
} }
@ -71,9 +71,8 @@ object TestUtils {
return InventoryVector(bytes) return InventoryVector(bytes)
} }
@JvmStatic fun getResource(resourceName: String): InputStream { @JvmStatic fun getResource(resourceName: String): InputStream =
return javaClass.classLoader.getResourceAsStream(resourceName) javaClass.classLoader.getResourceAsStream(resourceName)
}
@JvmStatic fun loadIdentity(address: String): BitmessageAddress { @JvmStatic fun loadIdentity(address: String): BitmessageAddress {
val privateKey = PrivateKey.read(TestUtils.getResource(address + ".privkey")) val privateKey = PrivateKey.read(TestUtils.getResource(address + ".privkey"))

View File

@ -68,7 +68,7 @@ object ContactExport {
Factory.readPubkey( Factory.readPubkey(
version = version, version = version,
stream = stream, stream = stream,
`is` = ByteArrayInputStream(it), input = ByteArrayInputStream(it),
length = it.size, length = it.size,
encrypted = false encrypted = false
) )

View File

@ -81,16 +81,16 @@ class CryptoCustomMessage<T : Streamable> : CustomMessage {
@Throws(DecryptionFailedException::class) @Throws(DecryptionFailedException::class)
fun decrypt(privateKey: ByteArray): T { fun decrypt(privateKey: ByteArray): T {
val `in` = SignatureCheckingInputStream(container?.decrypt(privateKey) ?: throw IllegalStateException("no encrypted data available")) val input = SignatureCheckingInputStream(container?.decrypt(privateKey) ?: throw IllegalStateException("no encrypted data available"))
if (dataReader == null) throw IllegalStateException("no data reader available") if (dataReader == null) throw IllegalStateException("no data reader available")
val addressVersion = varInt(`in`) val addressVersion = varInt(input)
val stream = varInt(`in`) val stream = varInt(input)
val behaviorBitfield = int32(`in`) val behaviorBitfield = int32(input)
val publicSigningKey = bytes(`in`, 64) val publicSigningKey = bytes(input, 64)
val publicEncryptionKey = bytes(`in`, 64) val publicEncryptionKey = bytes(input, 64)
val nonceTrialsPerByte = if (addressVersion >= 3) varInt(`in`) else 0 val nonceTrialsPerByte = if (addressVersion >= 3) varInt(input) else 0
val extraBytes = if (addressVersion >= 3) varInt(`in`) else 0 val extraBytes = if (addressVersion >= 3) varInt(input) else 0
val sender = BitmessageAddress(Factory.createPubkey( val sender = BitmessageAddress(Factory.createPubkey(
addressVersion, addressVersion,
@ -103,9 +103,9 @@ class CryptoCustomMessage<T : Streamable> : CustomMessage {
)) ))
this.sender = sender this.sender = sender
data = dataReader.read(sender, `in`) data = dataReader.read(sender, input)
`in`.checkSignature(sender.pubkey!!) input.checkSignature(sender.pubkey!!)
return data!! return data!!
} }
@ -124,7 +124,7 @@ class CryptoCustomMessage<T : Streamable> : CustomMessage {
} }
interface Reader<out T> { interface Reader<out T> {
fun read(sender: BitmessageAddress, `in`: InputStream): T fun read(sender: BitmessageAddress, input: InputStream): T
} }
private inner class SignatureCheckingInputStream internal constructor(private val wrapped: InputStream) : InputStream() { private inner class SignatureCheckingInputStream internal constructor(private val wrapped: InputStream) : InputStream() {

View File

@ -56,8 +56,8 @@ data class ProofOfWorkRequest @JvmOverloads constructor(val sender: BitmessageAd
class Reader(private val identity: BitmessageAddress) : CryptoCustomMessage.Reader<ProofOfWorkRequest> { class Reader(private val identity: BitmessageAddress) : CryptoCustomMessage.Reader<ProofOfWorkRequest> {
override fun read(sender: BitmessageAddress, `in`: InputStream): ProofOfWorkRequest { override fun read(sender: BitmessageAddress, input: InputStream): ProofOfWorkRequest {
return ProofOfWorkRequest.read(identity, `in`) return ProofOfWorkRequest.read(identity, input)
} }
} }
@ -87,12 +87,12 @@ data class ProofOfWorkRequest @JvmOverloads constructor(val sender: BitmessageAd
companion object { companion object {
@JvmStatic @JvmStatic
fun read(client: BitmessageAddress, `in`: InputStream): ProofOfWorkRequest { fun read(client: BitmessageAddress, input: InputStream): ProofOfWorkRequest {
return ProofOfWorkRequest( return ProofOfWorkRequest(
client, client,
bytes(`in`, 64), bytes(input, 64),
Request.valueOf(varString(`in`)), Request.valueOf(varString(input)),
varBytes(`in`) varBytes(input)
) )
} }
} }

View File

@ -44,14 +44,13 @@ class CryptoCustomMessageTest : TestBase() {
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
messageBefore.writer().write(out) messageBefore.writer().write(out)
val `in` = ByteArrayInputStream(out.toByteArray()) val input = ByteArrayInputStream(out.toByteArray())
val customMessage = CustomMessage.read(`in`, out.size()) val customMessage = CustomMessage.read(input, out.size())
val messageAfter = CryptoCustomMessage.read(customMessage, val messageAfter = CryptoCustomMessage.read(customMessage,
object : CryptoCustomMessage.Reader<GenericPayload> { object : CryptoCustomMessage.Reader<GenericPayload> {
override fun read(sender: BitmessageAddress, `in`: InputStream): GenericPayload { override fun read(sender: BitmessageAddress, input: InputStream) =
return GenericPayload.read(0, 1, `in`, 100) GenericPayload.read(0, 1, input, 100)
}
}) })
val payloadAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey) val payloadAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey)
@ -72,9 +71,9 @@ class CryptoCustomMessageTest : TestBase() {
val out = ByteArrayOutputStream() val out = ByteArrayOutputStream()
messageBefore.writer().write(out) messageBefore.writer().write(out)
val `in` = ByteArrayInputStream(out.toByteArray()) val input = ByteArrayInputStream(out.toByteArray())
val customMessage = CustomMessage.read(`in`, out.size()) val customMessage = CustomMessage.read(input, out.size())
val messageAfter = CryptoCustomMessage.read(customMessage, val messageAfter = CryptoCustomMessage.read(customMessage,
ProofOfWorkRequest.Reader(sendingIdentity)) ProofOfWorkRequest.Reader(sendingIdentity))
val requestAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey) val requestAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey)

View File

@ -51,7 +51,7 @@ import java.util.*
*/ */
class WifImporter constructor( class WifImporter constructor(
private val ctx: BitmessageContext, private val ctx: BitmessageContext,
`in`: InputStream, input: InputStream,
vararg features: Pubkey.Feature vararg features: Pubkey.Feature
) { ) {
private val identities = LinkedList<BitmessageAddress>() private val identities = LinkedList<BitmessageAddress>()
@ -62,7 +62,7 @@ class WifImporter constructor(
init { init {
val ini = Ini() val ini = Ini()
ini.load(`in`) ini.load(input)
for ((key, section) in ini) { for ((key, section) in ini) {
if (!key.startsWith("BM-")) if (!key.startsWith("BM-"))