Serializer { public static byte[] serialize(final Object o, final boolean zip) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream os = baos; if (zip) { os = new GZIPOutputStream(os); } final ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(o); oos.flush(); oos.close(); return baos.toByteArray(); }  static byte[] serialize(final Object o, final boolean zip); static Object deserialize(final Object data, final boolean zipped);  }
@Test public final void testSerialize() throws IOException, ClassNotFoundException { byte[] test = new byte[2048]; byte[] stringBytes = TEST_TEXT.getBytes(); System.arraycopy(stringBytes, 0, test, 0, stringBytes.length); byte[] ser = Serializer.serialize(test, false); byte[] after = (byte[]) Serializer.deserialize(ser, false); Assert.assertTrue(ser.length > 2048); Assert.assertTrue(after.length == 2048); Assert.assertTrue(new String(after).startsWith(TEST_TEXT)); ser = Serializer.serialize(test, true); after = (byte[]) Serializer.deserialize(ser, true); Assert.assertTrue(ser.length < 2048); Assert.assertTrue(after.length == 2048); Assert.assertTrue(new String(after).startsWith(TEST_TEXT)); }