Introduce serializeToByteArray/deserializeFromByteArray on (De)Serializer
Closes gh-25117
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.core.serializer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -24,8 +25,10 @@ import java.io.InputStream;
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Mark Fisher
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0.5
|
||||
* @param <T> the object type
|
||||
* @see Serializer
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Deserializer<T> {
|
||||
@@ -41,4 +44,15 @@ public interface Deserializer<T> {
|
||||
*/
|
||||
T deserialize(InputStream inputStream) throws IOException;
|
||||
|
||||
/**
|
||||
* Read (assemble) an object of type T from the given byte array.
|
||||
* @param serialized the byte array
|
||||
* @return the deserialized object
|
||||
* @throws IOException in case of deserialization failure
|
||||
* @since 5.2.7
|
||||
*/
|
||||
default T deserializeFromByteArray(byte[] serialized) throws IOException {
|
||||
return deserialize(new ByteArrayInputStream(serialized));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.core.serializer;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
@@ -24,8 +25,10 @@ import java.io.OutputStream;
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Mark Fisher
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0.5
|
||||
* @param <T> the object type
|
||||
* @see Deserializer
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Serializer<T> {
|
||||
@@ -41,4 +44,17 @@ public interface Serializer<T> {
|
||||
*/
|
||||
void serialize(T object, OutputStream outputStream) throws IOException;
|
||||
|
||||
/**
|
||||
* Turn an object of type T into a serialized byte array.
|
||||
* @param object the object to serialize
|
||||
* @return the resulting byte array
|
||||
* @throws IOException in case of serialization failure
|
||||
* @since 5.2.7
|
||||
*/
|
||||
default byte[] serializeToByteArray(T object) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
|
||||
serialize(object, out);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.core.serializer.support;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.serializer.DefaultSerializer;
|
||||
import org.springframework.core.serializer.Serializer;
|
||||
@@ -58,10 +56,8 @@ public class SerializingConverter implements Converter<Object, byte[]> {
|
||||
*/
|
||||
@Override
|
||||
public byte[] convert(Object source) {
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
|
||||
try {
|
||||
this.serializer.serialize(source, byteStream);
|
||||
return byteStream.toByteArray();
|
||||
return this.serializer.serializeToByteArray(source);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new SerializationFailedException("Failed to serialize object using " +
|
||||
|
||||
Reference in New Issue
Block a user