From d0a44759a1025f7152e5b09274a3ae714c07b8b0 Mon Sep 17 00:00:00 2001 From: HeYuJie <1445499792@qq.com> Date: Wed, 30 Oct 2024 14:21:45 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9InputStreamDeserializer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hessian/io/InputStreamDeserializer.java | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java index cbe92bf1..1f1192be 100644 --- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java +++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java @@ -48,20 +48,77 @@ package com.alibaba.com.caucho.hessian.io; -import java.io.IOException; +import java.io.*; +import java.nio.file.Files; +import java.util.UUID; /** + * InputStream类型返序列化 * Serializing a stream object. */ public class InputStreamDeserializer extends AbstractDeserializer { public static final InputStreamDeserializer DESER = new InputStreamDeserializer(); + // TODO 允许通过外部配置 + @SuppressWarnings("FieldCanBeLocal") + private final int bufferSize = Hessian2Output.SIZE; + // TODO 允许通过外部配置 + private final File tmpDir; + public InputStreamDeserializer() { + tmpDir = new File(System.getProperty("java.io.tmpdir"), "dubbo-"+ System.currentTimeMillis()); + if(!tmpDir.exists()){ + //noinspection ResultOfMethodCallIgnored + tmpDir.mkdirs(); + } } - public Object readObject(AbstractHessianInput in) - throws IOException { - return in.readInputStream(); + public Object readObject(AbstractHessianInput in) throws IOException { + + FileOutputStream out = null; + try { + @SuppressWarnings("resource") + InputStream input = in.readInputStream(); + // 读取配置的2倍字节(16k) + byte[] bytes = new byte[bufferSize * 2]; + File file = null; + while (true) { // 循环读取 + + int len = input.read(bytes, 0, bytes.length); + + if (out == null) { + + // 如果InputStream的长度小于缓存,则创建字节流返回 + if (len <= bufferSize) { + byte[] buff = new byte[len]; + System.arraycopy(bytes, 0, buff, 0, len); + return new ByteArrayInputStream(buff); + } + + // 如果InputStream的长度大于缓存,则创建临时文件返回 + String name = String.format("%d-%s.dubbo.tmp", System.currentTimeMillis(), UUID.randomUUID().toString().replace("-", "")); + file = new File(tmpDir, name); + // 在 finally中关闭流 + //noinspection resource + out = new FileOutputStream(file); + } + + // 读取到末尾时退出 + if (len == -1) { + break; + } + out.write(bytes, 0 ,len); + } + + out.flush(); + return Files.newInputStream(file.toPath()); + } finally { + if (out != null){ + out.close(); + } + } } + + } From 375b7513b62013d04474d343f531eca77ccef925 Mon Sep 17 00:00:00 2001 From: HeYuJie <1445499792@qq.com> Date: Wed, 30 Oct 2024 14:28:03 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9InputStreamDeserializer.j?= =?UTF-8?q?ava=20=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96InputStream=E6=97=B6?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E8=AF=BB=E5=8F=96=E5=AE=8C=E4=BB=96=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E8=8A=82=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=B8=8D=E8=AF=BB?= =?UTF-8?q?=E5=88=99=E5=9C=A8org.apache.dubbo.rpc.protocol.dubbo.Decodeabl?= =?UTF-8?q?eRpcResult#decode:handleAttachment(in);=20=E6=8A=9B=E5=87=BAexp?= =?UTF-8?q?ected=20map/object=20at=20java.lang.String=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/com/caucho/hessian/io/InputStreamDeserializer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java index 1f1192be..f3055a08 100644 --- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java +++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java @@ -120,5 +120,4 @@ public Object readObject(AbstractHessianInput in) throws IOException { } } - } From 3cb3b843468f2a354ec8b96a181c99d0e510045d Mon Sep 17 00:00:00 2001 From: HeYuJie <1445499792@qq.com> Date: Wed, 30 Oct 2024 15:01:18 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0InputStreamDeserializer?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hessian/io/InputStreamDeserializer.java | 4 +- .../HessianInputStreamDeserializerTest.java | 136 ++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java index f3055a08..ca43d73d 100644 --- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java +++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java @@ -49,7 +49,6 @@ package com.alibaba.com.caucho.hessian.io; import java.io.*; -import java.nio.file.Files; import java.util.UUID; /** @@ -112,7 +111,8 @@ public Object readObject(AbstractHessianInput in) throws IOException { } out.flush(); - return Files.newInputStream(file.toPath()); + //noinspection IOStreamConstructor + return new FileInputStream(file); } finally { if (out != null){ out.close(); diff --git a/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java b/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java new file mode 100644 index 00000000..121d4cd6 --- /dev/null +++ b/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java @@ -0,0 +1,136 @@ +/* + * 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 org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; + +import java.io.*; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +/** + * InputStreamDeserializer 测试 + */ +public class HessianInputStreamDeserializerTest extends SerializeTestBase { + + protected T baseHessian2Serialize(T data) throws IOException { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + Hessian2Output out = new Hessian2Output(bout); + + out.writeObject(data); + out.flush(); + + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); + Hessian2Input input = new Hessian2Input(bin); + Object obj = input.readObject(InputStream.class); + return (T) obj; + } + + byte[] returnBytes(int size){ + byte[] bytes = new byte[size]; + Arrays.fill(bytes, (byte) 'A'); + bytes[0] = '&'; + bytes[bytes.length-3] = '$'; + bytes[bytes.length-2] = '%'; + bytes[bytes.length-1] = '#'; + return bytes; + } + + /** + * 测试1024字节,返回 ByteArrayInputStream + */ + @Test + @EnabledForJreRange(max = JRE.JAVA_11) + public void test1024Byte() throws IOException { + int size = 1024; + byte[] bytes = returnBytes(size); + InputStream source = new ByteArrayInputStream(bytes); + InputStream result = baseHessian2Serialize(source); + + byte[] resultByte = new byte[size]; + result.read(resultByte); + + assertInstanceOf(ByteArrayInputStream.class, result, "类型错误"); + assertEquals(bytes.length, resultByte.length); + assertEquals(new String(bytes), new String(resultByte)); + } + + /** + * 测试8192字节,返回 ByteArrayInputStream + */ + @Test + @EnabledForJreRange(max = JRE.JAVA_11) + public void test8192Byte() throws IOException { + int size = 8192; + byte[] bytes = returnBytes(size); + InputStream source = new ByteArrayInputStream(bytes); + InputStream result = baseHessian2Serialize(source); + + byte[] resultByte = new byte[size]; + result.read(resultByte); + + assertInstanceOf(ByteArrayInputStream.class, result, "类型错误"); + assertEquals(bytes.length, resultByte.length); + assertEquals(new String(bytes), new String(resultByte)); + } + + /** + * 测试8193字节,返回 ByteArrayInputStream + * 比缓冲区多一个字节 + */ + @Test + @EnabledForJreRange(max = JRE.JAVA_11) + public void test8193Byte() throws IOException { + int size = 8193; + byte[] bytes = returnBytes(size); + InputStream source = new ByteArrayInputStream(bytes); + InputStream result = baseHessian2Serialize(source); + + byte[] resultByte = new byte[size]; + result.read(resultByte); + + assertInstanceOf(FileInputStream.class, result, "类型错误"); + assertEquals(bytes.length, resultByte.length); + assertEquals(new String(bytes), new String(resultByte)); + } + + /** + * 测试81920字节,返回 ByteArrayInputStream + * 比缓冲区多10倍字节 + */ + @Test + @EnabledForJreRange(max = JRE.JAVA_11) + public void test81920Byte() throws IOException { + int size = 81920; + byte[] bytes = returnBytes(size); + InputStream source = new ByteArrayInputStream(bytes); + InputStream result = baseHessian2Serialize(source); + + byte[] resultByte = new byte[size]; + result.read(resultByte); + + assertInstanceOf(FileInputStream.class, result, "类型错误"); + assertEquals(bytes.length, resultByte.length); + assertEquals(new String(bytes), new String(resultByte)); + } + +} From cc0dd684343f172f8595a90f15b399be250cf0a6 Mon Sep 17 00:00:00 2001 From: HeYuJie <1445499792@qq.com> Date: Wed, 30 Oct 2024 15:51:23 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BA=8F=E5=88=97=E5=8C=96=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/caucho/hessian/io/ContextSerializerFactory.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/ContextSerializerFactory.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/ContextSerializerFactory.java index b89d9942..b6c81535 100644 --- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/ContextSerializerFactory.java +++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/ContextSerializerFactory.java @@ -55,12 +55,7 @@ import java.lang.ref.WeakReference; import java.net.InetAddress; import java.net.URL; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Properties; -import java.util.WeakHashMap; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -234,7 +229,7 @@ public static ContextSerializerFactory create(ClassLoader loader) { } } - private static void addBasic(Class cl, String typeName, int type) { + public static void addBasic(Class cl, String typeName, int type) { _staticSerializerMap.put(cl.getName(), new BasicSerializer(type)); Deserializer deserializer = new BasicDeserializer(type); From 1066b98ea5b028bb8b2894b2886d68a350cc1d59 Mon Sep 17 00:00:00 2001 From: HeYuJie <1445499792@qq.com> Date: Thu, 31 Oct 2024 11:35:23 +0800 Subject: [PATCH 5/5] Revise English annotations --- .../hessian/io/InputStreamDeserializer.java | 20 +++++++------ .../HessianInputStreamDeserializerTest.java | 28 +++++++++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java index ca43d73d..a96c9837 100644 --- a/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java +++ b/hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/InputStreamDeserializer.java @@ -52,17 +52,19 @@ import java.util.UUID; /** - * InputStream类型返序列化 + * InputStream Deserializer * Serializing a stream object. + * @author HeYuJie + * @date 2024/10/31 */ public class InputStreamDeserializer extends AbstractDeserializer { public static final InputStreamDeserializer DESER = new InputStreamDeserializer(); - // TODO 允许通过外部配置 + // TODO Allow external configuration @SuppressWarnings("FieldCanBeLocal") private final int bufferSize = Hessian2Output.SIZE; - // TODO 允许通过外部配置 + // TODO Allow external configuration private final File tmpDir; public InputStreamDeserializer() { @@ -79,31 +81,31 @@ public Object readObject(AbstractHessianInput in) throws IOException { try { @SuppressWarnings("resource") InputStream input = in.readInputStream(); - // 读取配置的2倍字节(16k) + // Read twice the size of the buffer (16k) byte[] bytes = new byte[bufferSize * 2]; File file = null; - while (true) { // 循环读取 + while (true) { // Loop reading int len = input.read(bytes, 0, bytes.length); if (out == null) { - // 如果InputStream的长度小于缓存,则创建字节流返回 + // If the length of InputStream is less than the buffer, creating a byte stream returns if (len <= bufferSize) { byte[] buff = new byte[len]; System.arraycopy(bytes, 0, buff, 0, len); return new ByteArrayInputStream(buff); } - // 如果InputStream的长度大于缓存,则创建临时文件返回 + // If the length of InputStream is greater than the buffer, create a temporary file and return it String name = String.format("%d-%s.dubbo.tmp", System.currentTimeMillis(), UUID.randomUUID().toString().replace("-", "")); file = new File(tmpDir, name); - // 在 finally中关闭流 + // Close the stream in finally //noinspection resource out = new FileOutputStream(file); } - // 读取到末尾时退出 + // Exit when reading to the end if (len == -1) { break; } diff --git a/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java b/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java index 121d4cd6..7cd94f18 100644 --- a/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java +++ b/java-8-test/src/test/java/com/alibaba/com/caucho/hessian/io/HessianInputStreamDeserializerTest.java @@ -28,7 +28,9 @@ import static org.junit.jupiter.api.Assertions.assertInstanceOf; /** - * InputStreamDeserializer 测试 + * InputStreamDeserializer test + * @author HeYuJie + * @date 2024/10/31 */ public class HessianInputStreamDeserializerTest extends SerializeTestBase { @@ -56,7 +58,8 @@ byte[] returnBytes(int size){ } /** - * 测试1024字节,返回 ByteArrayInputStream + * test 1024 byte + * deserialize return ByteArrayInputStream */ @Test @EnabledForJreRange(max = JRE.JAVA_11) @@ -69,13 +72,14 @@ public void test1024Byte() throws IOException { byte[] resultByte = new byte[size]; result.read(resultByte); - assertInstanceOf(ByteArrayInputStream.class, result, "类型错误"); + assertInstanceOf(ByteArrayInputStream.class, result, "type error"); assertEquals(bytes.length, resultByte.length); assertEquals(new String(bytes), new String(resultByte)); } /** - * 测试8192字节,返回 ByteArrayInputStream + * test 8192 byte + * deserialize return ByteArrayInputStream */ @Test @EnabledForJreRange(max = JRE.JAVA_11) @@ -88,14 +92,15 @@ public void test8192Byte() throws IOException { byte[] resultByte = new byte[size]; result.read(resultByte); - assertInstanceOf(ByteArrayInputStream.class, result, "类型错误"); + assertInstanceOf(ByteArrayInputStream.class, result, "type error"); assertEquals(bytes.length, resultByte.length); assertEquals(new String(bytes), new String(resultByte)); } /** - * 测试8193字节,返回 ByteArrayInputStream - * 比缓冲区多一个字节 + * test 8193 byte + * One byte more than the buffer + * deserialize return FileInputStream */ @Test @EnabledForJreRange(max = JRE.JAVA_11) @@ -108,14 +113,15 @@ public void test8193Byte() throws IOException { byte[] resultByte = new byte[size]; result.read(resultByte); - assertInstanceOf(FileInputStream.class, result, "类型错误"); + assertInstanceOf(FileInputStream.class, result, "type error"); assertEquals(bytes.length, resultByte.length); assertEquals(new String(bytes), new String(resultByte)); } /** - * 测试81920字节,返回 ByteArrayInputStream - * 比缓冲区多10倍字节 + * test 81920 byte + * 10 times more bytes than the buffer + * deserialize return FileInputStream */ @Test @EnabledForJreRange(max = JRE.JAVA_11) @@ -128,7 +134,7 @@ public void test81920Byte() throws IOException { byte[] resultByte = new byte[size]; result.read(resultByte); - assertInstanceOf(FileInputStream.class, result, "类型错误"); + assertInstanceOf(FileInputStream.class, result, "type error"); assertEquals(bytes.length, resultByte.length); assertEquals(new String(bytes), new String(resultByte)); }