java - Behaviour of try-with-resources with closable parameters -


does java-7's try-with-resources require closable assigned directly variable? in short, block of code...

    try (final objectinputstream ois = new objectinputstream(             new bytearrayinputstream(data))) {         return ois.readobject();     } 

equivalent block?...

    try (final bytearrayinputstream in = new bytearrayinputstream(data);          final objectinputstream ois = new objectinputstream(in)) {         return ois.readobject();     } 

my understanding of section 14.20.3 of java language specification says not same , resources must assigned. surprising common usage standpoint , can't find documentation warning against pattern.

the 2 blocks not equivalent in sense won't generate same code. since objectinputstream.close() call close() on bytearrayinputstream passed it, first block fine.

edit: forgot unlike reasonable constructions new bufferedinputstream(new *inputstream(...)), objectinputstream constructor reads stream pass , reasonably throw exception. reason i'd recommend second block, not first block.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -