Skip to content

Instantly share code, notes, and snippets.

@ikatsov
Last active December 18, 2015 01:29
Show Gist options
  • Save ikatsov/5704511 to your computer and use it in GitHub Desktop.
Save ikatsov/5704511 to your computer and use it in GitHub Desktop.
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