The Artima Developer Community
Sponsored Link

Java Buzz Forum
Jython Is Just Too Useful

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Joey Gibson

Posts: 71
Nickname: joeygibson
Registered: Jun, 2003

Joey Gibson is an opinionated software architect who blogs about technology, politics, music, etc.
Jython Is Just Too Useful Posted: Jun 26, 2003 4:08 PM
Reply to this message Reply

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.
Latest Java Buzz Posts
Latest Java Buzz Posts by Joey Gibson
Latest Posts From Joey Gibson's Blog

Advertisement
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:

[c:\tmp] jython
*sys-package-mgr*: processing modified jar, 'C:\AspectJ1.1\lib\aspectjrt.jar'
Jython 2.1 on java1.3.1_02 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> from java.io import *
>>> from java.util import*
>>> m = ArrayList()
>>> m.add("Foo")
1
>>> m.add("Bar")
1
>>> m
[Foo, Bar]
>>> baos = ByteArrayOutputStream()
>>> oos = ObjectOutputStream(baos)
>>> oos.writeObject(m)
>>> oos.close()
>>> dir(baos.class)
['__init__', 'reset', 'size', 'toByteArray', 'toString', 'write', 'writeTo']
>>> baos.toByteArray()
array([-84, -19, 0, 5, 115, 114, 0, 19, 106, 97,
  118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114,
  97, 121, 76, 105, 115, 116, 120, -127, -46, 29,
  -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105,
  122, 101, 120, 112, 0, 0, 0, 2, 119, 4, 0, 0, 0,
  10, 116, 0, 3, 70, 111, 111, 116, 0, 3, 66, 97,
  114, 120], byte)
>>> bais = ByteArrayInputStream(baos.toByteArray())
>>> ois = ObjectInputStream(bais)
>>> x = ois.readObject()
>>> x
[Foo, Bar]
>>> m
[Foo, Bar]

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.

Read: Jython Is Just Too Useful

Topic: SourceForge mailing list archives - where have they gone? Previous Topic   Next Topic Topic: Some tips for customising Maven built websites

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use