Created
March 23, 2011 08:01
-
-
Save nobusue/882772 to your computer and use it in GitHub Desktop.
Groovyでオブジェクトをシリアライズしてバイト数とHexダンプを出力
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
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