This post originated from an RSS feed registered with Java Buzz
by Joey Gibson.
Original Post: Jython Is Just Too Useful
Feed Title: Joey Gibson's Blog
Feed URL: http://www.jobkabob.com/index.html
Feed Description: Thoughts, musings, ramblings and rants on Java, Ruby, Python, obscure languages, politics and other exciting topics.
A colleague just came to me asking about Java serialization and output
options. We're going to store some partially filled Serializable
DTOs in a BLOB in
our database so he needed some info. Our talk then turned to options for
storage and such. He already knew about using ObjectOutputStream on
top of a FileOutputStream, but I also told him how to get a byte array
from the object using a ByteArrayOutputStream. To illustrate, I fired up Jython in interactive mode and typed
the following:
There are lots of cool things there, but specifically, notice the bits
in red. I couldn't remember the method to call to get the byte array,
so by using the Python dir() method on the class of the
object, I got a list of available methods. toByteArray()
was the ticket and you can see both the array itself and then that I
went further and deserialized the byte array using a
ByteArrayInputStream. Think about how many lines of Java code I would
have had to write to show him the same thing. But even if the syntax
were just as verbose as Java, not having an edit-compile-run cycle
made the demo far faster and productive than it would have otherwise
been.