Last active
December 18, 2015 01:29
-
-
Save ikatsov/5704511 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.caucho.hessian.io.Deflation; | |
import com.caucho.hessian.io.Hessian2Input; | |
import com.caucho.hessian.io.Hessian2Output; | |
import java.io.*; | |
public class HessianSerializer { | |
public static byte[] toByteArray(Serializable o, boolean deflate, int expectedBytesCount) throws IOException { | |
return toOutputStream(new ByteArrayOutputStream(expectedBytesCount), o, deflate).toByteArray(); | |
} | |
public static <T extends OutputStream> T toOutputStream(T os, Serializable o, boolean deflate) throws IOException { | |
Hessian2Output hout; | |
if (deflate) { | |
hout = new Deflation().wrap(new Hessian2Output(os)); | |
} else { | |
hout = new Hessian2Output(os); | |
} | |
hout.writeObject(o); | |
hout.close(); | |
return os; | |
} | |
public static <T> T fromByteArray(byte[] data, boolean deflate) throws IOException { | |
return (T)fromOutputStream(new ByteArrayInputStream(data),deflate); | |
} | |
public static <X extends InputStream, T> T fromOutputStream(X is, boolean deflate) throws IOException { | |
Hessian2Input hout; | |
if (deflate) { | |
hout = new Deflation().unwrap(new Hessian2Input(is)); | |
} else { | |
hout = new Hessian2Input(is); | |
} | |
T result = (T)hout.readObject(); | |
hout.close(); | |
return result; | |
} | |
} | |
-------- | |
<dependency> | |
<groupId>com.caucho</groupId> | |
<artifactId>hessian</artifactId> | |
<version>4.0.7</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment