diff --git a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java index 8a649122194a..1a17605578a1 100644 --- a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java +++ b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java @@ -16,9 +16,6 @@ package org.springframework.cache.concurrent; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -45,6 +42,7 @@ * @author Juergen Hoeller * @author Stephane Nicoll * @since 3.1 + * @see ConcurrentMapCacheManager */ public class ConcurrentMapCache extends AbstractValueAdaptingCache { @@ -190,7 +188,7 @@ protected Object toStoreValue(@Nullable Object userValue) { Object storeValue = super.toStoreValue(userValue); if (this.serialization != null) { try { - return serializeValue(this.serialization, storeValue); + return this.serialization.serializeToByteArray(storeValue); } catch (Throwable ex) { throw new IllegalArgumentException("Failed to serialize cache value '" + userValue + @@ -202,17 +200,11 @@ protected Object toStoreValue(@Nullable Object userValue) { } } - private static Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - serialization.serialize(storeValue, out); - return out.toByteArray(); - } - @Override protected Object fromStoreValue(@Nullable Object storeValue) { if (storeValue != null && this.serialization != null) { try { - return super.fromStoreValue(deserializeValue(this.serialization, storeValue)); + return super.fromStoreValue(this.serialization.deserializeFromByteArray((byte[]) storeValue)); } catch (Throwable ex) { throw new IllegalArgumentException("Failed to deserialize cache value '" + storeValue + "'", ex); @@ -221,12 +213,6 @@ protected Object fromStoreValue(@Nullable Object storeValue) { else { return super.fromStoreValue(storeValue); } - - } - - private static Object deserializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { - ByteArrayInputStream in = new ByteArrayInputStream((byte[]) storeValue); - return serialization.deserialize(in); } } diff --git a/spring-core/src/main/java/org/springframework/core/serializer/Deserializer.java b/spring-core/src/main/java/org/springframework/core/serializer/Deserializer.java index 25d3324a6792..afa173ab3846 100644 --- a/spring-core/src/main/java/org/springframework/core/serializer/Deserializer.java +++ b/spring-core/src/main/java/org/springframework/core/serializer/Deserializer.java @@ -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 @@ * * @author Gary Russell * @author Mark Fisher + * @author Juergen Hoeller * @since 3.0.5 * @param the object type + * @see Serializer */ @FunctionalInterface public interface Deserializer { @@ -41,4 +44,15 @@ public interface Deserializer { */ 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)); + } + } diff --git a/spring-core/src/main/java/org/springframework/core/serializer/Serializer.java b/spring-core/src/main/java/org/springframework/core/serializer/Serializer.java index ab88650bfcf8..1436f99bc293 100644 --- a/spring-core/src/main/java/org/springframework/core/serializer/Serializer.java +++ b/spring-core/src/main/java/org/springframework/core/serializer/Serializer.java @@ -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 @@ * * @author Gary Russell * @author Mark Fisher + * @author Juergen Hoeller * @since 3.0.5 * @param the object type + * @see Deserializer */ @FunctionalInterface public interface Serializer { @@ -41,4 +44,17 @@ public interface Serializer { */ 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(); + } + } diff --git a/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java b/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java index a49637ce7646..1f0fa8f17aed 100644 --- a/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java +++ b/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java @@ -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 SerializingConverter(Serializer serializer) { */ @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 " +