/** * Serializes an object using default serialization. Compresses the byte array. * <p> * @param obj object * @return byte[] * @throws IOException on i/o problem */ @Override public <T> byte[] serialize( T obj ) throws IOException { byte[] uncompressed = serializeObject( obj ); byte[] compressed = CompressionUtil.compressByteArray( uncompressed ); return compressed; }
/** * Test serialization with a null object. Verify that we don't get an error. * <p> * @throws Exception on error */ public void testSerialize_NullInput() throws Exception { // SETUP CompressingSerializer serializer = new CompressingSerializer(); String before = null; // DO WORK byte[] serialized = serializer.serialize( before ); String after = (String) serializer.deSerialize( serialized, null ); // VERIFY assertNull( "Should have nothing. after =" + after, after ); }
/** * Uses default de-serialization to turn a byte array into an object. Decompresses the value * first. All exceptions are converted into IOExceptions. * <p> * @param data bytes of data * @return Object * @throws IOException on i/o problem * @throws ClassNotFoundException if class is not found during deserialization */ @Override public <T> T deSerialize( byte[] data, ClassLoader loader ) throws IOException, ClassNotFoundException { if ( data == null ) { return null; } byte[] decompressedByteArray = CompressionUtil.decompressByteArray( data ); return deserializeObject( decompressedByteArray ); }
/** * Verify that the compressed is smaller. * <p> * @throws Exception on error */ public void testSerialize_CompareCompressedAndUncompressed() throws Exception { // SETUP CompressingSerializer serializer = new CompressingSerializer(); // I hate for loops. String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdssaf dsaf sadf dsaf dsaf dsaf " + "dsafdsa fdsaf dsaf dsafdsa dsaf dsaf dsaf dsaf dsafdsa76f dsa798f dsa6fdsa 087f " + "gh 987dsahb dsahbuhbfnui nufdsa hbv87 f8vhdsgbnfv h8fdg8dfjvn8fdwgj fdsgjb9fdsjbv" + "jvhjv hg98f-dsaghj j9fdsb gfsb 9fdshjbgb987fdsbfdwgh ujbhjbhb hbfdsgh fdshb " + "Ofdsgyfesgyfdsafdsafsa333 31231"; // DO WORK byte[] compressed = serializer.serialize( before ); byte[] nonCompressed = serializer.serializeObject( before ); // VERIFY assertTrue( "Compressed should be smaller. compressed size = " + compressed.length + "nonCompressed size = " + nonCompressed.length, compressed.length < nonCompressed.length ); } }
/** * Verify that we don't get any erorrs for null input. * <p> * @throws ClassNotFoundException * @throws IOException */ public void testDeserialize_NullInput() throws IOException, ClassNotFoundException { // SETUP CompressingSerializer serializer = new CompressingSerializer(); // DO WORK Object result = serializer.deSerialize( null, null ); // VERIFY assertNull( "Should have nothing.", result ); }
/** * Verify that the compressed is smaller. * <p> * @throws Exception on error */ public void testSerialize_CompareCompressedAndUncompressed() throws Exception { // SETUP CompressingSerializer serializer = new CompressingSerializer(); // I hate for loops. String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdssaf dsaf sadf dsaf dsaf dsaf " + "dsafdsa fdsaf dsaf dsafdsa dsaf dsaf dsaf dsaf dsafdsa76f dsa798f dsa6fdsa 087f " + "gh 987dsahb dsahbuhbfnui nufdsa hbv87 f8vhdsgbnfv h8fdg8dfjvn8fdwgj fdsgjb9fdsjbv" + "jvhjv hg98f-dsaghj j9fdsb gfsb 9fdshjbgb987fdsbfdwgh ujbhjbhb hbfdsgh fdshb " + "Ofdsgyfesgyfdsafdsafsa333 31231"; // DO WORK byte[] compressed = serializer.serialize( before ); byte[] nonCompressed = serializer.serializeObject( before ); // VERIFY assertTrue( "Compressed should be smaller. compressed size = " + compressed.length + "nonCompressed size = " + nonCompressed.length, compressed.length < nonCompressed.length ); } }
/** * Verify that we don't get any erorrs for null input. * <p> * @throws ClassNotFoundException * @throws IOException */ public void testDeserialize_NullInput() throws IOException, ClassNotFoundException { // SETUP CompressingSerializer serializer = new CompressingSerializer(); // DO WORK Object result = serializer.deSerialize( null, null ); // VERIFY assertNull( "Should have nothing.", result ); }
/** * Test serialization with a null object. Verify that we don't get an error. * <p> * @throws Exception on error */ public void testSerialize_NullInput() throws Exception { // SETUP CompressingSerializer serializer = new CompressingSerializer(); String before = null; // DO WORK byte[] serialized = serializer.serialize( before ); String after = (String) serializer.deSerialize( serialized, null ); // VERIFY assertNull( "Should have nothing. after =" + after, after ); }
/** * Serializes an object using default serialization. Compresses the byte array. * <p> * @param obj object * @return byte[] * @throws IOException on i/o problem */ @Override public <T> byte[] serialize( T obj ) throws IOException { byte[] uncompressed = serializeObject( obj ); byte[] compressed = CompressionUtil.compressByteArray( uncompressed ); return compressed; }
/** * Uses default de-serialization to turn a byte array into an object. Decompresses the value * first. All exceptions are converted into IOExceptions. * <p> * @param data bytes of data * @return Object * @throws IOException on i/o problem * @throws ClassNotFoundException if class is not found during deserialization */ @Override public <T> T deSerialize( byte[] data, ClassLoader loader ) throws IOException, ClassNotFoundException { if ( data == null ) { return null; } byte[] decompressedByteArray = CompressionUtil.decompressByteArray( data ); return deserializeObject( decompressedByteArray ); }
/** * Test simple back and forth with a string. * <p> * ))<=>(( * <p> * @throws Exception on error */ public void testSimpleBackAndForth() throws Exception { // SETUP CompressingSerializer serializer = new CompressingSerializer(); // DO WORK String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231"; String after = (String) serializer.deSerialize( serializer.serialize( before ), null ); // VERIFY assertEquals( "Before and after should be the same.", before, after ); }
/** * Test simple back and forth with a string. * <p> * ))<=>(( * <p> * @throws Exception on error */ public void testSimpleBackAndForth() throws Exception { // SETUP CompressingSerializer serializer = new CompressingSerializer(); // DO WORK String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231"; String after = (String) serializer.deSerialize( serializer.serialize( before ), null ); // VERIFY assertEquals( "Before and after should be the same.", before, after ); }