Browse Source

Introduce serializeToByteArray/deserializeFromByteArray on (De)Serializer

Closes gh-25117
pull/25758/head
Juergen Hoeller 6 years ago
parent
commit
e5c079edfc
  1. 20
      spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java
  2. 16
      spring-core/src/main/java/org/springframework/core/serializer/Deserializer.java
  3. 18
      spring-core/src/main/java/org/springframework/core/serializer/Serializer.java
  4. 8
      spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java

20
spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java vendored

@ -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);
} }
} }

16
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"); * 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));
}
} }

18
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"); * 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();
}
} }

8
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"); * 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 " +

Loading…
Cancel
Save