Skip to content
Permalink
Browse files
Merge remote-tracking branch 'origin/master'
  • Loading branch information
chickenlj committed Mar 27, 2020
2 parents b4a7fee + 5d16bbc commit 2ac26a54f926211501686dd57b0cfc6294cf031d
Showing 8 changed files with 315 additions and 24 deletions.
@@ -114,9 +114,6 @@
private boolean _isStreaming;
// the method for a call
private String _method;
private int _argLength;
private Reader _chunkReader;
private InputStream _chunkInputStream;
private Throwable _replyFault;
private StringBuilder _sbuf = new StringBuilder();
// true if this is the last chunk
@@ -184,15 +181,38 @@ public Throwable getReplyFault() {
return _replyFault;
}

@Override
public void init(InputStream is) {
_is = is;

reset();
}

public void reset() {
resetReferences();

if (_classDefs != null) {
_classDefs.clear();
}

if (_types != null) {
_types.clear();
}

_offset = 0;
_length = 0;
}

@Override
public boolean checkAndReadNull() {
try {
int tmp_offset = _offset;
int tag = read();
if ('N' == tag) {
return true;
}
_offset = tmp_offset;
if (-1 != tag) {
_offset--;
}
} catch (IOException ignored) {
}
return false;
@@ -455,12 +475,9 @@ public int startMessage()
throws IOException {
int tag = read();

if (tag == 'p')
_isStreaming = false;
else if (tag == 'P')
_isStreaming = true;
else
if (tag != 'p' && tag != 'P') {
throw error("expected Hessian message ('p') at " + codeName(tag));
}

int major = read();
int minor = read();
@@ -3393,7 +3410,10 @@ public InputStream readInputStream()
_isLastChunk = true;
_chunkLength = tag - 0x20;
break;
case 0x34: case 0x35: case 0x36: case 0x37:
case 0x34:
case 0x35:
case 0x36:
case 0x37:
_isLastChunk = true;
_chunkLength = (tag - 0x34) * 256 + read();
break;
@@ -3451,7 +3471,10 @@ int read(byte[] buffer, int offset, int length)
_isLastChunk = true;
_chunkLength = code - 0x20;
break;
case 0x34: case 0x35: case 0x36: case 0x37:
case 0x34:
case 0x35:
case 0x36:
case 0x37:
_isLastChunk = true;
_chunkLength = (code - 0x34) * 256 + read();
break;
@@ -101,6 +101,30 @@ public Hessian2Output(OutputStream os) {
_os = os;
}

@Override
public void init(OutputStream os) {
reset();

_os = os;
}

/**
* Resets all counters and references
*/
public void reset() {
resetReferences();

if (_classRefs != null) {
_classRefs.clear();
}

if (_typeRefs != null) {
_typeRefs.clear();
}

_offset = 0;
}

public boolean isCloseStreamOnClose() {
return _isCloseStreamOnClose;
}
@@ -384,10 +408,7 @@ public void writeObject(Object object)
return;
}

Serializer serializer;

serializer = findSerializerFactory().getSerializer(object.getClass());

Serializer serializer = findSerializerFactory().getSerializer(object.getClass());
serializer.writeObject(object, this);
}

@@ -0,0 +1,154 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.com.caucho.hessian.io;

import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
import com.alibaba.com.caucho.hessian.io.beans.*;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class Hessian2ReuseTest extends SerializeTestBase {

private static final Hessian2Input h2i = new Hessian2Input(null);

private static final Hessian2Output h2o = new Hessian2Output(null);

@SuppressWarnings("unchecked")
private static <T> T serializeAndDeserialize(T obj, Class<T> clazz) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
h2o.init(outputStream);
h2o.writeObject(obj);
h2o.flush();

h2i.init(new ByteArrayInputStream(outputStream.toByteArray()));
return (T) h2i.readObject(clazz);
}

@Test
public void testString() throws IOException {
for (int i = 0; i < 100; i++) {
String obj = "Hello, Hessian2, round:" + i;

String newObj = serializeAndDeserialize(obj, String.class);
Assert.assertEquals(obj, newObj);

String newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testDate() throws IOException {
long mills = System.currentTimeMillis();
serializeAndDeserialize(new Date(mills), Date.class);
serializeAndDeserialize(new Date(mills + 1000_000), Date.class);
}

@Test
public void testBaseUser() throws IOException {
for (int i = 0; i < 100; i++) {
BaseUser obj = new BaseUser();
obj.setUserId(i * ThreadLocalRandom.current().nextInt(10000));
obj.setUserName(String.valueOf(System.currentTimeMillis()));

BaseUser newObj = serializeAndDeserialize(obj, BaseUser.class);
Assert.assertEquals(obj, newObj);

BaseUser newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testSubUser() throws IOException {
for (int i = 0; i < 100; i++) {
SubUser obj = new SubUser();
obj.setUserId(i * ThreadLocalRandom.current().nextInt(10000));
obj.setUserName(String.valueOf(System.currentTimeMillis()));
obj.setAgeList(Arrays.asList(12, 13, 14, 15));
obj.setSexyList(Arrays.asList(true, false));
obj.setWeightList(Arrays.asList(120D, 130D, 140D));

SubUser newObj = serializeAndDeserialize(obj, SubUser.class);
Assert.assertEquals(obj, newObj);

SubUser newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testGrandsonUser() throws IOException {
for (int i = 0; i < 100; i++) {
GrandsonUser obj = new GrandsonUser();
obj.setUserId(i * ThreadLocalRandom.current().nextInt(10000));
obj.setUserName(String.valueOf(System.currentTimeMillis()));
obj.setAgeList(Arrays.asList(12, 13, 14, 15));
obj.setSexyList(Arrays.asList(true, false));
obj.setWeightList(Arrays.asList(120D, 130D, 140D));

SubUser newObj = serializeAndDeserialize(obj, SubUser.class);
Assert.assertEquals(obj, newObj);

SubUser newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testType() throws IOException {
serializeAndDeserialize(Type.Lower, Type.class);
serializeAndDeserialize(Type.Normal, Type.class);
serializeAndDeserialize(Type.High, Type.class);
}

@Test
public void testHessian2StringShortType() throws IOException {
for (int i = 0; i < 100; i++) {
Hessian2StringShortType obj = new Hessian2StringShortType();
obj.shortSet = new HashSet<>();
obj.stringShortMap = new HashMap<>();
obj.stringByteMap = new HashMap<>();
obj.stringPersonTypeMap = new HashMap<>();

obj.shortSet.add((short) i);
obj.shortSet.add((short) (i * 2));

// shortType.stringShortMap.put(String.valueOf(i), (short) i);
// shortType.stringShortMap.put(String.valueOf(i * 100), (short) (i * 100));

// shortType.stringByteMap.put(String.valueOf(i), (byte) 1);

List<Short> shorts = Arrays.asList((short) 12, (short) 4);
PersonType abc = new PersonType("ABC", 12, 128D, (short) 1, (byte) 2, shorts);
obj.stringPersonTypeMap.put("P_" + i, abc);

Hessian2StringShortType newObj = serializeAndDeserialize(obj, Hessian2StringShortType.class);
Assert.assertEquals(obj, newObj);

Hessian2StringShortType newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

}
@@ -20,10 +20,12 @@
import com.alibaba.com.caucho.hessian.io.beans.Hessian2StringShortType;
import com.alibaba.com.caucho.hessian.io.beans.PersonType;

import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

import static junit.framework.TestCase.assertEquals;
@@ -92,8 +94,8 @@ public void serialize_map_then_deserialize() throws Exception {
public void serialize_map_then_deserialize0() throws Exception {

Map<String, Short> stringShortMap = new HashMap<String, Short>();
stringShortMap.put("first", (short)0);
stringShortMap.put("last", (short)60);
stringShortMap.put("first", (short) 0);
stringShortMap.put("last", (short) 60);

ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bout);
@@ -112,8 +114,8 @@ public void serialize_map_then_deserialize0() throws Exception {
assertTrue(deserialize != null);
assertTrue(deserialize.size() == 2);
assertTrue(deserialize.get("last") instanceof Short);
assertEquals(Short.valueOf((short)0), deserialize.get("first"));
assertEquals(Short.valueOf((short)60), deserialize.get("last"));
assertEquals(Short.valueOf((short) 0), deserialize.get("first"));
assertEquals(Short.valueOf((short) 60), deserialize.get("last"));
}

@Test
@@ -181,8 +183,8 @@ public void serialize_list_then_deserialize() throws Exception {
public void serialize_list_then_deserialize0() throws Exception {

List<Short> shortList = new ArrayList<Short>();
shortList.add((short)0);
shortList.add((short)60);
shortList.add((short) 0);
shortList.add((short) 60);

ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bout);
@@ -200,8 +202,8 @@ public void serialize_list_then_deserialize0() throws Exception {
assertTrue(deserialize != null);
assertTrue(deserialize.size() == 2);
assertTrue(deserialize.get(1) instanceof Short);
assertEquals(Short.valueOf((short)0), deserialize.get(0));
assertEquals(Short.valueOf((short)60), deserialize.get(1));
assertEquals(Short.valueOf((short) 0), deserialize.get(0));
assertEquals(Short.valueOf((short) 60), deserialize.get(1));
}

@Test
@@ -219,4 +221,30 @@ public void serialize_short_set_then_deserialize() throws Exception {
assertTrue(deserialize.shortSet.contains((short) 0));
assertTrue(deserialize.shortSet.contains((short) 60));
}

@Test
public void test_string_short_type() throws IOException {
for (int i = 0; i < 100; i++) {
Hessian2StringShortType obj = new Hessian2StringShortType();
obj.shortSet = new HashSet<>();
obj.stringShortMap = new HashMap<>();
obj.stringByteMap = new HashMap<>();
obj.stringPersonTypeMap = new HashMap<>();

obj.shortSet.add((short) i);
obj.shortSet.add((short) (i * 2));

obj.stringShortMap.put(String.valueOf(i), (short) i);
obj.stringShortMap.put(String.valueOf(i * 100), (short) (i * 100));
obj.stringByteMap.put(String.valueOf(i), (byte) 1);

List<Short> shorts = Arrays.asList((short) 12, (short) 4);
PersonType abc = new PersonType("ABC", 12, 128D, (short) 1, (byte) 2, shorts);
obj.stringPersonTypeMap.put("P_" + i, abc);

Hessian2StringShortType newObj = baseHessian2Serialize(obj);
Assert.assertEquals(obj, newObj);
System.out.println("ShortTypeTest.testHessian2StringShortType(): i=" + i + " passed!");
}
}
}

0 comments on commit 2ac26a5

Please sign in to comment.