/* * Copyright 1999-2012 Alibaba Group. * * Licensed 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.dubbo.common.serialize.support.nativejava; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.lang.reflect.Type; import com.alibaba.dubbo.common.serialize.ObjectInput; import com.alibaba.dubbo.common.utils.Assert; /** * @author <a href="mailto:gang.lvg@alibaba-inc.com">kimi</a> */ public class NativeJavaObjectInput implements ObjectInput { private final ObjectInputStream inputStream; public NativeJavaObjectInput(InputStream is) throws IOException { this(new ObjectInputStream(is)); } protected NativeJavaObjectInput(ObjectInputStream is) { Assert.notNull(is, "input == null"); inputStream = is; } protected ObjectInputStream getObjectInputStream() { return inputStream; } public Object readObject() throws IOException, ClassNotFoundException { return inputStream.readObject(); } @SuppressWarnings("unchecked") public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException { return (T) readObject(); } @SuppressWarnings("unchecked") public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException { return (T) readObject(); } public boolean readBool() throws IOException { return inputStream.readBoolean(); } public byte readByte() throws IOException { return inputStream.readByte(); } public short readShort() throws IOException { return inputStream.readShort(); } public int readInt() throws IOException { return inputStream.readInt(); } public long readLong() throws IOException { return inputStream.readLong(); } public float readFloat() throws IOException { return inputStream.readFloat(); } public double readDouble() throws IOException { return inputStream.readDouble(); } public String readUTF() throws IOException { return inputStream.readUTF(); } public byte[] readBytes() throws IOException { int len = inputStream.readInt(); if (len < 0) { return null; } else if (len == 0) { return new byte[]{}; } else { byte[] result = new byte[len]; inputStream.readFully(result); return result; } } }