Skip to content

Instantly share code, notes, and snippets.

@milovtim
Created September 4, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milovtim/2c094e5e09c78b482929 to your computer and use it in GitHub Desktop.
Save milovtim/2c094e5e09c78b482929 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 groovy.transform.Canonical
import spock.lang.Specification
import java.time.LocalDate
@Canonical
class Person implements Serializable {
String name
String birthDate
String phone
}
class HessianSpec extends Specification {
def deflation = new Deflation()
def "Serialize deserialize"() {
given:
def someInt = 123
def persons = [
new Person("John", LocalDate.of(1983, 1, 9).toString(), '3827857'),
new Person("David", LocalDate.of(1974, 6, 16).toString(), '846726'),
]
def data = serializeSomeData(someInt, persons)
when:
def (outInt, outPerson) = deserialize(data)
then:
outInt && outPerson
persons == outPerson
}
def deserialize(byte[] bytes) {
def inputStream = new ByteArrayInputStream(bytes)
def hi = new Hessian2Input(inputStream)
def unwrappedHI = deflation.unwrap(hi)
unwrappedHI.startMessage()
def outInt = unwrappedHI.readInt()
def resultList = []
def start = unwrappedHI.readListStart()
def object = unwrappedHI.readObject()
resultList << object
unwrappedHI.readListEnd()
unwrappedHI.completeMessage()
unwrappedHI.close()
inputStream.close()
[outInt, resultList]
}
def serializeSomeData(int i, List<Person> persons) {
def outStream = new ByteArrayOutputStream()
def ho = new Hessian2Output(outStream)
def wrapedHO = deflation.wrap(ho)
wrapedHO.startMessage()
wrapedHO.writeInt i
// ho.writeListBegin(persons.size(), nameC)
wrapedHO.writeListBegin(persons.size(), Person.canonicalName)
persons.each { wrapedHO.writeObject it }
wrapedHO.writeListEnd()
wrapedHO.completeMessage()
wrapedHO.close()
outStream.toByteArray()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment