Skip to content

Commit d0bcf70

Browse files
committed
improved programs for getting size of read bytes
1 parent d67f5eb commit d0bcf70

9 files changed

Lines changed: 99 additions & 96 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2011 Muga Nishizawa
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+
23+
private long count = 0;
24+
25+
public long getReadByteCount() {
26+
return count;
27+
}
28+
29+
public void resetReadByteCount() {
30+
count = 0;
31+
}
32+
33+
protected final void incrReadByteCount(long size) {
34+
count += size;
35+
}
36+
37+
protected final void incrReadOneByteCount() {
38+
count += 1;
39+
}
40+
}

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 getReadByteCount();
46+
47+
public void resetReadByteCount();
4448
}
4549

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.nio.ByteBuffer;
2424

2525

26-
public class LinkedBufferInput implements Input {
26+
public class LinkedBufferInput extends AbstractInput {// FIXME #MN
2727
private LinkedList<ByteBuffer> link;
2828

2929
private int writable;
@@ -49,17 +49,19 @@ public int read(byte[] b, int off, int len) throws EOFException {
4949
return 0;
5050
}
5151
int olen = len;
52-
while(true) {
52+
while (true) {
5353
ByteBuffer bb = link.peekFirst();
54-
if(len < bb.remaining()) {
54+
if (len < bb.remaining()) {
5555
bb.get(b, off, len);
56+
incrReadByteCount(len);
5657
return olen;
5758
}
5859
int rem = bb.remaining();
5960
bb.get(b, off, rem);
61+
incrReadByteCount(rem);
6062
len -= rem;
6163
off += rem;
62-
if(!removeFirstLink(bb)) {
64+
if (!removeFirstLink(bb)) {
6365
break;
6466
}
6567
}
@@ -68,26 +70,27 @@ public int read(byte[] b, int off, int len) throws EOFException {
6870

6971
public boolean tryRefer(BufferReferer ref, int len) throws IOException {
7072
ByteBuffer bb = link.peekFirst();
71-
if(bb == null) {
73+
if (bb == null) {
7274
throw new EndOfBufferException();
73-
} else if(bb.remaining() < len) {
75+
} else if (bb.remaining() < len) {
7476
return false;
7577
}
7678
boolean success = false;
7779
int pos = bb.position();
7880
int lim = bb.limit();
7981
try {
80-
bb.limit(pos+len);
82+
bb.limit(pos + len);
8183
ref.refer(bb, true);
84+
this.incrReadByteCount(len);
8285
success = true;
8386
} finally {
8487
bb.limit(lim);
85-
if(success) {
86-
bb.position(pos+len);
88+
if (success) {
89+
bb.position(pos + len);
8790
} else {
8891
bb.position(pos);
8992
}
90-
if(bb.remaining() == 0) {
93+
if (bb.remaining() == 0) {
9194
removeFirstLink(bb);
9295
}
9396
}
@@ -96,10 +99,11 @@ public boolean tryRefer(BufferReferer ref, int len) throws IOException {
9699

97100
public byte readByte() throws EOFException {
98101
ByteBuffer bb = link.peekFirst();
99-
if(bb == null || bb.remaining() == 0) {
102+
if (bb == null || bb.remaining() == 0) {
100103
throw new EndOfBufferException();
101104
}
102105
byte result = bb.get();
106+
incrReadOneByteCount();
103107
if(bb.remaining() == 0) {
104108
removeFirstLink(bb);
105109
}
@@ -146,8 +150,8 @@ private boolean removeFirstLink(ByteBuffer first) {
146150

147151
private void requireMore(int n) throws EOFException {
148152
int off = 0;
149-
for(ByteBuffer bb : link) {
150-
if(n <= bb.remaining()) {
153+
for (ByteBuffer bb : link) {
154+
if (n <= bb.remaining()) {
151155
int pos = bb.position();
152156
bb.get(tmpBuffer, off, n);
153157
bb.position(pos);
@@ -170,9 +174,11 @@ private ByteBuffer require(int n) throws EOFException {
170174
}
171175
if(n <= bb.remaining()) {
172176
nextAdvance = n;
177+
incrReadByteCount(n);
173178
return bb;
174179
} else {
175180
requireMore(n);
181+
incrReadByteCount(n);
176182
nextAdvance = n;
177183
return tmpByteBuffer;
178184
}

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

Lines changed: 8 additions & 5 deletions
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 {// FIXME #MN
2727
private final InputStream in;
2828

2929
private byte[] castBuffer;
@@ -39,9 +39,10 @@ public StreamInput(InputStream in) {
3939

4040
public int read(byte[] b, int off, int len) throws IOException {
4141
int remain = len;
42-
while(remain > 0) {
42+
while (remain > 0) {
4343
int n = in.read(b, off, remain);
44-
if(n <= 0) {
44+
incrReadByteCount(remain);
45+
if (n <= 0) {
4546
throw new EOFException();
4647
}
4748
remain -= n;
@@ -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+
incrReadOneByteCount();
5961
if(n < 0) {
6062
throw new EOFException();
6163
}
@@ -67,9 +69,10 @@ public void advance() {
6769
}
6870

6971
private void require(int len) throws IOException {
70-
while(filled < len) {
72+
while (filled < len) {
7173
int n = in.read(castBuffer, filled, len - filled);
72-
if(n < 0) {
74+
incrReadByteCount(len - filled);
75+
if (n < 0) {
7376
throw new EOFException();
7477
}
7578
filled += n;

src/main/java/org/msgpack/unpacker/AbstractUnpacker.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
public abstract class AbstractUnpacker implements Unpacker {
2929
protected MessagePack msgpack;
3030

31-
protected long messageSize;
32-
3331
protected AbstractUnpacker(MessagePack msgpack) {
3432
this.msgpack = msgpack;
3533
}
@@ -104,35 +102,11 @@ public <T> T read(T to, Template<T> tmpl) throws IOException {
104102
return (T) tmpl.read(this, to);
105103
}
106104

107-
public long getLastMessageSize() {
108-
return messageSize;
109-
}
110-
111-
public void setMessageSizeLimit(long size) {
105+
public long getReadByteCount() {
112106
throw new UnsupportedOperationException("Not implemented yet");
113107
}
114108

115-
protected void incrMessageSize(long size) {
116-
messageSize += size;
117-
}
118-
119-
protected void incrMessageSizeOne() {
120-
messageSize += 1;
121-
}
122-
123-
protected void incrMessageSizeTwo() {
124-
messageSize += 2;
125-
}
126-
127-
protected void incrMessageSizeFour() {
128-
messageSize += 4;
129-
}
130-
131-
protected void incrMessageSizeEight() {
132-
messageSize += 8;
133-
}
134-
135-
protected void resetMessageSize() {
136-
messageSize = 0;
109+
public void resetReadByteCount() {
110+
throw new UnsupportedOperationException("Not implemented yet");
137111
}
138112
}

src/main/java/org/msgpack/unpacker/Converter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public void close() throws IOException {
408408
}
409409

410410
@Override
411-
public long getLastMessageSize() {
411+
public long getReadByteCount() {
412412
throw new UnsupportedOperationException("Not implemented yet");
413413
}
414414
}

0 commit comments

Comments
 (0)