Skip to content

Commit 8010bf4

Browse files
committed
added TestLinkedBufferOutput
1 parent 9adaa41 commit 8010bf4

5 files changed

Lines changed: 114 additions & 5 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public abstract class BufferedOutput implements Output {
2727
protected ByteBuffer castByteBuffer;
2828

2929
public BufferedOutput(int bufferSize) {
30-
if(bufferSize < 8) {
31-
bufferSize = 8;
30+
if(bufferSize < 9) {
31+
bufferSize = 9;
3232
}
3333
this.bufferSize = bufferSize;
3434
}
@@ -49,6 +49,14 @@ private void reserve(int len) throws IOException {
4949
}
5050

5151
public void write(byte[] b, int off, int len) throws IOException {
52+
if(buffer == null) {
53+
if(bufferSize < len) {
54+
flushBuffer(b, off, len);
55+
return;
56+
}
57+
buffer = new byte[bufferSize];
58+
castByteBuffer = ByteBuffer.wrap(buffer);
59+
}
5260
if(buffer.length - filled < len) {
5361
System.arraycopy(b, off, buffer, filled, len);
5462
filled += len;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public byte[] toByteArray() {
5555
}
5656

5757
public int getSize() {
58-
return size;
58+
return size + filled;
5959
}
6060

6161
public void flush() {

src/test/java/org/msgpack/TestCrossLang.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.msgpack.util;
1+
package org.msgpack;
22

33
import java.math.BigInteger;
44
import java.nio.ByteBuffer;

src/test/java/org/msgpack/TestSimplePackUnpack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.msgpack.util;
1+
package org.msgpack;
22

33
import java.math.BigInteger;
44
import java.nio.ByteBuffer;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.msgpack.io;
2+
3+
import java.math.BigInteger;
4+
import java.nio.ByteBuffer;
5+
import java.io.IOException;
6+
import java.io.DataOutputStream;
7+
import java.io.ByteArrayOutputStream;
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Arrays;
14+
15+
import org.junit.Test;
16+
import org.msgpack.MessagePack;
17+
import org.msgpack.value.Value;
18+
19+
import junit.framework.TestCase;
20+
21+
public class TestLinkedBufferOutput extends TestCase {
22+
@Test
23+
public void testGetSize() throws IOException {
24+
LinkedBufferOutput o = new LinkedBufferOutput(10);
25+
for(int i=0; i < 21; i++) {
26+
o.writeByte((byte)1);
27+
assertEquals(i+1, o.getSize());
28+
}
29+
}
30+
31+
@Test
32+
public void testWritePrimitives() throws IOException {
33+
ByteArrayOutputStream bo = new ByteArrayOutputStream();
34+
DataOutputStream o1 = new DataOutputStream(bo);
35+
LinkedBufferOutput o2 = new LinkedBufferOutput(10);
36+
o1.writeByte((byte)2);
37+
o2.writeByte((byte)2);
38+
o1.writeShort((short)2);
39+
o2.writeShort((short)2);
40+
o1.writeInt((int)2);
41+
o2.writeInt((int)2);
42+
o1.writeLong(2L);
43+
o2.writeLong(2L);
44+
o1.writeFloat(1.1f);
45+
o2.writeFloat(1.1f);
46+
o1.writeDouble(1.1);
47+
o2.writeDouble(1.1);
48+
byte[] b1 = bo.toByteArray();
49+
byte[] b2 = o2.toByteArray();
50+
assertEquals(b1.length, b2.length);
51+
assertTrue(Arrays.equals(b1, b2));
52+
}
53+
54+
@Test
55+
public void testByteAndWritePrimitives() throws IOException {
56+
ByteArrayOutputStream bo = new ByteArrayOutputStream();
57+
DataOutputStream o1 = new DataOutputStream(bo);
58+
LinkedBufferOutput o2 = new LinkedBufferOutput(10);
59+
o1.writeByte((byte)9);
60+
o1.writeByte((byte)2);
61+
o2.writeByteAndByte((byte)9, (byte)2);
62+
o1.writeByte((byte)9);
63+
o1.writeShort((short)2);
64+
o2.writeByteAndShort((byte)9, (short)2);
65+
o1.writeByte((byte)9);
66+
o1.writeInt((int)2);
67+
o2.writeByteAndInt((byte)9, (int)2);
68+
o1.writeByte((byte)9);
69+
o1.writeLong(2L);
70+
o2.writeByteAndLong((byte)9, 2L);
71+
o1.writeByte((byte)9);
72+
o1.writeFloat(1.1f);
73+
o2.writeByteAndFloat((byte)9, 1.1f);
74+
o1.writeByte((byte)9);
75+
o1.writeDouble(1.1);
76+
o2.writeByteAndDouble((byte)9, 1.1);
77+
byte[] b1 = bo.toByteArray();
78+
byte[] b2 = o2.toByteArray();
79+
assertEquals(b1.length, b2.length);
80+
assertTrue(Arrays.equals(b1, b2));
81+
}
82+
83+
@Test
84+
public void testWrite() throws IOException {
85+
ByteArrayOutputStream bo = new ByteArrayOutputStream();
86+
DataOutputStream o1 = new DataOutputStream(bo);
87+
LinkedBufferOutput o2 = new LinkedBufferOutput(10);
88+
byte[] raw = new byte[9];
89+
raw[0] = (byte)1;
90+
raw[7] = (byte)1;
91+
for(int i=0; i < 11; i++) {
92+
o1.write(raw, 0, raw.length);
93+
o2.write(raw, 0, raw.length);
94+
}
95+
byte[] b1 = bo.toByteArray();
96+
byte[] b2 = o2.toByteArray();
97+
assertEquals(b1.length, b2.length);
98+
assertTrue(Arrays.equals(b1, b2));
99+
}
100+
}
101+

0 commit comments

Comments
 (0)