Skip to content

Commit 49d80ea

Browse files
committed
2 parents 383fe05 + 02bd129 commit 49d80ea

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ target
55
.settings
66
.iml
77
*~
8+
._*
9+
.DS_Store
10+
build
11+
lib

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,23 @@ public <T> T read(byte[] bytes, T v) throws IOException {
340340
return read(bytes, v, tmpl);
341341
}
342342

343+
/**
344+
* Deserializes byte array to object.
345+
*
346+
* @since 0.6.0
347+
* @param bytes
348+
* input byte array
349+
* @param v
350+
* @return
351+
* @throws IOException
352+
*/
353+
public <T> T read(byte[] bytes, int off, int len, Class<T> c) throws IOException {
354+
@SuppressWarnings("unchecked")
355+
Template<T> tmpl = registry.lookup(c);
356+
BufferUnpacker u = createBufferUnpacker(bytes, off, len);
357+
return (T) tmpl.read(u, null);
358+
}
359+
343360
/**
344361
* Deserializes byte array to object according to template.
345362
*

src/main/java/org/msgpack/template/builder/ArrayTemplateBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.msgpack.packer.Packer;
3030
import org.msgpack.template.AbstractTemplate;
3131
import org.msgpack.template.BooleanArrayTemplate;
32+
import org.msgpack.template.ByteArrayTemplate;
3233
import org.msgpack.template.DoubleArrayTemplate;
3334
import org.msgpack.template.FieldList;
3435
import org.msgpack.template.FloatArrayTemplate;
@@ -156,6 +157,8 @@ private Template toTemplate(Type arrayType, Type genericBaseType, Class baseClas
156157
return FloatArrayTemplate.getInstance();
157158
} else if (baseClass == double.class) {
158159
return DoubleArrayTemplate.getInstance();
160+
} else if (baseClass == byte.class) {
161+
return ByteArrayTemplate.getInstance();
159162
} else {
160163
Template baseTemplate = registry.lookup(genericBaseType);
161164
return new ObjectArrayTemplate(baseClass, baseTemplate);

src/test/java/org/msgpack/TestSimpleArrays.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public void testGenerics() throws Exception {
262262
@Message
263263
public static class Dim2Test {
264264
public int[][] i;
265+
public byte[][] b;
265266
public String[][] str;
266267
public List<String>[][] slist;
267268

@@ -277,6 +278,9 @@ public void testDim2() throws Exception {
277278
t.i = new int[2][];
278279
t.i[0] = new int[] { 0, 1 };
279280
t.i[1] = new int[] { 2, 3, 4 };
281+
t.b = new byte[2][];
282+
t.b[0] = new byte[] { 5, 6 };
283+
t.b[1] = new byte[] { 7, 8, 9 };
280284
t.str = new String[2][];
281285
t.str[0] = new String[] { "aa", "bb" };
282286
t.str[1] = new String[] { "cc", "dd", "ee" };
@@ -298,21 +302,28 @@ public void testDim2() throws Exception {
298302
byte[] raw = packer.toByteArray();
299303
BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
300304
Dim2Test u = unpacker.read(Dim2Test.class);
301-
assertEquals(t.i.length, t.i.length);
305+
assertEquals(t.i.length, u.i.length);
302306
for (int i = 0; i < t.i.length; i++) {
303307
assertEquals(t.i[i].length, u.i[i].length);
304308
for (int j = 0; j < t.i[i].length; j++) {
305309
assertEquals(t.i[i][j], u.i[i][j]);
306310
}
307311
}
308-
assertEquals(t.str.length, t.str.length);
312+
assertEquals(t.b.length, u.b.length);
313+
for (int i = 0; i < t.b.length; i++) {
314+
assertEquals(t.b[i].length, u.b[i].length);
315+
for (int j = 0; j < t.i[i].length; j++) {
316+
assertEquals(t.b[i][j], u.b[i][j]);
317+
}
318+
}
319+
assertEquals(t.str.length, u.str.length);
309320
for (int i = 0; i < t.str.length; i++) {
310321
assertEquals(t.str[i].length, u.str[i].length);
311322
for (int j = 0; j < t.str[i].length; j++) {
312323
assertEquals(t.str[i][j], u.str[i][j]);
313324
}
314325
}
315-
assertEquals(t.slist.length, t.slist.length);
326+
assertEquals(t.slist.length, u.slist.length);
316327
for (int i = 0; i < t.slist.length; i++) {
317328
assertEquals(t.slist[i].length, u.slist[i].length);
318329
for (int j = 0; j < t.slist[i].length; j++) {

0 commit comments

Comments
 (0)