Skip to content
Permalink
Browse files
perf: Implement init() method for reuse Hessian2Output and Hessian2In…
…put object
  • Loading branch information
LinShunKang committed Mar 18, 2020
1 parent cd29011 commit 3e991dffd19114b877f8aef3782bee9ccab0f696
Showing 3 changed files with 100 additions and 14 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,6 +181,25 @@ 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();
}
}

@Override
public boolean checkAndReadNull() {
try {
@@ -454,12 +470,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();
@@ -3392,7 +3405,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;
@@ -3450,7 +3466,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,46 @@
/*
* 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 org.junit.Assert;
import org.junit.Test;

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

public class Hessian2ReuseTest {

@Test
public void test() throws IOException {
Hessian2Input h2i = new Hessian2Input(null);
Hessian2Output h2o = new Hessian2Output(null);
for (int i = 0; i < 100; i++) {
String str = "Hello, Hessian2, round:" + i;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
h2o.init(outputStream);
h2o.writeObject(str);
h2o.flush();

byte[] bytes = outputStream.toByteArray();

h2i.init(new ByteArrayInputStream(bytes));
String newStr = (String) h2i.readObject(String.class);
Assert.assertEquals(str, newStr);
}
}
}

0 comments on commit 3e991df

Please sign in to comment.