Java Code Examples for org.springframework.core.ConfigurableObjectInputStream

Following code examples demonstrate how to use org.springframework.core.ConfigurableObjectInputStreamfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.core.ConfigurableObjectInputStreamand various code implementation of this class.

	public static <T> T deserialize(byte[] byteArray) {
		ObjectInputStream oip = null;
		try {
			oip = new ConfigurableObjectInputStream(new ByteArrayInputStream(byteArray),
					Thread.currentThread().getContextClassLoader());
			@SuppressWarnings("unchecked")
			T result = (T) oip.readObject();
			return result;
		}
		catch (IOException e) {
			throw new IllegalArgumentException(e);
		}
		catch (ClassNotFoundException e) {
			throw new IllegalArgumentException(e);
		}
		finally {
			if (oip != null) {
				try {
					oip.close();
				}
				catch (IOException e) {
				}
			}
		}
	} 


	public static Object deserialize(byte[] source) {
		Object retVal = null;
		if (source != null) {
			Object ObjIn = null;

			try {
				ConfigurableObjectInputStream e = new ConfigurableObjectInputStream(new ByteArrayInputStream(source),
						Thread.currentThread().getContextClassLoader());
				retVal = e.readObject();
			} catch (Exception arg6) {
				logger.error("deserialized error  !", arg6);
			} finally {
				IOUtils.close((Closeable) ObjIn);
			}
		}

		return retVal;
	} 

	public Object deserialize(InputStream inputStream) throws IOException {
		ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader);
		try {
			return objectInputStream.readObject();
		}
		catch (ClassNotFoundException ex) {
			throw new NestedIOException("Failed to deserialize object type", ex);
		}
	} 

    public static Object deserialize(byte[] data) {
        Object t = null;
        try {
            ByteArrayInputStream fileIn = new ByteArrayInputStream(data);
            ObjectInputStream in = new ConfigurableObjectInputStream(fileIn,
                    Thread.currentThread().getContextClassLoader());
            t = in.readObject();
            in.close();
            fileIn.close();
        } catch (Exception e) {
            String msg = "Deserialization of marshalled XML failed!";
            log.error(msg, e);
            throw new RuntimeException(msg, e);
        }
        return t;
    } 

Advertisement
Javadoc
Special ObjectInputStream subclass that resolves class names against a specific ClassLoader. Serves

as base class for org.springframework.remoting.rmi.CodebaseAwareObjectInputStream. @author Juergen Hoeller @since 2.5.5

Read More
Advertisement