-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move jdk-serialize and add test cases (#417)
* move jdk-serialize and add test cases * rename package * repalce old license header * restore Redundant renaming
- Loading branch information
Showing
25 changed files
with
1,761 additions
and
10 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
dubbo-serialization-extensions/dubbo-serialization-jdk/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.dubbo.extensions</groupId> | ||
<artifactId>dubbo-serialization-extensions</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>dubbo-serialization-jdk</artifactId> | ||
<packaging>jar</packaging> | ||
<name>${project.artifactId}</name> | ||
<description>The jdk serialization module of dubbo project</description> | ||
<version>${revision}</version> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo-serialization-api</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo-common</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>hessian-lite</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
58 changes: 58 additions & 0 deletions
58
...-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedJavaSerialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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 org.apache.dubbo.common.serialize.java; | ||
|
||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.common.serialize.ObjectInput; | ||
import org.apache.dubbo.common.serialize.ObjectOutput; | ||
import org.apache.dubbo.common.serialize.Serialization; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
import static org.apache.dubbo.common.serialize.Constants.COMPACTED_JAVA_SERIALIZATION_ID; | ||
|
||
/** | ||
* Compacted java serialization implementation | ||
* | ||
* <pre> | ||
* e.g. <dubbo:protocol serialization="compactedjava" /> | ||
* </pre> | ||
*/ | ||
public class CompactedJavaSerialization implements Serialization { | ||
|
||
@Override | ||
public byte getContentTypeId() { | ||
return COMPACTED_JAVA_SERIALIZATION_ID; | ||
} | ||
|
||
@Override | ||
public String getContentType() { | ||
return "x-application/compactedjava"; | ||
} | ||
|
||
@Override | ||
public ObjectOutput serialize(URL url, OutputStream out) throws IOException { | ||
return new JavaObjectOutput(out, true); | ||
} | ||
|
||
@Override | ||
public ObjectInput deserialize(URL url, InputStream is) throws IOException { | ||
return new JavaObjectInput(is, true); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...-jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 org.apache.dubbo.common.serialize.java; | ||
|
||
import org.apache.dubbo.common.utils.ClassUtils; | ||
|
||
import java.io.EOFException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectStreamClass; | ||
import java.io.StreamCorruptedException; | ||
|
||
/** | ||
* Compacted java object input implementation | ||
*/ | ||
public class CompactedObjectInputStream extends ObjectInputStream { | ||
private ClassLoader mClassLoader; | ||
|
||
public CompactedObjectInputStream(InputStream in) throws IOException { | ||
this(in, Thread.currentThread().getContextClassLoader()); | ||
} | ||
|
||
public CompactedObjectInputStream(InputStream in, ClassLoader cl) throws IOException { | ||
super(in); | ||
mClassLoader = cl == null ? ClassUtils.getClassLoader() : cl; | ||
} | ||
|
||
@Override | ||
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { | ||
int type = read(); | ||
if (type < 0) { | ||
throw new EOFException(); | ||
} | ||
switch (type) { | ||
case 0: | ||
return super.readClassDescriptor(); | ||
case 1: | ||
Class<?> clazz = loadClass(readUTF()); | ||
return ObjectStreamClass.lookup(clazz); | ||
default: | ||
throw new StreamCorruptedException("Unexpected class descriptor type: " + type); | ||
} | ||
} | ||
|
||
private Class<?> loadClass(String className) throws ClassNotFoundException { | ||
return mClassLoader.loadClass(className); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...jdk/src/main/java/org/apache/dubbo/common/serialize/java/CompactedObjectOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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 org.apache.dubbo.common.serialize.java; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.io.ObjectStreamClass; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* Compacted java object output implementation | ||
*/ | ||
public class CompactedObjectOutputStream extends ObjectOutputStream { | ||
public CompactedObjectOutputStream(OutputStream out) throws IOException { | ||
super(out); | ||
} | ||
|
||
@Override | ||
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { | ||
Class<?> clazz = desc.forClass(); | ||
if (clazz.isPrimitive() || clazz.isArray()) { | ||
write(0); | ||
super.writeClassDescriptor(desc); | ||
} else { | ||
write(1); | ||
writeUTF(desc.getName()); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...rialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 org.apache.dubbo.common.serialize.java; | ||
|
||
import org.apache.dubbo.common.serialize.nativejava.NativeJavaObjectInput; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.ObjectInputStream; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Java object input implementation | ||
*/ | ||
public class JavaObjectInput extends NativeJavaObjectInput { | ||
public static final int MAX_BYTE_ARRAY_LENGTH = 8 * 1024 * 1024; | ||
|
||
public JavaObjectInput(InputStream is) throws IOException { | ||
super(new ObjectInputStream(is)); | ||
} | ||
|
||
public JavaObjectInput(InputStream is, boolean compacted) throws IOException { | ||
super(compacted ? new CompactedObjectInputStream(is) : new ObjectInputStream(is)); | ||
} | ||
|
||
@Override | ||
public byte[] readBytes() throws IOException { | ||
int len = getObjectInputStream().readInt(); | ||
if (len < 0) { | ||
return null; | ||
} | ||
if (len == 0) { | ||
return new byte[0]; | ||
} | ||
if (len > MAX_BYTE_ARRAY_LENGTH) { | ||
throw new IOException("Byte array length too large. " + len); | ||
} | ||
|
||
byte[] b = new byte[len]; | ||
getObjectInputStream().readFully(b); | ||
return b; | ||
} | ||
|
||
@Override | ||
public String readUTF() throws IOException { | ||
int len = getObjectInputStream().readInt(); | ||
if (len < 0) { | ||
return null; | ||
} | ||
|
||
return getObjectInputStream().readUTF(); | ||
} | ||
|
||
@Override | ||
public Object readObject() throws IOException, ClassNotFoundException { | ||
byte b = getObjectInputStream().readByte(); | ||
if (b == 0) { | ||
return null; | ||
} | ||
|
||
return getObjectInputStream().readObject(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException { | ||
return (T) readObject(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException { | ||
return (T) readObject(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ialization-jdk/src/main/java/org/apache/dubbo/common/serialize/java/JavaObjectOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 org.apache.dubbo.common.serialize.java; | ||
|
||
import org.apache.dubbo.common.serialize.nativejava.NativeJavaObjectOutput; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* Java object output implementation | ||
*/ | ||
public class JavaObjectOutput extends NativeJavaObjectOutput { | ||
public JavaObjectOutput(OutputStream os) throws IOException { | ||
super(new ObjectOutputStream(os)); | ||
} | ||
|
||
public JavaObjectOutput(OutputStream os, boolean compact) throws IOException { | ||
super(compact ? new CompactedObjectOutputStream(os) : new ObjectOutputStream(os)); | ||
} | ||
|
||
@Override | ||
public void writeUTF(String v) throws IOException { | ||
if (v == null) { | ||
getObjectOutputStream().writeInt(-1); | ||
} else { | ||
getObjectOutputStream().writeInt(v.length()); | ||
getObjectOutputStream().writeUTF(v); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeObject(Object obj) throws IOException { | ||
if (obj == null) { | ||
getObjectOutputStream().writeByte(0); | ||
} else { | ||
getObjectOutputStream().writeByte(1); | ||
getObjectOutputStream().writeObject(obj); | ||
} | ||
} | ||
|
||
@Override | ||
public void flushBuffer() throws IOException { | ||
getObjectOutputStream().flush(); | ||
} | ||
} |
Oops, something went wrong.