The two blocks are not equivalent in the sense that they won't generate the same code. But since ObjectInputStream.close()
will call close()
on the ByteArrayInputStream
that you passed it, the first block is completely fine.
EDIT: Something I forgot is that unlike reasonable constructions like new BufferedInputStream(new *InputStream(...))
, the ObjectInputStream
constructor actually reads from the stream you pass it and thus could reasonably throw an exception. For that reason I'd actually recommend the second block, not the first block.