Introduce serializeToByteArray/deserializeFromByteArray on (De)Serializer

Closes gh-25117
This commit is contained in:
Juergen Hoeller
2020-05-22 16:22:59 +02:00
parent 807ded24c4
commit e5c079edfc
4 changed files with 37 additions and 25 deletions

View File

@@ -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 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.1
* @see ConcurrentMapCacheManager
*/
public class ConcurrentMapCache extends AbstractValueAdaptingCache {
@@ -190,7 +188,7 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
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 @@ 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
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 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
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);
}
}