Merge branch '5.2.x'

# Conflicts:
#	spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java
#	spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java
This commit is contained in:
Juergen Hoeller 2020-05-22 17:16:09 +02:00
commit 399453d9f1
18 changed files with 80 additions and 74 deletions

View File

@ -27,7 +27,7 @@ configure(allprojects) { project ->
mavenBom "io.netty:netty-bom:4.1.50.Final" mavenBom "io.netty:netty-bom:4.1.50.Final"
mavenBom "io.projectreactor:reactor-bom:Dysprosium-SR7" mavenBom "io.projectreactor:reactor-bom:Dysprosium-SR7"
mavenBom "io.rsocket:rsocket-bom:1.0.0" mavenBom "io.rsocket:rsocket-bom:1.0.0"
mavenBom "org.eclipse.jetty:jetty-bom:9.4.28.v20200408" mavenBom "org.eclipse.jetty:jetty-bom:9.4.29.v20200521"
mavenBom "org.jetbrains.kotlin:kotlin-bom:1.3.72" mavenBom "org.jetbrains.kotlin:kotlin-bom:1.3.72"
mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.3.5" mavenBom "org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.3.5"
mavenBom "org.junit:junit-bom:5.6.2" mavenBom "org.junit:junit-bom:5.6.2"
@ -114,9 +114,9 @@ configure(allprojects) { project ->
dependency "net.sf.ehcache:ehcache:2.10.6" dependency "net.sf.ehcache:ehcache:2.10.6"
dependency "org.ehcache:jcache:1.0.1" dependency "org.ehcache:jcache:1.0.1"
dependency "org.ehcache:ehcache:3.4.0" dependency "org.ehcache:ehcache:3.4.0"
dependency "org.hibernate:hibernate-core:5.4.15.Final" dependency "org.hibernate:hibernate-core:5.4.16.Final"
dependency "org.hibernate:hibernate-validator:6.1.5.Final" dependency "org.hibernate:hibernate-validator:6.1.5.Final"
dependency "org.webjars:webjars-locator-core:0.44" dependency "org.webjars:webjars-locator-core:0.45"
dependency "org.webjars:underscorejs:1.8.3" dependency "org.webjars:underscorejs:1.8.3"
dependencySet(group: 'org.apache.tomcat', version: '9.0.35') { dependencySet(group: 'org.apache.tomcat', version: '9.0.35') {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,6 +35,7 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 4.3 * @since 4.3
* @see CaffeineCacheManager
*/ */
public class CaffeineCache extends AbstractValueAdaptingCache { public class CaffeineCache extends AbstractValueAdaptingCache {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.1 * @since 3.1
* @see EhCacheCacheManager
*/ */
public class EhCacheCache implements Cache { public class EhCacheCache implements Cache {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.1 * @since 3.1
* @see EhCacheCache
*/ */
public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager { public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,6 +36,7 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.2 * @since 3.2
* @see JCacheCacheManager
*/ */
public class JCacheCache extends AbstractValueAdaptingCache { public class JCacheCache extends AbstractValueAdaptingCache {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,6 +36,7 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.2 * @since 3.2
* @see JCacheCache
*/ */
public class JCacheCacheManager extends AbstractTransactionSupportingCacheManager { public class JCacheCacheManager extends AbstractTransactionSupportingCacheManager {

View File

@ -16,9 +16,6 @@
package org.springframework.cache.concurrent; 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.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
@ -45,6 +42,7 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.1 * @since 3.1
* @see ConcurrentMapCacheManager
*/ */
public class ConcurrentMapCache extends AbstractValueAdaptingCache { public class ConcurrentMapCache extends AbstractValueAdaptingCache {
@ -190,7 +188,7 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
Object storeValue = super.toStoreValue(userValue); Object storeValue = super.toStoreValue(userValue);
if (this.serialization != null) { if (this.serialization != null) {
try { try {
return serializeValue(this.serialization, storeValue); return this.serialization.serializeToByteArray(storeValue);
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new IllegalArgumentException("Failed to serialize cache value '" + userValue + throw new IllegalArgumentException("Failed to serialize cache value '" + userValue +
@ -202,17 +200,11 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
} }
} }
private static Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
serialization.serialize(storeValue, out);
return out.toByteArray();
}
@Override @Override
protected Object fromStoreValue(@Nullable Object storeValue) { protected Object fromStoreValue(@Nullable Object storeValue) {
if (storeValue != null && this.serialization != null) { if (storeValue != null && this.serialization != null) {
try { try {
return super.fromStoreValue(deserializeValue(this.serialization, storeValue)); return super.fromStoreValue(this.serialization.deserializeFromByteArray((byte[]) storeValue));
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new IllegalArgumentException("Failed to deserialize cache value '" + storeValue + "'", ex); throw new IllegalArgumentException("Failed to deserialize cache value '" + storeValue + "'", ex);
@ -221,12 +213,6 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
else { else {
return super.fromStoreValue(storeValue); return super.fromStoreValue(storeValue);
} }
}
private static Object deserializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream((byte[]) storeValue);
return serialization.deserialize(in);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -190,9 +190,7 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA
*/ */
protected Cache createConcurrentMapCache(String name) { protected Cache createConcurrentMapCache(String name) {
SerializationDelegate actualSerialization = (isStoreByValue() ? this.serialization : null); SerializationDelegate actualSerialization = (isStoreByValue() ? this.serialization : null);
return new ConcurrentMapCache(name, new ConcurrentHashMap<>(256), return new ConcurrentMapCache(name, new ConcurrentHashMap<>(256), isAllowNullValues(), actualSerialization);
isAllowNullValues(), actualSerialization);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,6 +52,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier; import org.springframework.util.function.SingletonSupplier;
import org.springframework.util.function.SupplierUtils; import org.springframework.util.function.SupplierUtils;
@ -382,9 +383,9 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker)))); return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker))));
} }
catch (Cache.ValueRetrievalException ex) { catch (Cache.ValueRetrievalException ex) {
// The invoker wraps any Throwable in a ThrowableWrapper instance so we // Directly propagate ThrowableWrapper from the invoker,
// can just make sure that one bubbles up the stack. // or potentially also an IllegalArgumentException etc.
throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause(); ReflectionUtils.rethrowRuntimeException(ex.getCause());
} }
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import org.springframework.util.Assert;
* @author Costin Leau * @author Costin Leau
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 4.3.4 * @since 4.3.4
* @see NoOpCacheManager
*/ */
public class NoOpCache implements Cache { public class NoOpCache implements Cache {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,7 +37,7 @@ import org.springframework.lang.Nullable;
* @author Costin Leau * @author Costin Leau
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.1 * @since 3.1
* @see CompositeCacheManager * @see NoOpCache
*/ */
public class NoOpCacheManager implements CacheManager { public class NoOpCacheManager implements CacheManager {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -393,7 +393,6 @@ public class CacheReproTests {
public TestBean insertItem(TestBean item) { public TestBean insertItem(TestBean item) {
return item; return item;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,18 +33,15 @@ public class ConcurrentMapCacheManagerTests {
public void testDynamicMode() { public void testDynamicMode() {
CacheManager cm = new ConcurrentMapCacheManager(); CacheManager cm = new ConcurrentMapCacheManager();
Cache cache1 = cm.getCache("c1"); Cache cache1 = cm.getCache("c1");
boolean condition2 = cache1 instanceof ConcurrentMapCache; assertThat(cache1 instanceof ConcurrentMapCache).isTrue();
assertThat(condition2).isTrue();
Cache cache1again = cm.getCache("c1"); Cache cache1again = cm.getCache("c1");
assertThat(cache1).isSameAs(cache1again); assertThat(cache1).isSameAs(cache1again);
Cache cache2 = cm.getCache("c2"); Cache cache2 = cm.getCache("c2");
boolean condition1 = cache2 instanceof ConcurrentMapCache; assertThat(cache2 instanceof ConcurrentMapCache).isTrue();
assertThat(condition1).isTrue();
Cache cache2again = cm.getCache("c2"); Cache cache2again = cm.getCache("c2");
assertThat(cache2).isSameAs(cache2again); assertThat(cache2).isSameAs(cache2again);
Cache cache3 = cm.getCache("c3"); Cache cache3 = cm.getCache("c3");
boolean condition = cache3 instanceof ConcurrentMapCache; assertThat(cache3 instanceof ConcurrentMapCache).isTrue();
assertThat(condition).isTrue();
Cache cache3again = cm.getCache("c3"); Cache cache3again = cm.getCache("c3");
assertThat(cache3).isSameAs(cache3again); assertThat(cache3).isSameAs(cache3again);
@ -74,13 +71,11 @@ public class ConcurrentMapCacheManagerTests {
public void testStaticMode() { public void testStaticMode() {
ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2"); ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2");
Cache cache1 = cm.getCache("c1"); Cache cache1 = cm.getCache("c1");
boolean condition3 = cache1 instanceof ConcurrentMapCache; assertThat(cache1 instanceof ConcurrentMapCache).isTrue();
assertThat(condition3).isTrue();
Cache cache1again = cm.getCache("c1"); Cache cache1again = cm.getCache("c1");
assertThat(cache1).isSameAs(cache1again); assertThat(cache1).isSameAs(cache1again);
Cache cache2 = cm.getCache("c2"); Cache cache2 = cm.getCache("c2");
boolean condition2 = cache2 instanceof ConcurrentMapCache; assertThat(cache2 instanceof ConcurrentMapCache).isTrue();
assertThat(condition2).isTrue();
Cache cache2again = cm.getCache("c2"); Cache cache2again = cm.getCache("c2");
assertThat(cache2).isSameAs(cache2again); assertThat(cache2).isSameAs(cache2again);
Cache cache3 = cm.getCache("c3"); Cache cache3 = cm.getCache("c3");
@ -97,12 +92,10 @@ public class ConcurrentMapCacheManagerTests {
cm.setAllowNullValues(false); cm.setAllowNullValues(false);
Cache cache1x = cm.getCache("c1"); Cache cache1x = cm.getCache("c1");
boolean condition1 = cache1x instanceof ConcurrentMapCache; assertThat(cache1x instanceof ConcurrentMapCache).isTrue();
assertThat(condition1).isTrue();
assertThat(cache1x != cache1).isTrue(); assertThat(cache1x != cache1).isTrue();
Cache cache2x = cm.getCache("c2"); Cache cache2x = cm.getCache("c2");
boolean condition = cache2x instanceof ConcurrentMapCache; assertThat(cache2x instanceof ConcurrentMapCache).isTrue();
assertThat(condition).isTrue();
assertThat(cache2x != cache2).isTrue(); assertThat(cache2x != cache2).isTrue();
Cache cache3x = cm.getCache("c3"); Cache cache3x = cm.getCache("c3");
assertThat(cache3x).isNull(); assertThat(cache3x).isNull();
@ -126,16 +119,14 @@ public class ConcurrentMapCacheManagerTests {
ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2"); ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2");
assertThat(cm.isStoreByValue()).isFalse(); assertThat(cm.isStoreByValue()).isFalse();
Cache cache1 = cm.getCache("c1"); Cache cache1 = cm.getCache("c1");
boolean condition1 = cache1 instanceof ConcurrentMapCache; assertThat(cache1 instanceof ConcurrentMapCache).isTrue();
assertThat(condition1).isTrue(); assertThat(((ConcurrentMapCache) cache1).isStoreByValue()).isFalse();
assertThat(((ConcurrentMapCache)cache1).isStoreByValue()).isFalse();
cache1.put("key", "value"); cache1.put("key", "value");
cm.setStoreByValue(true); cm.setStoreByValue(true);
assertThat(cm.isStoreByValue()).isTrue(); assertThat(cm.isStoreByValue()).isTrue();
Cache cache1x = cm.getCache("c1"); Cache cache1x = cm.getCache("c1");
boolean condition = cache1x instanceof ConcurrentMapCache; assertThat(cache1x instanceof ConcurrentMapCache).isTrue();
assertThat(condition).isTrue();
assertThat(cache1x != cache1).isTrue(); assertThat(cache1x != cache1).isTrue();
assertThat(cache1x.get("key")).isNull(); assertThat(cache1x.get("key")).isNull();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,8 +36,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class ConcurrentMapCacheTests public class ConcurrentMapCacheTests extends AbstractValueAdaptingCacheTests<ConcurrentMapCache> {
extends AbstractValueAdaptingCacheTests<ConcurrentMapCache> {
protected ConcurrentMap<Object, Object> nativeCache; protected ConcurrentMap<Object, Object> nativeCache;
@ -49,12 +48,11 @@ public class ConcurrentMapCacheTests
@BeforeEach @BeforeEach
public void setUp() throws Exception { public void setup() {
this.nativeCache = new ConcurrentHashMap<>(); this.nativeCache = new ConcurrentHashMap<>();
this.cache = new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true); this.cache = new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true);
this.nativeCacheNoNull = new ConcurrentHashMap<>(); this.nativeCacheNoNull = new ConcurrentHashMap<>();
this.cacheNoNull = new ConcurrentMapCache(CACHE_NAME_NO_NULL, this.cacheNoNull = new ConcurrentMapCache(CACHE_NAME_NO_NULL, this.nativeCacheNoNull, false);
this.nativeCacheNoNull, false);
this.cache.clear(); this.cache.clear();
} }
@ -73,6 +71,7 @@ public class ConcurrentMapCacheTests
return this.nativeCache; return this.nativeCache;
} }
@Test @Test
public void testIsStoreByReferenceByDefault() { public void testIsStoreByReferenceByDefault() {
assertThat(this.cache.isStoreByValue()).isFalse(); assertThat(this.cache.isStoreByValue()).isFalse();

View File

@ -105,7 +105,7 @@ public final class ReactiveTypeDescriptor {
* Whether the underlying operation is deferred and needs to be started * Whether the underlying operation is deferred and needs to be started
* explicitly, e.g. via subscribing (or similar), or whether it is triggered * explicitly, e.g. via subscribing (or similar), or whether it is triggered
* without the consumer having any control. * without the consumer having any control.
* @since 5.1.16 * @since 5.2.7
*/ */
public boolean isDeferred() { public boolean isDeferred() {
return this.deferred; return this.deferred;
@ -169,7 +169,7 @@ public final class ReactiveTypeDescriptor {
* non-deferred, async type such as {@link java.util.concurrent.CompletableFuture}. * non-deferred, async type such as {@link java.util.concurrent.CompletableFuture}.
* @param type the reactive type * @param type the reactive type
* @param emptySupplier a supplier of an empty-value instance of the reactive type * @param emptySupplier a supplier of an empty-value instance of the reactive type
* @since 5.1.16 * @since 5.2.7
*/ */
public static ReactiveTypeDescriptor nonDeferredAsyncValue(Class<?> type, Supplier<?> emptySupplier) { public static ReactiveTypeDescriptor nonDeferredAsyncValue(Class<?> type, Supplier<?> emptySupplier) {
return new ReactiveTypeDescriptor(type, false, false, emptySupplier, false); return new ReactiveTypeDescriptor(type, false, false, emptySupplier, false);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.core.serializer; package org.springframework.core.serializer;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -24,8 +25,10 @@ import java.io.InputStream;
* *
* @author Gary Russell * @author Gary Russell
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller
* @since 3.0.5 * @since 3.0.5
* @param <T> the object type * @param <T> the object type
* @see Serializer
*/ */
@FunctionalInterface @FunctionalInterface
public interface Deserializer<T> { public interface Deserializer<T> {
@ -41,4 +44,15 @@ public interface Deserializer<T> {
*/ */
T deserialize(InputStream inputStream) throws IOException; 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));
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.core.serializer; package org.springframework.core.serializer;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -24,8 +25,10 @@ import java.io.OutputStream;
* *
* @author Gary Russell * @author Gary Russell
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller
* @since 3.0.5 * @since 3.0.5
* @param <T> the object type * @param <T> the object type
* @see Deserializer
*/ */
@FunctionalInterface @FunctionalInterface
public interface Serializer<T> { public interface Serializer<T> {
@ -41,4 +44,17 @@ public interface Serializer<T> {
*/ */
void serialize(T object, OutputStream outputStream) throws IOException; 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();
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package org.springframework.core.serializer.support; package org.springframework.core.serializer.support;
import java.io.ByteArrayOutputStream;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.DefaultSerializer; import org.springframework.core.serializer.DefaultSerializer;
import org.springframework.core.serializer.Serializer; import org.springframework.core.serializer.Serializer;
@ -58,10 +56,8 @@ public class SerializingConverter implements Converter<Object, byte[]> {
*/ */
@Override @Override
public byte[] convert(Object source) { public byte[] convert(Object source) {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
try { try {
this.serializer.serialize(source, byteStream); return this.serializer.serializeToByteArray(source);
return byteStream.toByteArray();
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new SerializationFailedException("Failed to serialize object using " + throw new SerializationFailedException("Failed to serialize object using " +