From 35de4525cf5427c8ccf0d43c689a9aa19fb31fb8 Mon Sep 17 00:00:00 2001 From: e5l Date: Thu, 18 Aug 2016 12:59:30 +0300 Subject: [PATCH] kotstd: update runtime --- car_fmw/src/proto_src/direction.kt | 1 - car_fmw/src/proto_src/route.kt | 38 +++++++++++++++++++++-------- kotstd/include/CodedInputStream.kt | 10 +++++++- kotstd/include/KotlinInputStream.kt | 14 ++++++++--- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/car_fmw/src/proto_src/direction.kt b/car_fmw/src/proto_src/direction.kt index e0ff2adfcb5..dcd6a76f24d 100644 --- a/car_fmw/src/proto_src/direction.kt +++ b/car_fmw/src/proto_src/direction.kt @@ -187,7 +187,6 @@ class DirectionRequest private constructor (var command: DirectionRequest.Comman } - class DirectionResponse private constructor (var code: Int) { //========== Properties =========== //int32 code = 1 diff --git a/car_fmw/src/proto_src/route.kt b/car_fmw/src/proto_src/route.kt index b09629aa0cd..3a876d0230f 100644 --- a/car_fmw/src/proto_src/route.kt +++ b/car_fmw/src/proto_src/route.kt @@ -247,16 +247,25 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int return false } val expectedByteSize = input.readInt32NoTag() - var newArray = IntArray(0) var readSize = 0 + var arraySize = 0 + input.mark() do { var i = 0 while(readSize < expectedByteSize) { - var tmp = IntArray(1) - tmp[0] = input.readInt32NoTag() - newArray = newArray.plus(tmp) - readSize += WireFormat.getInt32SizeNoTag(tmp[0]) + var tmp = 0 + tmp = input.readInt32NoTag() + arraySize += 1 + readSize += WireFormat.getInt32SizeNoTag(tmp) } + } while (false) + var newArray = IntArray(arraySize) + input.reset() + do { + var i = 0 + while(i < arraySize) { + newArray[i] = input.readInt32NoTag() + i += 1} distances = newArray } while (false) } @@ -266,16 +275,25 @@ class RouteRequest private constructor (var distances: IntArray, var angles: Int return false } val expectedByteSize = input.readInt32NoTag() - var newArray = IntArray(0) var readSize = 0 + var arraySize = 0 + input.mark() do { var i = 0 while(readSize < expectedByteSize) { - var tmp = IntArray(1) - tmp[0] = input.readInt32NoTag() - newArray = newArray.plus(tmp) - readSize += WireFormat.getInt32SizeNoTag(tmp[0]) + var tmp = 0 + tmp = input.readInt32NoTag() + arraySize += 1 + readSize += WireFormat.getInt32SizeNoTag(tmp) } + } while (false) + var newArray = IntArray(arraySize) + input.reset() + do { + var i = 0 + while(i < arraySize) { + newArray[i] = input.readInt32NoTag() + i += 1} angles = newArray } while (false) } diff --git a/kotstd/include/CodedInputStream.kt b/kotstd/include/CodedInputStream.kt index 1ba96e47970..bb5b6e47cae 100644 --- a/kotstd/include/CodedInputStream.kt +++ b/kotstd/include/CodedInputStream.kt @@ -12,7 +12,15 @@ class CodedInputStream(val buffer: ByteArray) { val inputStream: KotlinInputStream init { - inputStream = KotlinInputStream(buffer) // TODO: Java's realization uses hand-written buffers. Why? + inputStream = KotlinInputStream(buffer) + } + + fun mark() { + inputStream.mark() + } + + fun reset() { + inputStream.reset() } fun readInt32(expectedFieldNumber: Int): Int { diff --git a/kotstd/include/KotlinInputStream.kt b/kotstd/include/KotlinInputStream.kt index d5d7f951d47..ee2c40aa387 100644 --- a/kotstd/include/KotlinInputStream.kt +++ b/kotstd/include/KotlinInputStream.kt @@ -4,14 +4,22 @@ class KotlinInputStream(val buffer: ByteArray) { var pos = 0 + var mark_ = 0 fun read(): Byte { - val result = buffer[pos] - pos++ - return result + pos += 1 + return buffer[pos - 1] } fun isAtEnd(): Boolean { return pos >= buffer.size } + + fun mark() { + mark_ = pos + } + + fun reset() { + pos = mark_ + } }