From 6493368285ce77792f6076954d4311a9a457a826 Mon Sep 17 00:00:00 2001 From: "kenton@google.com" Date: Thu, 12 Nov 2009 18:13:33 +0000 Subject: CodedInputStream.getTotalBytesRead(); patch from Michael Kucharski. --- .../main/java/com/google/protobuf/CodedInputStream.java | 16 ++++++++++++++-- .../java/com/google/protobuf/CodedInputStreamTest.java | 7 +++++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'java') diff --git a/java/src/main/java/com/google/protobuf/CodedInputStream.java b/java/src/main/java/com/google/protobuf/CodedInputStream.java index 9125957d..f339c00e 100644 --- a/java/src/main/java/com/google/protobuf/CodedInputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedInputStream.java @@ -467,7 +467,9 @@ public final class CodedInputStream { /** * The total number of bytes read before the current buffer. The total * bytes read up to the current position can be computed as - * {@code totalBytesRetired + bufferPos}. + * {@code totalBytesRetired + bufferPos}. This value may be negative if + * reading started in the middle of the current buffer (e.g. if the + * constructor that takes a byte array and an offset was used). */ private int totalBytesRetired; @@ -489,6 +491,7 @@ public final class CodedInputStream { this.buffer = buffer; bufferSize = off + len; bufferPos = off; + totalBytesRetired = -off; input = null; } @@ -496,6 +499,7 @@ public final class CodedInputStream { buffer = new byte[BUFFER_SIZE]; bufferSize = 0; bufferPos = 0; + totalBytesRetired = 0; this.input = input; } @@ -546,7 +550,7 @@ public final class CodedInputStream { * Resets the current size counter to zero (see {@link #setSizeLimit(int)}). */ public void resetSizeCounter() { - totalBytesRetired = 0; + totalBytesRetired = -bufferPos; } /** @@ -615,6 +619,14 @@ public final class CodedInputStream { return bufferPos == bufferSize && !refillBuffer(false); } + /** + * The total bytes read up to the current position. Calling + * {@link #resetSizeCounter()} resets this value to zero. + */ + public int getTotalBytesRead() { + return totalBytesRetired + bufferPos; + } + /** * Called with {@code this.buffer} is empty to read more bytes from the * input. If {@code mustSucceed} is true, refillBuffer() gurantees that diff --git a/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java index 850b8aa7..a75a400b 100644 --- a/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java +++ b/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java @@ -434,6 +434,7 @@ public class CodedInputStreamTest extends TestCase { new SmallBlockInputStream(new byte[256], 8)); input.setSizeLimit(16); input.readRawBytes(16); + assertEquals(16, input.getTotalBytesRead()); try { input.readRawByte(); @@ -443,7 +444,10 @@ public class CodedInputStreamTest extends TestCase { } input.resetSizeCounter(); + assertEquals(0, input.getTotalBytesRead()); input.readRawByte(); // No exception thrown. + input.resetSizeCounter(); + assertEquals(0, input.getTotalBytesRead()); try { input.readRawBytes(16); // Hits limit again. @@ -477,10 +481,13 @@ public class CodedInputStreamTest extends TestCase { public void testReadFromSlice() throws Exception { byte[] bytes = bytes(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); CodedInputStream in = CodedInputStream.newInstance(bytes, 3, 5); + assertEquals(0, in.getTotalBytesRead()); for (int i = 3; i < 8; i++) { assertEquals(i, in.readRawByte()); + assertEquals(i-2, in.getTotalBytesRead()); } // eof assertEquals(0, in.readTag()); + assertEquals(5, in.getTotalBytesRead()); } } -- cgit v1.2.3