Skip to content

Commit 78e16a9

Browse files
committed
added cross-language test
1 parent f18db10 commit 78e16a9

17 files changed

Lines changed: 164 additions & 32 deletions

src/main/java/org/msgpack/MessagePack.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,19 @@ public Value unpack(ByteBuffer buf) throws IOException { // TODO IOException
100100
}
101101

102102
public void pack(OutputStream out, Object v) throws IOException {
103-
Template tmpl = registry.lookup(v.getClass());
103+
Template tmpl = getTemplate(v.getClass());
104104
tmpl.write(new StreamPacker(out), v);
105105
}
106106

107+
public byte[] pack(Value v) throws IOException { // TODO IOException
108+
// FIXME ValueTemplate should do this
109+
BufferPacker pk = new BufferPacker();
110+
pk.write(v);
111+
return pk.toByteArray();
112+
}
113+
107114
public byte[] pack(Object v) throws IOException { // TODO IOException
108-
Template tmpl = registry.lookup(v.getClass());
115+
Template tmpl = getTemplate(v.getClass());
109116
BufferPacker pk = new BufferPacker();
110117
tmpl.write(pk, v);
111118
return pk.toByteArray();
@@ -168,6 +175,7 @@ public static void loadDefaultTemplates(TemplateRegistry reg) {
168175
reg.register(Short.class, ShortTemplate.getInstance());
169176
reg.register(short.class, ShortTemplate.getInstance());
170177
reg.register(int[].class, IntArrayTemplate.getInstance());
178+
reg.register(Value.class, ValueTemplate.getInstance());
171179
}
172180
}
173181

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,11 @@ public void feed(ByteBuffer buf, boolean nocopy) {
250250
int pos = bb.position();
251251
bb.position(bb.limit());
252252
bb.limit(bb.limit() + writable);
253-
bb.put(buf); // FIXME BufferOverflowException
253+
buf.limit(writable);
254+
bb.put(buf);
254255
bb.position(pos);
255256
rem -= writable;
257+
buf.limit(buf.limit() + rem);
256258
writable = 0;
257259
}
258260

src/main/java/org/msgpack/packer/AbstractMessagePackPacker.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public void writeBytes(byte[] b, int off, int len) throws IOException {
198198
out.writeByteAndInt((byte)0xdb, len);
199199
}
200200
out.write(b, off, len);
201+
stack.reduceCount();
201202
}
202203

203204
@Override
@@ -216,12 +217,6 @@ public void writeString(String s) throws IOException {
216217
@Override
217218
public void writeArrayBegin(int size) throws IOException {
218219
// TODO check size < 0?
219-
if(size == 0) {
220-
stack.reduceCount();
221-
out.writeByte((byte)0x90);
222-
stack.reduceCount();
223-
return;
224-
}
225220
if(size < 16) {
226221
// FixArray
227222
out.writeByte((byte)(0x90 | size));
@@ -242,7 +237,7 @@ public void writeArrayEnd(boolean check) throws IOException {
242237
int remain = stack.getTopCount();
243238
if(remain > 0) {
244239
if(check) {
245-
throw new MessageTypeException("writeArrayEnd(check=true) is called but the array is not end");
240+
throw new MessageTypeException("writeArrayEnd(check=true) is called but the array is not end: "+remain);
246241
}
247242
for(int i=0; i < remain; i++) {
248243
writeNil();
@@ -255,12 +250,6 @@ public void writeArrayEnd(boolean check) throws IOException {
255250
@Override
256251
public void writeMapBegin(int size) throws IOException {
257252
// TODO check size < 0?
258-
if(size == 0) {
259-
stack.reduceCount();
260-
out.writeByte((byte)0x80);
261-
stack.reduceCount();
262-
return;
263-
}
264253
if(size < 16) {
265254
// FixMap
266255
out.writeByte((byte)(0x80 | size));
@@ -275,13 +264,13 @@ public void writeMapBegin(int size) throws IOException {
275264
@Override
276265
public void writeMapEnd(boolean check) throws IOException {
277266
if(!stack.topIsMap()) {
278-
throw new MessageTypeException("writeArrayEnd() is called but writeArrayBegin() is not called");
267+
throw new MessageTypeException("writeMapEnd() is called but writeMapBegin() is not called");
279268
}
280269

281270
int remain = stack.getTopCount();
282271
if(remain > 0) {
283272
if(check) {
284-
throw new MessageTypeException("writeArrayEnd(check=true) is called but the array is not end");
273+
throw new MessageTypeException("writeMapEnd(check=true) is called but the map is not end: "+remain);
285274
}
286275
for(int i=0; i < remain; i++) {
287276
writeNil();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Copyright (C) 2009-2010 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.template;
19+
20+
import java.io.IOException;
21+
import org.msgpack.packer.Packer;
22+
import org.msgpack.unpacker.Unpacker;
23+
import org.msgpack.MessageTypeException;
24+
import org.msgpack.value.Value;
25+
26+
public class ValueTemplate implements Template {
27+
private ValueTemplate() { }
28+
29+
public void write(Packer pk, Object target) throws IOException {
30+
if(target == null) {
31+
// FIXME NullPointerException?
32+
throw new MessageTypeException("Trying to write null.");
33+
}
34+
pk.write((Value)target);
35+
}
36+
37+
public Object read(Unpacker u, Object to) throws IOException {
38+
return u.readValue();
39+
}
40+
41+
static public ValueTemplate getInstance() {
42+
return instance;
43+
}
44+
45+
static final ValueTemplate instance = new ValueTemplate();
46+
}
47+

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import java.nio.ByteBuffer;
2424
import java.math.BigInteger;
2525
import java.util.NoSuchElementException;
26+
import java.lang.Iterable;
2627
import org.msgpack.value.Value;
2728
import org.msgpack.packer.Unconverter;
2829
import org.msgpack.io.EndOfBufferException;
2930

30-
public abstract class Unpacker {
31+
public abstract class Unpacker implements Iterable<Value> {
3132
public abstract void readNil() throws IOException;
3233

3334
public abstract boolean tryReadNil() throws IOException;

src/main/java/org/msgpack/value/AbstractArrayValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.AbstractList;
2121
import org.msgpack.MessageTypeException;
2222

23-
public abstract class AbstractArrayValue extends AbstractList<Value> implements ArrayValue {
23+
abstract class AbstractArrayValue extends AbstractList<Value> implements ArrayValue {
2424
public ValueType getType() {
2525
return ValueType.ARRAY;
2626
}

src/main/java/org/msgpack/value/AbstractBooleanValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//
1818
package org.msgpack.value;
1919

20-
public abstract class AbstractBooleanValue extends AbstractValue implements BooleanValue {
20+
abstract class AbstractBooleanValue extends AbstractValue implements BooleanValue {
2121
public ValueType getType() {
2222
return ValueType.BOOLEAN;
2323
}

src/main/java/org/msgpack/value/AbstractMapValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.AbstractMap;
2121
import org.msgpack.MessageTypeException;
2222

23-
public abstract class AbstractMapValue extends AbstractMap<Value,Value> implements MapValue {
23+
abstract class AbstractMapValue extends AbstractMap<Value,Value> implements MapValue {
2424
public ValueType getType() {
2525
return ValueType.MAP;
2626
}

src/main/java/org/msgpack/value/AbstractRawValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import java.util.Arrays;
2121

22-
public abstract class AbstractRawValue extends AbstractValue implements RawValue {
22+
abstract class AbstractRawValue extends AbstractValue implements RawValue {
2323
public ValueType getType() {
2424
return ValueType.RAW;
2525
}

src/main/java/org/msgpack/value/AbstractValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.math.BigInteger;
2424
import org.msgpack.MessageTypeException;
2525

26-
public abstract class AbstractValue implements Value {
26+
abstract class AbstractValue implements Value {
2727
public boolean isNil() {
2828
return false;
2929
}

0 commit comments

Comments
 (0)