|
20 | 20 | import org.apache.dubbo.common.logger.LoggerFactory; |
21 | 21 | import org.apache.dubbo.common.serialize.Cleanable; |
22 | 22 | import org.apache.dubbo.common.serialize.ObjectInput; |
| 23 | +import org.apache.dubbo.common.utils.ArrayUtils; |
23 | 24 | import org.apache.dubbo.common.utils.Assert; |
24 | 25 | import org.apache.dubbo.common.utils.StringUtils; |
25 | 26 | import org.apache.dubbo.remoting.Channel; |
@@ -79,19 +80,21 @@ public Object decode(Channel channel, InputStream input) throws IOException { |
79 | 80 | case DubboCodec.RESPONSE_NULL_VALUE: |
80 | 81 | break; |
81 | 82 | case DubboCodec.RESPONSE_VALUE: |
82 | | - setResponseResult(in, true, false, false); |
| 83 | + handleValue(in); |
83 | 84 | break; |
84 | 85 | case DubboCodec.RESPONSE_WITH_EXCEPTION: |
85 | | - setResponseResult(in, false, true, false); |
| 86 | + handleException(in); |
86 | 87 | break; |
87 | 88 | case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS: |
88 | | - setResponseResult(in, false, false, true); |
| 89 | + handleAttachment(in); |
89 | 90 | break; |
90 | 91 | case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS: |
91 | | - setResponseResult(in, true, false, true); |
| 92 | + handleValue(in); |
| 93 | + handleAttachment(in); |
92 | 94 | break; |
93 | 95 | case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS: |
94 | | - setResponseResult(in, false, true, false); |
| 96 | + handleException(in); |
| 97 | + handleAttachment(in); |
95 | 98 | break; |
96 | 99 | default: |
97 | 100 | throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag); |
@@ -119,28 +122,44 @@ public void decode() throws Exception { |
119 | 122 | } |
120 | 123 | } |
121 | 124 |
|
122 | | - private void setResponseResult(ObjectInput in, boolean hasValue, boolean hasException, boolean hasAttachments) throws IOException { |
| 125 | + private void handleValue(ObjectInput in) throws IOException { |
123 | 126 | try { |
124 | | - if (hasValue) { |
125 | | - Type[] returnType = RpcUtils.getReturnTypes(invocation); |
126 | | - setValue(returnType == null || returnType.length == 0 ? in.readObject() : |
127 | | - (returnType.length == 1 ? in.readObject((Class<?>) returnType[0]) |
128 | | - : in.readObject((Class<?>) returnType[0], returnType[1]))); |
| 127 | + Type[] returnTypes = RpcUtils.getReturnTypes(invocation); |
| 128 | + Object value = null; |
| 129 | + if (ArrayUtils.isEmpty(returnTypes)) { |
| 130 | + value = in.readObject(); |
| 131 | + } else if (returnTypes.length == 1) { |
| 132 | + value = in.readObject((Class<?>) returnTypes[0]); |
| 133 | + } else { |
| 134 | + value = in.readObject((Class<?>) returnTypes[0], returnTypes[1]); |
129 | 135 | } |
130 | | - if (hasException) { |
131 | | - Object obj = in.readObject(); |
132 | | - if (obj instanceof Throwable == false) { |
133 | | - throw new IOException("Response data error, expect Throwable, but get " + obj); |
134 | | - } |
135 | | - setException((Throwable) obj); |
136 | | - } |
137 | | - if (hasAttachments) { |
138 | | - setAttachments((Map<String, String>) in.readObject(Map.class)); |
| 136 | + setValue(value); |
| 137 | + } catch (ClassNotFoundException e) { |
| 138 | + rethrow(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void handleException(ObjectInput in) throws IOException { |
| 143 | + try { |
| 144 | + Object obj = in.readObject(); |
| 145 | + if (!(obj instanceof Throwable)) { |
| 146 | + throw new IOException("Response data error, expect Throwable, but get " + obj); |
139 | 147 | } |
| 148 | + setException((Throwable) obj); |
140 | 149 | } catch (ClassNotFoundException e) { |
141 | | - throw new IOException(StringUtils.toString("Read response data failed.", e)); |
| 150 | + rethrow(e); |
142 | 151 | } |
| 152 | + } |
143 | 153 |
|
| 154 | + private void handleAttachment(ObjectInput in) throws IOException { |
| 155 | + try { |
| 156 | + setAttachments((Map<String, String>) in.readObject(Map.class)); |
| 157 | + } catch (ClassNotFoundException e) { |
| 158 | + rethrow(e); |
| 159 | + } |
144 | 160 | } |
145 | 161 |
|
| 162 | + private void rethrow(Exception e) throws IOException { |
| 163 | + throw new IOException(StringUtils.toString("Read response data failed.", e)); |
| 164 | + } |
146 | 165 | } |
0 commit comments