Skip to content

Commit

Permalink
Fix hessian2 serialized short, byte is converted to int bug (apache#1232
Browse files Browse the repository at this point in the history
)

* Fix hessian2 serialized short, byte is converted to int bug

* Fix hessian2 serialized short, byte is converted to int bug

* adapt jdk1.5+
  • Loading branch information
商宗海 authored and lovepoem committed Feb 8, 2018
1 parent b87d3ad commit 03a8322
Show file tree
Hide file tree
Showing 11 changed files with 619 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,27 @@ public Object readList(AbstractHessianInput in, int length)
throw new UnsupportedOperationException(String.valueOf(this));
}

@Override
public Object readList(AbstractHessianInput in, int length, Class<?> expectType) throws IOException {
if(expectType == null) {
return readList(in, length);
}
throw new UnsupportedOperationException(String.valueOf(this));
}

public Object readLengthList(AbstractHessianInput in, int length)
throws IOException {
throw new UnsupportedOperationException(String.valueOf(this));
}

@Override
public Object readLengthList(AbstractHessianInput in, int length, Class<?> expectType) throws IOException {
if(expectType == null){
return readLengthList(in , length);
}
throw new UnsupportedOperationException(String.valueOf(this));
}

public Object readMap(AbstractHessianInput in)
throws IOException {
Object obj = in.readObject();
Expand All @@ -92,6 +108,14 @@ public Object readMap(AbstractHessianInput in)
throw error(className + ": unexpected null value");
}

@Override
public Object readMap(AbstractHessianInput in, Class<?> expectKeyType, Class<?> expectValueType) throws IOException {
if(expectKeyType == null && expectValueType == null){
return readMap(in);
}
throw new UnsupportedOperationException(String.valueOf(this));
}

public Object readObject(AbstractHessianInput in, String[] fieldNames)
throws IOException {
throw new UnsupportedOperationException(String.valueOf(this));
Expand All @@ -107,4 +131,15 @@ protected String codeName(int ch) {
else
return "0x" + Integer.toHexString(ch & 0xff);
}

protected SerializerFactory findSerializerFactory(AbstractHessianInput in) {
SerializerFactory serializerFactory = null;
if(in instanceof Hessian2Input) {
serializerFactory = ((Hessian2Input) in).findSerializerFactory();
}
else if(in instanceof HessianInput) {
serializerFactory = ((HessianInput) in).getSerializerFactory();
}
return serializerFactory == null? new SerializerFactory(): serializerFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;

/**
* Abstract base class for Hessian requests. Hessian users should only
Expand Down Expand Up @@ -334,12 +335,34 @@ abstract public byte[] readBytes()
abstract public Object readObject(Class expectedClass)
throws IOException;

/**
* Reads an arbitrary object from the input stream.
*
* @param expectedClass the expected class if the protocol doesn't supply it.
* @param expectedTypes the runtime type hints, eg: expectedClass equals Map, expectedTypes can
* equals String.class, Short.class
*/
public Object readObject(Class expectedClass, Class<?>... expectedTypes)
throws IOException{
throw new UnsupportedOperationException(String.valueOf(this));
}

/**
* Reads an arbitrary object from the input stream.
*/
abstract public Object readObject()
throws IOException;

/**
* Reads an arbitrary object from the input stream.
* @param expectedTypes the runtime type hints, eg: expectedTypes can
* equals String.class, Short.class for HashMap
*/
public Object readObject(List<Class<?>> expectedTypes)
throws IOException{
throw new UnsupportedOperationException(String.valueOf(this));
}

/**
* Reads a remote object reference to the stream. The type is the
* type of the remote interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ public Class getType() {

public Object readList(AbstractHessianInput in, int length)
throws IOException {
return readList(in, length, null);
}

@Override
public Object readList(AbstractHessianInput in, int length, Class<?> expectType) throws IOException {
Collection list = createList();

in.addRef(list);

Deserializer deserializer = null;

SerializerFactory factory = findSerializerFactory(in);
if(expectType != null){
deserializer = factory.getDeserializer(expectType.getName());
}

while (!in.isEnd())
list.add(in.readObject());
list.add(deserializer != null ? deserializer.readObject(in) : in.readObject());

in.readEnd();

Expand All @@ -87,12 +99,24 @@ public Object readList(AbstractHessianInput in, int length)

public Object readLengthList(AbstractHessianInput in, int length)
throws IOException {
return readList(in, length, null);
}

@Override
public Object readLengthList(AbstractHessianInput in, int length, Class<?> expectType) throws IOException {
Collection list = createList();

in.addRef(list);

Deserializer deserializer = null;

SerializerFactory factory = findSerializerFactory(in);
if(expectType != null){
deserializer = factory.getDeserializer(expectType.getName());
}

for (; length > 0; length--)
list.add(in.readObject());
list.add(deserializer != null ? deserializer.readObject(in) : in.readObject());

return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

/**
* Deserializing an object.
*
* @author jason.shang@hotmail.com
*/
public interface Deserializer {
public Class getType();
Expand All @@ -62,12 +64,49 @@ public Object readObject(AbstractHessianInput in)
public Object readList(AbstractHessianInput in, int length)
throws IOException;

/**
*
* deserialize list object from expect type.
*
* @param in
* @param length
* @param expectType
* @return
* @throws IOException
*/
public Object readList(AbstractHessianInput in, int length, Class<?> expectType)
throws IOException;

public Object readLengthList(AbstractHessianInput in, int length)
throws IOException;

/**
*
* deserialize list object from expect type.
*
* @param in
* @param length
* @param expectType
* @return
* @throws IOException
*/
public Object readLengthList(AbstractHessianInput in, int length, Class<?> expectType)
throws IOException;

public Object readMap(AbstractHessianInput in)
throws IOException;

/**
* deserialize map object from expect key and value type.
* @param in
* @param expectKeyType
* @param expectValueType
* @return
* @throws IOException
*/
public Object readMap(AbstractHessianInput in, Class<?> expectKeyType, Class<?> expectValueType )
throws IOException;

public Object readObject(AbstractHessianInput in, String[] fieldNames)
throws IOException;
}
Loading

0 comments on commit 03a8322

Please sign in to comment.