Skip to content

Commit 80b125a

Browse files
committed
added getSize, incrSize and resetSize methods in Input.java for getting size of deserialized data to Unpacker objects
1 parent 4766178 commit 80b125a

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
package org.msgpack.io;
19+
20+
21+
abstract class AbstractInput implements Input {
22+
private long size = 0;
23+
24+
public long getSize() {
25+
return size;
26+
}
27+
28+
public void incrSize(long s) {
29+
size += s;
30+
}
31+
32+
public void resetSize() {
33+
size = 0;
34+
}
35+
}

src/main/java/org/msgpack/io/Input.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ public interface Input extends Closeable {
4141
public float getFloat() throws IOException;
4242

4343
public double getDouble() throws IOException;
44+
45+
public long getSize();
46+
47+
public void resetSize();
4448
}
4549

src/main/java/org/msgpack/io/LinkedBufferInput.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
import java.nio.ByteBuffer;
2424

2525

26-
public class LinkedBufferInput implements Input {
26+
public class LinkedBufferInput extends AbstractInput {
2727
private LinkedList<ByteBuffer> link;
28+
2829
private int writable;
30+
2931
private int nextAdvance;
3032

3133
private byte[] tmpBuffer;
34+
3235
private ByteBuffer tmpByteBuffer;
3336

3437
private final int bufferSize;
@@ -50,10 +53,12 @@ public int read(byte[] b, int off, int len) throws EOFException {
5053
ByteBuffer bb = link.peekFirst();
5154
if(len < bb.remaining()) {
5255
bb.get(b, off, len);
56+
incrSize(olen);
5357
return olen;
5458
}
5559
int rem = bb.remaining();
5660
bb.get(b, off, rem);
61+
incrSize(rem);
5762
len -= rem;
5863
off += rem;
5964
if(!removeFirstLink(bb)) {
@@ -76,6 +81,7 @@ public boolean tryRefer(BufferReferer ref, int len) throws IOException {
7681
try {
7782
bb.limit(pos+len);
7883
ref.refer(bb, true);
84+
incrSize(len);
7985
success = true;
8086
} finally {
8187
bb.limit(lim);
@@ -97,6 +103,7 @@ public byte readByte() throws EOFException {
97103
throw new EndOfBufferException();
98104
}
99105
byte result = bb.get();
106+
incrSize(1);
100107
if(bb.remaining() == 0) {
101108
removeFirstLink(bb);
102109
}
@@ -170,6 +177,7 @@ private ByteBuffer require(int n) throws EOFException {
170177
return bb;
171178
} else {
172179
requireMore(n);
180+
incrSize(n);
173181
nextAdvance = n;
174182
return tmpByteBuffer;
175183
}

src/main/java/org/msgpack/io/StreamInput.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.io.EOFException;
2424

2525

26-
public class StreamInput implements Input {
26+
public class StreamInput extends AbstractInput {
2727
private final InputStream in;
2828

2929
private byte[] castBuffer;
@@ -41,6 +41,7 @@ public int read(byte[] b, int off, int len) throws IOException {
4141
int remain = len;
4242
while(remain > 0) {
4343
int n = in.read(b, off, remain);
44+
incrSize(remain);
4445
if(n <= 0) {
4546
throw new EOFException();
4647
}
@@ -56,6 +57,7 @@ public boolean tryRefer(BufferReferer ref, int size) throws IOException {
5657

5758
public byte readByte() throws IOException {
5859
int n = in.read();
60+
incrSize(1);
5961
if(n < 0) {
6062
throw new EOFException();
6163
}
@@ -69,6 +71,7 @@ public void advance() {
6971
private void require(int len) throws IOException {
7072
while(filled < len) {
7173
int n = in.read(castBuffer, filled, len - filled);
74+
incrSize(len - filled);
7275
if(n < 0) {
7376
throw new EOFException();
7477
}

0 commit comments

Comments
 (0)