|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. 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 | +package org.apache.dubbo.common.serialize.avro; |
| 18 | + |
| 19 | +import org.apache.avro.io.BinaryDecoder; |
| 20 | +import org.apache.avro.io.DecoderFactory; |
| 21 | +import org.apache.avro.reflect.ReflectDatumReader; |
| 22 | +import org.apache.avro.util.Utf8; |
| 23 | +import org.apache.dubbo.common.serialize.ObjectInput; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.lang.reflect.Type; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public class AvroObjectInput implements ObjectInput { |
| 32 | + private static DecoderFactory decoderFactory = DecoderFactory.get(); |
| 33 | + private BinaryDecoder decoder; |
| 34 | + |
| 35 | + public AvroObjectInput(InputStream in) { |
| 36 | + decoder = decoderFactory.binaryDecoder(in, null); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean readBool() throws IOException { |
| 41 | + return decoder.readBoolean(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public byte readByte() throws IOException { |
| 46 | + byte[] bytes = new byte[1]; |
| 47 | + decoder.readFixed(bytes); |
| 48 | + return bytes[0]; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public short readShort() throws IOException { |
| 53 | + return (short) decoder.readInt(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public int readInt() throws IOException { |
| 58 | + return decoder.readInt(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public long readLong() throws IOException { |
| 63 | + return decoder.readLong(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public float readFloat() throws IOException { |
| 68 | + return decoder.readFloat(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public double readDouble() throws IOException { |
| 73 | + return decoder.readDouble(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String readUTF() throws IOException { |
| 78 | + Utf8 result = new Utf8(); |
| 79 | + result = decoder.readString(result); |
| 80 | + return result.toString(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public byte[] readBytes() throws IOException { |
| 85 | + String resultStr = decoder.readString(); |
| 86 | + return resultStr.getBytes("utf8"); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * will lost all attribute |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public Object readObject() throws IOException, ClassNotFoundException { |
| 94 | + ReflectDatumReader<Object> reader = new ReflectDatumReader<>(Object.class); |
| 95 | + return reader.read(null, decoder); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + @SuppressWarnings(value = {"unchecked"}) |
| 100 | + public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException { |
| 101 | + //Map interface class change to HashMap implement |
| 102 | + if (cls == Map.class) { |
| 103 | + cls = (Class<T>) HashMap.class; |
| 104 | + } |
| 105 | + |
| 106 | + ReflectDatumReader<T> reader = new ReflectDatumReader<>(cls); |
| 107 | + return reader.read(null, decoder); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException { |
| 112 | + ReflectDatumReader<T> reader = new ReflectDatumReader<>(cls); |
| 113 | + return reader.read(null, decoder); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments