Skip to content

Commit b2eb070

Browse files
zonghaishangchickenlj
authored andcommitted
Merge pull request #1616, fix hessian1 serialized short, byte is converted to int.
1 parent bf58e49 commit b2eb070

File tree

3 files changed

+318
-12
lines changed

3 files changed

+318
-12
lines changed

hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/HessianInput.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.ArrayList;
5757
import java.util.Date;
5858
import java.util.HashMap;
59+
import java.util.List;
5960

6061
/**
6162
* Input stream for Hessian requests.
@@ -999,7 +1000,15 @@ private HashMap readFault()
9991000
@Override
10001001
public Object readObject(Class cl)
10011002
throws IOException {
1002-
if (cl == null || cl == Object.class)
1003+
return readObject(cl, null, null);
1004+
}
1005+
1006+
/**
1007+
* Reads an object from the input stream with an expected type.
1008+
*/
1009+
public Object readObject(Class expectedClass, Class<?>... expectedTypes)
1010+
throws IOException {
1011+
if (expectedClass == null || expectedClass == Object.class)
10031012
return readObject();
10041013

10051014
int tag = read();
@@ -1011,17 +1020,23 @@ public Object readObject(Class cl)
10111020
case 'M': {
10121021
String type = readType();
10131022

1023+
boolean keyValuePair = expectedTypes != null && expectedTypes.length == 2;
1024+
10141025
// hessian/3386
10151026
if ("".equals(type)) {
10161027
Deserializer reader;
1017-
reader = _serializerFactory.getDeserializer(cl);
1028+
reader = _serializerFactory.getDeserializer(expectedClass);
10181029

1019-
return reader.readMap(this);
1030+
return reader.readMap(this
1031+
, keyValuePair ? expectedTypes[0] : null
1032+
, keyValuePair ? expectedTypes[1] : null);
10201033
} else {
10211034
Deserializer reader;
1022-
reader = _serializerFactory.getObjectDeserializer(type, cl);
1035+
reader = _serializerFactory.getObjectDeserializer(type, expectedClass);
10231036

1024-
return reader.readMap(this);
1037+
return reader.readMap(this
1038+
, keyValuePair ? expectedTypes[0] : null
1039+
, keyValuePair ? expectedTypes[1] : null);
10251040
}
10261041
}
10271042

@@ -1032,12 +1047,14 @@ public Object readObject(Class cl)
10321047
Deserializer reader;
10331048
reader = _serializerFactory.getObjectDeserializer(type);
10341049

1035-
if (cl != reader.getType() && cl.isAssignableFrom(reader.getType()))
1036-
return reader.readList(this, length);
1050+
boolean valueType = expectedTypes != null && expectedTypes.length == 1;
1051+
1052+
if (expectedClass != reader.getType() && expectedClass.isAssignableFrom(reader.getType()))
1053+
return reader.readList(this, length, valueType ? expectedTypes[0] : null);
10371054

1038-
reader = _serializerFactory.getDeserializer(cl);
1055+
reader = _serializerFactory.getDeserializer(expectedClass);
10391056

1040-
Object v = reader.readList(this, length);
1057+
Object v = reader.readList(this, length, valueType ? expectedTypes[0] : null);
10411058

10421059
return v;
10431060
}
@@ -1061,7 +1078,7 @@ public Object readObject(Class cl)
10611078
// hessian/332i vs hessian/3406
10621079
//return readObject();
10631080

1064-
Object value = _serializerFactory.getDeserializer(cl).readObject(this);
1081+
Object value = _serializerFactory.getDeserializer(expectedClass).readObject(this);
10651082

10661083
return value;
10671084
}
@@ -1073,6 +1090,15 @@ public Object readObject(Class cl)
10731090
@Override
10741091
public Object readObject()
10751092
throws IOException {
1093+
return readObject((List<Class<?>>) null);
1094+
}
1095+
1096+
/**
1097+
* Reads an arbitrary object from the input stream when the type
1098+
* is unknown.
1099+
*/
1100+
public Object readObject(List<Class<?>> expectedTypes)
1101+
throws IOException {
10761102
int tag = read();
10771103

10781104
switch (tag) {
@@ -1137,13 +1163,29 @@ public Object readObject()
11371163
String type = readType();
11381164
int length = readLength();
11391165

1140-
return _serializerFactory.readList(this, length, type);
1166+
Deserializer reader;
1167+
reader = _serializerFactory.getObjectDeserializer(type);
1168+
1169+
boolean valueType = expectedTypes != null && expectedTypes.size() == 1;
1170+
1171+
if (List.class != reader.getType() && List.class.isAssignableFrom(reader.getType()))
1172+
return reader.readList(this, length, valueType ? expectedTypes.get(0) : null);
1173+
1174+
reader = _serializerFactory.getDeserializer(List.class);
1175+
1176+
Object v = reader.readList(this, length, valueType ? expectedTypes.get(0) : null);
1177+
1178+
return v;
11411179
}
11421180

11431181
case 'M': {
11441182
String type = readType();
11451183

1146-
return _serializerFactory.readMap(this, type);
1184+
boolean keyValuePair = expectedTypes != null && expectedTypes.size() == 2;
1185+
1186+
return _serializerFactory.readMap(this, type
1187+
, keyValuePair ? expectedTypes.get(0) : null
1188+
, keyValuePair ? expectedTypes.get(1) : null);
11471189
}
11481190

11491191
case 'R': {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.com.caucho.hessian.io;
18+
19+
import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
20+
import com.alibaba.com.caucho.hessian.io.beans.Hessian2StringShortType;
21+
import com.alibaba.com.caucho.hessian.io.beans.PersonType;
22+
23+
import org.junit.Test;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
33+
import static junit.framework.TestCase.assertEquals;
34+
import static junit.framework.TestCase.assertTrue;
35+
36+
public class Hessian1StringShortTest extends SerializeTestBase {
37+
38+
@Test
39+
public void serialize_string_short_map_then_deserialize() throws Exception {
40+
41+
Hessian2StringShortType stringShort = new Hessian2StringShortType();
42+
Map<String, Short> stringShortMap = new HashMap<String, Short>();
43+
stringShortMap.put("first", (short)0);
44+
stringShortMap.put("last", (short)60);
45+
stringShort.stringShortMap = stringShortMap;
46+
47+
Hessian2StringShortType deserialize = baseHessionSerialize(stringShort);
48+
assertTrue(deserialize.stringShortMap != null);
49+
assertTrue(deserialize.stringShortMap.size() == 2);
50+
assertTrue(deserialize.stringShortMap.get("last") instanceof Short);
51+
assertEquals(Short.valueOf((short)0), deserialize.stringShortMap.get("first"));
52+
assertEquals(Short.valueOf((short)60), deserialize.stringShortMap.get("last"));
53+
}
54+
55+
@Test
56+
public void serialize_string_byte_map_then_deserialize() throws Exception {
57+
58+
Hessian2StringShortType stringShort = new Hessian2StringShortType();
59+
Map<String, Byte> stringByteMap = new HashMap<String, Byte>();
60+
stringByteMap.put("first", (byte)0);
61+
stringByteMap.put("last", (byte)60);
62+
stringShort.stringByteMap = stringByteMap;
63+
64+
Hessian2StringShortType deserialize = baseHessionSerialize(stringShort);
65+
assertTrue(deserialize.stringByteMap != null);
66+
assertTrue(deserialize.stringByteMap.size() == 2);
67+
assertTrue(deserialize.stringByteMap.get("last") instanceof Byte);
68+
assertEquals(Byte.valueOf((byte)0), deserialize.stringByteMap.get("first"));
69+
assertEquals(Byte.valueOf((byte) 60), deserialize.stringByteMap.get("last"));
70+
}
71+
72+
@Test
73+
public void serialize_map_then_deserialize() throws Exception {
74+
75+
Map<String, Short> stringShortMap = new HashMap<String, Short>();
76+
stringShortMap.put("first", (short)0);
77+
stringShortMap.put("last", (short)60);
78+
79+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
80+
HessianOutput out = new HessianOutput(bout);
81+
82+
out.writeObject(stringShortMap);
83+
out.flush();
84+
85+
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
86+
HessianInput input = new HessianInput(bin);
87+
Map deserialize = (Map) input.readObject(HashMap.class, String.class, Short.class);
88+
assertTrue(deserialize != null);
89+
assertTrue(deserialize.size() == 2);
90+
assertTrue(deserialize.get("last") instanceof Short);
91+
assertEquals(Short.valueOf((short)0), deserialize.get("first"));
92+
assertEquals(Short.valueOf((short)60), deserialize.get("last"));
93+
}
94+
95+
@Test
96+
public void serialize_map_then_deserialize0() throws Exception {
97+
98+
Map<String, Short> stringShortMap = new HashMap<String, Short>();
99+
stringShortMap.put("first", (short)0);
100+
stringShortMap.put("last", (short)60);
101+
102+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
103+
HessianOutput out = new HessianOutput(bout);
104+
105+
out.writeObject(stringShortMap);
106+
out.flush();
107+
108+
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
109+
HessianInput input = new HessianInput(bin);
110+
List<Class<?>> keyValueType = new ArrayList<Class<?>>();
111+
keyValueType.add(String.class);
112+
keyValueType.add(short.class);
113+
114+
Map deserialize = (Map) input.readObject(keyValueType);
115+
assertTrue(deserialize != null);
116+
assertTrue(deserialize.size() == 2);
117+
assertTrue(deserialize.get("last") instanceof Short);
118+
assertEquals(Short.valueOf((short)0), deserialize.get("first"));
119+
assertEquals(Short.valueOf((short)60), deserialize.get("last"));
120+
}
121+
122+
@Test
123+
public void serialize_string_person_map_then_deserialize() throws Exception {
124+
125+
Hessian2StringShortType stringShort = new Hessian2StringShortType();
126+
Map<String, PersonType> stringPersonTypeMap = new HashMap<String, PersonType>();
127+
stringPersonTypeMap.put("first", new PersonType(
128+
"jason.shang", 26, (double) 0.1, (short)1, (byte)2, Arrays.asList((short)1,(short)1)
129+
));
130+
stringPersonTypeMap.put("last", new PersonType(
131+
"jason.shang2", 52, (double) 0.2, (short)2, (byte)4, Arrays.asList((short)2,(short)2)
132+
));
133+
stringShort.stringPersonTypeMap = stringPersonTypeMap;
134+
135+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
136+
HessianOutput out = new HessianOutput(bout);
137+
138+
out.writeObject(stringShort);
139+
out.flush();
140+
141+
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
142+
HessianInput input = new HessianInput(bin);
143+
144+
Hessian2StringShortType deserialize = (Hessian2StringShortType) input.readObject();
145+
assertTrue(deserialize.stringPersonTypeMap != null);
146+
assertTrue(deserialize.stringPersonTypeMap.size() == 2);
147+
assertTrue(deserialize.stringPersonTypeMap.get("last") instanceof PersonType);
148+
149+
150+
assertEquals(new PersonType(
151+
"jason.shang", 26, (double) 0.1, (short)1, (byte)2, Arrays.asList((short)1,(short)1)
152+
), deserialize.stringPersonTypeMap.get("first"));
153+
154+
assertEquals(new PersonType(
155+
"jason.shang2", 52, (double) 0.2, (short)2, (byte)4, Arrays.asList((short)2,(short)2)
156+
), deserialize.stringPersonTypeMap.get("last"));
157+
158+
}
159+
160+
@Test
161+
public void serialize_list_then_deserialize() throws Exception {
162+
163+
List<Short> shortList = new ArrayList<Short>();
164+
shortList.add((short)0);
165+
shortList.add((short)60);
166+
167+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
168+
HessianOutput out = new HessianOutput(bout);
169+
170+
out.writeObject(shortList);
171+
out.flush();
172+
173+
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
174+
HessianInput input = new HessianInput(bin);
175+
List<Short> deserialize = (List) input.readObject(ArrayList.class, Short.class);
176+
assertTrue(deserialize != null);
177+
assertTrue(deserialize.size() == 2);
178+
assertTrue(deserialize.get(1) instanceof Short);
179+
assertEquals(Short.valueOf((short)0), deserialize.get(0));
180+
assertEquals(Short.valueOf((short)60), deserialize.get(1));
181+
}
182+
183+
@Test
184+
public void serialize_list_then_deserialize0() throws Exception {
185+
186+
List<Short> shortList = new ArrayList<Short>();
187+
shortList.add((short)0);
188+
shortList.add((short)60);
189+
190+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
191+
HessianOutput out = new HessianOutput(bout);
192+
193+
out.writeObject(shortList);
194+
out.flush();
195+
196+
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
197+
HessianInput input = new HessianInput(bin);
198+
199+
List<Class<?>> valueType = new ArrayList<Class<?>>();
200+
valueType.add(short.class);
201+
202+
List<Short> deserialize = (List) input.readObject(valueType);
203+
assertTrue(deserialize != null);
204+
assertTrue(deserialize.size() == 2);
205+
assertTrue(deserialize.get(1) instanceof Short);
206+
assertEquals(Short.valueOf((short)0), deserialize.get(0));
207+
assertEquals(Short.valueOf((short)60), deserialize.get(1));
208+
}
209+
210+
}

0 commit comments

Comments
 (0)