Skip to content

Commit

Permalink
Introduce serializeToByteArray/deserializeFromByteArray on (De)Serial…
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller authored and xcl(徐程林) committed Aug 16, 2020
1 parent 313e861 commit a743149
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
Expand Up @@ -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;
Expand All @@ -45,6 +42,7 @@
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.1
* @see ConcurrentMapCacheManager
*/
public class ConcurrentMapCache extends AbstractValueAdaptingCache {

Expand Down Expand Up @@ -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 +
Expand All @@ -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);
Expand All @@ -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);
}

}
@@ -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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.core.serializer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -24,8 +25,10 @@
*
* @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> {
Expand All @@ -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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.core.serializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

Expand All @@ -24,8 +25,10 @@
*
* @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> {
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -58,10 +56,8 @@ public SerializingConverter(Serializer<Object> 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 " +
Expand Down

0 comments on commit a743149

Please sign in to comment.