Make java 1.6 compatible

It was mostly Yifan's change, which I cherry-picked on top of the newest master, and closed the output stream properly after try block.
done, changed to -turn, hopefully we are going to move to java 7 soon and we can actually get rid of this fork