@Override public T deserialize(byte[] bytes) { if (bytes == null) { return null; } else { return (T) SerializationUtils.deserialize(bytes); } }
/** * <p> * Deserializes a single {@code Object} from an array of bytes. * </p> * * <p> * If the call site incorrectly types the return value, a {@link ClassCastException} is thrown from the call site. * Without Generics in this declaration, the call site must type cast and can cause the same ClassCastException. * Note that in both cases, the ClassCastException is in the call site, not in this method. * </p> * * @param <T> the object type to be deserialized * @param objectData * the serialized object, must not be null * @return the deserialized object * @throws IllegalArgumentException * if {@code objectData} is {@code null} * @throws SerializationException * (runtime) if the serialization fails */ public static <T> T deserialize(final byte[] objectData) { Validate.isTrue(objectData != null, "The byte[] must not be null"); return deserialize(new ByteArrayInputStream(objectData)); }
/** * Performs a serialization roundtrip. Serializes and deserializes the given object, great for testing objects that * implement {@link Serializable}. * * @param <T> * the type of the object involved * @param msg * the object to roundtrip * @return the serialized and deserialized object * @since 3.3 */ @SuppressWarnings("unchecked") // OK, because we serialized a type `T` public static <T extends Serializable> T roundtrip(final T msg) { return (T) deserialize(serialize(msg)); }
@Override public T read(ScanBuffer buffer) { byte[] data = serializer.readObjectNotNull(buffer,byte[].class); return (T) SerializationUtils.deserialize(data); }
public SegmentQueryResult get(String key) { byte[] value = memcachedCache.get(key); if (value == null) { return null; } return (SegmentQueryResult) (SerializationUtils.deserialize(value)); }
@Override public KeyHook lookupKeyHook(String keyS) { byte[] bytes = super.getBinary(keyS); if (bytes == null) { return null; } return (KeyHook) SerializationUtils.deserialize(bytes); } }
@Test(expected = SerializationException.class) public void testDeserializeBytesBadStream() throws Exception { SerializationUtils.deserialize(new byte[0]); }
@Test(expected = SerializationException.class) public void testDeserializeStreamBadStream() throws Exception { SerializationUtils.deserialize(new ByteArrayInputStream(new byte[0])); }
@Test(expected = IllegalArgumentException.class) public void testDeserializeBytesNull() throws Exception { SerializationUtils.deserialize((byte[]) null); }
@Test(expected = IllegalArgumentException.class) public void testDeserializeStreamNull() throws Exception { SerializationUtils.deserialize((InputStream) null); }
@Test public void testDeserializeStreamOfNull() throws Exception { final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); final Object test = SerializationUtils.deserialize(inTest); assertNull(test); }
@Test public void testDeserializeBytesOfNull() throws Exception { final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); final Object test = SerializationUtils.deserialize(streamReal.toByteArray()); assertNull(test); }
@Test(expected=ClassCastException.class) public void testDeserializeClassCastException() { final String value = "Hello"; final byte[] serialized = SerializationUtils.serialize(value); assertEquals(value, SerializationUtils.deserialize(serialized)); // Causes ClassCastException in call site, not in SerializationUtils.deserialize @SuppressWarnings("unused") // needed to cause Exception final Integer i = SerializationUtils.deserialize(serialized); }
@Test public void testDeserializeStreamClassNotFound() throws Exception { final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(new ClassNotFoundSerialization()); oos.flush(); oos.close(); final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); try { @SuppressWarnings("unused") final Object test = SerializationUtils.deserialize(inTest); } catch(final SerializationException se) { assertEquals("java.lang.ClassNotFoundException: " + CLASS_NOT_FOUND_MESSAGE, se.getMessage()); } }
@Test public void testDeserializeStream() throws Exception { final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(iMap); oos.flush(); oos.close(); final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray()); final Object test = SerializationUtils.deserialize(inTest); assertNotNull(test); assertTrue(test instanceof HashMap<?, ?>); assertNotSame(test, iMap); final HashMap<?, ?> testMap = (HashMap<?, ?>) test; assertEquals(iString, testMap.get("FOO")); assertNotSame(iString, testMap.get("FOO")); assertEquals(iInteger, testMap.get("BAR")); assertNotSame(iInteger, testMap.get("BAR")); assertEquals(iMap, testMap); }
@Test public void testDeserializeBytes() throws Exception { final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(iMap); oos.flush(); oos.close(); final Object test = SerializationUtils.deserialize(streamReal.toByteArray()); assertNotNull(test); assertTrue(test instanceof HashMap<?, ?>); assertNotSame(test, iMap); final HashMap<?, ?> testMap = (HashMap<?, ?>) test; assertEquals(iString, testMap.get("FOO")); assertNotSame(iString, testMap.get("FOO")); assertEquals(iInteger, testMap.get("BAR")); assertNotSame(iInteger, testMap.get("BAR")); assertEquals(iMap, testMap); }
@Test public void testLang303() throws ParseException { DateParser parser = getInstance(YMD_SLASH); final Calendar cal = Calendar.getInstance(); cal.set(2004, Calendar.DECEMBER, 31); final Date date = parser.parse("2004/11/31"); parser = SerializationUtils.deserialize(SerializationUtils.serialize((Serializable) parser)); assertEquals(date, parser.parse("2004/11/31")); }
@Test public void testLang303() { final Calendar cal = Calendar.getInstance(); cal.set(2004, Calendar.DECEMBER, 31); DatePrinter format = getInstance(YYYY_MM_DD); final String output = format.format(cal); format = SerializationUtils.deserialize(SerializationUtils.serialize((Serializable) format)); assertEquals(output, format.format(cal)); }
@Test public void testLang303() throws ParseException { DateParser parser = getInstance(YMD_SLASH); final Calendar cal = Calendar.getInstance(); cal.set(2004, Calendar.DECEMBER, 31); final Date date = parser.parse("2004/11/31"); parser = SerializationUtils.deserialize(SerializationUtils.serialize((Serializable) parser)); assertEquals(date, parser.parse("2004/11/31")); }
@Test public void testJavaSerialization() { exceptionContext.setContextValue("test Poorly written obj", "serializable replacement"); final T clone = SerializationUtils.deserialize(SerializationUtils.serialize(exceptionContext)); assertEquals(exceptionContext.getFormattedExceptionMessage(null), clone.getFormattedExceptionMessage(null)); } }