/** * Creates a new {@link JdkSerializationRedisSerializer} using the default class loader. */ public JdkSerializationRedisSerializer() { this(new SerializingConverter(), new DeserializingConverter()); }
private Object deserializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream((byte[]) storeValue); try { return serialization.deserialize(in); } finally { in.close(); } }
private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serialization.serialize(storeValue, out); return out.toByteArray(); } finally { out.close(); } }
@Test public void serializeAndDeserializeString() { SerializingConverter toBytes = new SerializingConverter(); byte[] bytes = toBytes.convert("Testing"); DeserializingConverter fromBytes = new DeserializingConverter(); assertEquals("Testing", fromBytes.convert(bytes)); }
@Test public void nonSerializableObject() { SerializingConverter toBytes = new SerializingConverter(); try { toBytes.convert(new Object()); fail("Expected IllegalArgumentException"); } catch (SerializationFailedException e) { assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof IllegalArgumentException); } }
/** * A converter for serializing messages to byte arrays for storage. * * @param serializer the serializer to set */ @SuppressWarnings("unchecked") public void setSerializer(Serializer<? super Message<?>> serializer) { this.serializer = new SerializingConverter((Serializer<Object>) serializer); }
@Override public Object convert(byte[] source) { ByteArrayInputStream byteStream = new ByteArrayInputStream(source); try { return this.deserializer.deserialize(byteStream); } catch (Throwable ex) { throw new SerializationFailedException("Failed to deserialize payload. " + "Is the byte array a result of corresponding serialization for " + this.deserializer.getClass().getSimpleName() + "?", ex); } }
@Test(expected = SerializationFailedException.class) public void deserializationFailure() { DeserializingConverter fromBytes = new DeserializingConverter(); fromBytes.convert("Junk".getBytes()); }
@Override public void setBeanClassLoader(ClassLoader classLoader) { this.serialization = new SerializationDelegate(classLoader); // Need to recreate all Cache instances with new ClassLoader in store-by-value mode... if (isStoreByValue()) { recreateCaches(); } }
/** * Creates a new {@link JdkSerializationRedisSerializer} using a {@link ClassLoader}. * * @param classLoader the {@link ClassLoader} to use for deserialization. Can be {@literal null}. * @since 1.7 */ public JdkSerializationRedisSerializer(@Nullable ClassLoader classLoader) { this(new SerializingConverter(), new DeserializingConverter(classLoader)); }
@Test public void nonSerializableField() { SerializingConverter toBytes = new SerializingConverter(); try { toBytes.convert(new UnSerializable()); fail("Expected SerializationFailureException"); } catch (SerializationFailedException e) { assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof NotSerializableException); } }
/** * Serializes the source object and returns the byte array result. */ @Override public byte[] convert(Object source) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024); try { this.serializer.serialize(source, byteStream); return byteStream.toByteArray(); } catch (Throwable ex) { throw new SerializationFailedException("Failed to serialize object using " + this.serializer.getClass().getSimpleName(), ex); } }
private ConcurrentMapCache createCacheWithStoreByValue() { return new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true, new SerializationDelegate(ConcurrentMapCacheTests.class.getClassLoader())); }
private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serialization.serialize(storeValue, out); return out.toByteArray(); } finally { out.close(); } }
private Object deserializeValue(SerializationDelegate serialization, Object storeValue) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream((byte[]) storeValue); try { return serialization.deserialize(in); } finally { in.close(); } }
private static GenericConversionService createDefaultConversionService() { GenericConversionService converter = new GenericConversionService(); converter.addConverter(Object.class, byte[].class, new SerializingConverter()); converter.addConverter(byte[].class, Object.class, new DeserializingConverter()); return converter; }
@Override public Object convert(byte[] source) { ByteArrayInputStream byteStream = new ByteArrayInputStream(source); try { return this.deserializer.deserialize(byteStream); } catch (Throwable ex) { throw new SerializationFailedException("Failed to deserialize payload. " + "Is the byte array a result of corresponding serialization for " + this.deserializer.getClass().getSimpleName() + "?", ex); } }
@Override public void setBeanClassLoader(ClassLoader classLoader) { this.serialization = new SerializationDelegate(classLoader); // Need to recreate all Cache instances with new ClassLoader in store-by-value mode... if (isStoreByValue()) { recreateCaches(); } }
private GenericConversionService createConversionServiceWithBeanClassLoader() { GenericConversionService conversionService = new GenericConversionService(); conversionService.addConverter(Object.class, byte[].class, new SerializingConverter()); conversionService.addConverter(byte[].class, Object.class, new DeserializingConverter(this.classLoader)); return conversionService; }
/** * Serializes the source object and returns the byte array result. */ @Override public byte[] convert(Object source) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024); try { this.serializer.serialize(source, byteStream); return byteStream.toByteArray(); } catch (Throwable ex) { throw new SerializationFailedException("Failed to serialize object using " + this.serializer.getClass().getSimpleName(), ex); } }