@@ -154,7 +154,7 @@ open class ProtoReader(private val source: BufferedSource) {
154154 nextFieldEncoding = FieldEncoding .LENGTH_DELIMITED
155155 state = STATE_LENGTH_DELIMITED
156156 val length = internalReadVarint32()
157- if (length < 0 ) throw ProtocolException (" Negative length: $length " )
157+ if (length < 0 ) throw ProtocolException (" Negative length: $length . Reader position: $pos . Last read tag: $tag . " )
158158 if (pushedLimit != - 1L ) throw IllegalStateException ()
159159 // Push the current limit, and set a new limit to the length of this value.
160160 pushedLimit = limit
@@ -179,7 +179,7 @@ open class ProtoReader(private val source: BufferedSource) {
179179
180180 loop@ while (pos < limit && ! source.exhausted()) {
181181 val tagAndFieldEncoding = internalReadVarint32()
182- if (tagAndFieldEncoding == 0 ) throw ProtocolException (" Unexpected tag 0" )
182+ if (tagAndFieldEncoding == 0 ) throw ProtocolException (" Unexpected tag 0. Reader position: $pos . Last read tag: $tag . " )
183183
184184 tag = tagAndFieldEncoding shr TAG_FIELD_ENCODING_BITS
185185 when (val groupOrFieldEncoding = tagAndFieldEncoding and FIELD_ENCODING_MASK ) {
@@ -188,7 +188,7 @@ open class ProtoReader(private val source: BufferedSource) {
188188 continue @loop
189189 }
190190
191- STATE_END_GROUP -> throw ProtocolException (" Unexpected end group" )
191+ STATE_END_GROUP -> throw ProtocolException (" Unexpected end group. Reader position: $pos . Last read tag: $tag . " )
192192
193193 STATE_LENGTH_DELIMITED -> {
194194 internalNextLengthDelimited()
@@ -213,7 +213,7 @@ open class ProtoReader(private val source: BufferedSource) {
213213 return tag
214214 }
215215
216- else -> throw ProtocolException (" Unexpected field encoding: $groupOrFieldEncoding " )
216+ else -> throw ProtocolException (" Unexpected field encoding: $groupOrFieldEncoding . Reader position: $pos . Last read tag: $tag . " )
217217 }
218218 }
219219 return - 1
@@ -246,7 +246,7 @@ open class ProtoReader(private val source: BufferedSource) {
246246 private fun skipGroup (expectedEndTag : Int ) {
247247 while (pos < limit && ! source.exhausted()) {
248248 val tagAndFieldEncoding = internalReadVarint32()
249- if (tagAndFieldEncoding == 0 ) throw ProtocolException (" Unexpected tag 0" )
249+ if (tagAndFieldEncoding == 0 ) throw ProtocolException (" Unexpected tag 0. Reader position: $pos . Last read tag: $tag . " )
250250 val tag = tagAndFieldEncoding shr TAG_FIELD_ENCODING_BITS
251251 when (val groupOrFieldEncoding = tagAndFieldEncoding and FIELD_ENCODING_MASK ) {
252252 STATE_START_GROUP -> {
@@ -263,7 +263,7 @@ open class ProtoReader(private val source: BufferedSource) {
263263 }
264264 STATE_END_GROUP -> {
265265 if (tag == expectedEndTag) return // Success!
266- throw ProtocolException (" Unexpected end group" )
266+ throw ProtocolException (" Unexpected end group. Reader position: $pos . Last read tag: $tag . " )
267267 }
268268 STATE_LENGTH_DELIMITED -> {
269269 val length = internalReadVarint32()
@@ -282,7 +282,7 @@ open class ProtoReader(private val source: BufferedSource) {
282282 state = STATE_FIXED32
283283 readFixed32()
284284 }
285- else -> throw ProtocolException (" Unexpected field encoding: $groupOrFieldEncoding " )
285+ else -> throw ProtocolException (" Unexpected field encoding: $groupOrFieldEncoding . Reader position: $pos . Last read tag: $tag . " )
286286 }
287287 }
288288 throw EOFException ()
@@ -322,7 +322,7 @@ open class ProtoReader(private val source: BufferedSource) {
322322 STATE_FIXED32 ,
323323 -> true // Not packed.
324324
325- else -> throw ProtocolException (" unexpected state: $state " )
325+ else -> throw ProtocolException (" unexpected state: $state . Reader position: $pos . Last read tag: $tag . " )
326326 }
327327 }
328328
@@ -340,7 +340,7 @@ open class ProtoReader(private val source: BufferedSource) {
340340 @Throws(IOException ::class )
341341 open fun readVarint32 (): Int {
342342 if (state != STATE_VARINT && state != STATE_LENGTH_DELIMITED ) {
343- throw ProtocolException (" Expected VARINT or LENGTH_DELIMITED but was $state " )
343+ throw ProtocolException (" Expected VARINT or LENGTH_DELIMITED but was $state . Reader position: $pos . Last read tag: $tag . " )
344344 }
345345 val result = internalReadVarint32()
346346 afterPackableScalar(STATE_VARINT )
@@ -389,7 +389,7 @@ open class ProtoReader(private val source: BufferedSource) {
389389 return result
390390 }
391391 }
392- throw ProtocolException (" Malformed VARINT" )
392+ throw ProtocolException (" Malformed VARINT. Reader position: $pos . Last read tag: $tag . " )
393393 }
394394 }
395395 }
@@ -401,7 +401,7 @@ open class ProtoReader(private val source: BufferedSource) {
401401 @Throws(IOException ::class )
402402 open fun readVarint64 (): Long {
403403 if (state != STATE_VARINT && state != STATE_LENGTH_DELIMITED ) {
404- throw ProtocolException (" Expected VARINT or LENGTH_DELIMITED but was $state " )
404+ throw ProtocolException (" Expected VARINT or LENGTH_DELIMITED but was $state . Reader position: $pos . Last read tag: $tag . " )
405405 }
406406 var shift = 0
407407 var result: Long = 0
@@ -416,14 +416,14 @@ open class ProtoReader(private val source: BufferedSource) {
416416 }
417417 shift + = 7
418418 }
419- throw ProtocolException (" WireInput encountered a malformed varint " )
419+ throw ProtocolException (" Malformed VARINT. Reader position: $pos . Last read tag: $tag . " )
420420 }
421421
422422 /* * Reads a 32-bit little-endian integer from the stream. */
423423 @Throws(IOException ::class )
424424 open fun readFixed32 (): Int {
425425 if (state != STATE_FIXED32 && state != STATE_LENGTH_DELIMITED ) {
426- throw ProtocolException (" Expected FIXED32 or LENGTH_DELIMITED but was $state " )
426+ throw ProtocolException (" Expected FIXED32 or LENGTH_DELIMITED but was $state . Reader position: $pos . Last read tag: $tag . " )
427427 }
428428 source.require(4 ) // Throws EOFException if insufficient bytes are available.
429429 pos + = 4
@@ -436,7 +436,7 @@ open class ProtoReader(private val source: BufferedSource) {
436436 @Throws(IOException ::class )
437437 open fun readFixed64 (): Long {
438438 if (state != STATE_FIXED64 && state != STATE_LENGTH_DELIMITED ) {
439- throw ProtocolException (" Expected FIXED64 or LENGTH_DELIMITED but was $state " )
439+ throw ProtocolException (" Expected FIXED64 or LENGTH_DELIMITED but was $state . Reader position: $pos . Last read tag: $tag . " )
440440 }
441441 source.require(8 ) // Throws EOFException if insufficient bytes are available.
442442 pos + = 8
@@ -466,7 +466,7 @@ open class ProtoReader(private val source: BufferedSource) {
466466 @Throws(IOException ::class )
467467 private fun beforeLengthDelimitedScalar (): Long {
468468 if (state != STATE_LENGTH_DELIMITED ) {
469- throw ProtocolException (" Expected LENGTH_DELIMITED but was $state " )
469+ throw ProtocolException (" Expected LENGTH_DELIMITED but was $state . Reader position: $pos . Last read tag: $tag . " )
470470 }
471471 val byteCount = limit - pos
472472 source.require(byteCount) // Throws EOFException if insufficient bytes are available.
0 commit comments