|
| 1 | +/* |
| 2 | +
|
| 3 | + * Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved. |
| 4 | +
|
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +/* |
| 18 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 19 | + * contributor license agreements. See the NOTICE file distributed with |
| 20 | + * this work for additional information regarding copyright ownership. |
| 21 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 22 | + * (the "License"); you may not use this file except in compliance with |
| 23 | + * the License. You may obtain a copy of the License at |
| 24 | + * |
| 25 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | + * |
| 27 | + * Unless required by applicable law or agreed to in writing, software |
| 28 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 29 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 30 | + * See the License for the specific language governing permissions and |
| 31 | + * limitations under the License. |
| 32 | + */ |
| 33 | +package com.huaweicloud.hessian; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.lang.reflect.Type; |
| 38 | + |
| 39 | +import org.apache.dubbo.common.serialize.Cleanable; |
| 40 | +import org.apache.dubbo.common.serialize.ObjectInput; |
| 41 | +import org.apache.dubbo.common.serialize.hessian2.Hessian2SerializerFactory; |
| 42 | + |
| 43 | +import com.alibaba.com.caucho.hessian.io.Hessian2Input; |
| 44 | + |
| 45 | +/** |
| 46 | + * Hessian2 object input implementation. This file is based on original Dubbo implementation. |
| 47 | + * This purpose is to support non-serializable model |
| 48 | + */ |
| 49 | +public class Hessian2ObjectInput implements ObjectInput, Cleanable { |
| 50 | + |
| 51 | + private static ThreadLocal<Hessian2Input> INPUT_TL = ThreadLocal.withInitial(() -> { |
| 52 | + Hessian2Input h2i = new Hessian2Input(null); |
| 53 | + Hessian2SerializerFactory factory = new Hessian2SerializerFactory(); |
| 54 | + factory.setAllowNonSerializable(true); |
| 55 | + h2i.setSerializerFactory(factory); |
| 56 | + h2i.setCloseStreamOnClose(true); |
| 57 | + return h2i; |
| 58 | + }); |
| 59 | + |
| 60 | + private final Hessian2Input mH2i; |
| 61 | + |
| 62 | + public Hessian2ObjectInput(InputStream is) { |
| 63 | + mH2i = INPUT_TL.get(); |
| 64 | + mH2i.init(is); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean readBool() throws IOException { |
| 69 | + return mH2i.readBoolean(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public byte readByte() throws IOException { |
| 74 | + return (byte) mH2i.readInt(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public short readShort() throws IOException { |
| 79 | + return (short) mH2i.readInt(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int readInt() throws IOException { |
| 84 | + return mH2i.readInt(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public long readLong() throws IOException { |
| 89 | + return mH2i.readLong(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public float readFloat() throws IOException { |
| 94 | + return (float) mH2i.readDouble(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public double readDouble() throws IOException { |
| 99 | + return mH2i.readDouble(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public byte[] readBytes() throws IOException { |
| 104 | + return mH2i.readBytes(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String readUTF() throws IOException { |
| 109 | + return mH2i.readString(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Object readObject() throws IOException { |
| 114 | + return mH2i.readObject(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + @SuppressWarnings("unchecked") |
| 119 | + public <T> T readObject(Class<T> cls) throws IOException, |
| 120 | + ClassNotFoundException { |
| 121 | + return (T) mH2i.readObject(cls); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException { |
| 126 | + return readObject(cls); |
| 127 | + } |
| 128 | + |
| 129 | + public InputStream readInputStream() throws IOException { |
| 130 | + return mH2i.readInputStream(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void cleanup() { |
| 135 | + if(mH2i != null) { |
| 136 | + mH2i.reset(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments