Skip to content

Instantly share code, notes, and snippets.

@nobusue
Created March 23, 2011 08:01
Show Gist options
  • Save nobusue/882772 to your computer and use it in GitHub Desktop.
Save nobusue/882772 to your computer and use it in GitHub Desktop.
Groovyでオブジェクトをシリアライズしてバイト数とHexダンプを出力
def obj = new Date()
try{
serialize(obj)
}catch(e){
println "Object is not Serializable"
}
def serialize(obj){
def baos = new ByteArrayOutputStream()
def oos = new ObjectOutputStream(baos)
def width = 16
oos.writeObject(obj)
println "Serialized size[bytes]: " + baos.toByteArray().size()
println "Hex dump:"
println "-"*(width*3-1)
baos.toByteArray().eachWithIndex{ b,i ->
print Integer.toHexString(b & 0xff).padLeft(2, "0") + " "
if (i % width == width-1) println ""
}
println ""
println "-"*(width*3-1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment