@Override public byte[] serialize(T object) { return SerializationUtils.serialize(object); }
public void setCubeSegmentStatistics(CubeSegmentStatistics cubeSegmentStatistics) { this.cubeSegmentStatisticsBytes = (cubeSegmentStatistics == null ? null : SerializationUtils .serialize(cubeSegmentStatistics)); }
/** * <p>Serializes an {@code Object} to a byte array for * storage/serialization.</p> * * @param obj the object to serialize to bytes * @return a byte[] with the converted Serializable * @throws SerializationException (runtime) if the serialization fails */ public static byte[] serialize(final Serializable obj) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(512); serialize(obj, baos); return baos.toByteArray(); }
public void setCubeSegmentStatisticsList( List<QueryContext.CubeSegmentStatisticsResult> cubeSegmentStatisticsList) { try { this.queryStatistics = cubeSegmentStatisticsList == null ? null : SerializationUtils.serialize((Serializable) cubeSegmentStatisticsList); } catch (Exception e) { // serialize exception should not block query logger.warn("Error while serialize queryStatistics due to " + e); this.queryStatistics = null; } } }
/** * 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)); }
static <T extends Serializable> T cloneSerializable(T source) { byte[] bytes = SerializationUtils.serialize(source); try { ObjectInputStream input = new ModProxyObjectInputStream(new ByteArrayInputStream(bytes)); return (T) input.readObject(); } catch (Exception e) { throw new RuntimeException("Error cloning serializable object", e); } }
@Test(expected = SerializationException.class) public void testSerializeStreamUnserializable() throws Exception { final ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); iMap.put(new Object(), new Object()); SerializationUtils.serialize(iMap, streamTest); }
@Test(expected = SerializationException.class) public void testSerializeBytesUnserializable() throws Exception { iMap.put(new Object(), new Object()); SerializationUtils.serialize(iMap); }
@Test(expected = IllegalArgumentException.class) public void testSerializeStreamObjNull() throws Exception { SerializationUtils.serialize(iMap, null); }
@Test(expected = IllegalArgumentException.class) public void testSerializeStreamNullNull() throws Exception { SerializationUtils.serialize(null, null); }
@Test public void testSerializeBytesNull() throws Exception { final byte[] testBytes = SerializationUtils.serialize(null); final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); final byte[] realBytes = streamReal.toByteArray(); assertEquals(testBytes.length, realBytes.length); assertArrayEquals(realBytes, testBytes); }
@Test public void testSerializeBytes() throws Exception { final byte[] testBytes = SerializationUtils.serialize(iMap); final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(iMap); oos.flush(); oos.close(); final byte[] realBytes = streamReal.toByteArray(); assertEquals(testBytes.length, realBytes.length); assertArrayEquals(realBytes, testBytes); }
@Test public void testSerializeStream() throws Exception { final ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); SerializationUtils.serialize(iMap, streamTest); final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(iMap); oos.flush(); oos.close(); final byte[] testBytes = streamTest.toByteArray(); final byte[] realBytes = streamReal.toByteArray(); assertEquals(testBytes.length, realBytes.length); assertArrayEquals(realBytes, testBytes); }
@Test public void testSerializeStreamNullObj() throws Exception { final ByteArrayOutputStream streamTest = new ByteArrayOutputStream(); SerializationUtils.serialize(null, streamTest); final ByteArrayOutputStream streamReal = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(streamReal); oos.writeObject(null); oos.flush(); oos.close(); final byte[] testBytes = streamTest.toByteArray(); final byte[] realBytes = streamReal.toByteArray(); assertEquals(testBytes.length, realBytes.length); assertArrayEquals(realBytes, testBytes); }
@Test public void testSerializeIOException() throws Exception { // forces an IOException when the ObjectOutputStream is created, to test not closing the stream // in the finally block final OutputStream streamTest = new OutputStream() { @Override public void write(final int arg0) throws IOException { throw new IOException(SERIALIZE_IO_EXCEPTION_MESSAGE); } }; try { SerializationUtils.serialize(iMap, streamTest); } catch(final SerializationException e) { assertEquals("java.io.IOException: " + SERIALIZE_IO_EXCEPTION_MESSAGE, e.getMessage()); } }
@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(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 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)); } }