"findbugs - “may fail to close stream” when using objectoutputstream" Code Answer

5

i think findbugs does not undestand that ioutils.closequietly(out) closes out.

anyway it is enough to close objectoutputstream and it will close underlying bytearrayoutputstream. this is objectoutputstream.close implementation

public void close() throws ioexception {
    flush();
    clear();
    bout.close();
}

so you can simplify your code

    bytearrayoutputstream out = new bytearrayoutputstream();
    objectoutputstream s = new objectoutputstream(out);
    try {
        s.writeobject(1);
    } finally {
        ioutils.closequietly(s);
    }

or if you are in java 7

    bytearrayoutputstream out = new bytearrayoutputstream();
    try (objectoutputstream s = new objectoutputstream(out)) {
        s.writeobject(1);
    }
By dashton on September 12 2022

Answers related to “findbugs - “may fail to close stream” when using objectoutputstream”

Only authorized users can answer the Search term. Please sign in first, or register a free account.