java: ensure that the stream is cleaned up before the method returns

Change-Id: Id3efeda2fd66173ba2f5662eaacb3629da54573d
Reviewed-on: https://gerrit.libreoffice.org/11991
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Tested-by: Noel Grandin <noelgrandin@gmail.com>