FindBugs - "may fail to close stream" when using ObjectOutputStream

10,807

Solution 1

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);
    }

Solution 2

It means that s.close() will try to close underlying stream, but it may fail to do it. So to be sure you should close it on your own also. Try to add out.close() and see if warning disappears.

Share:
10,807
Eugene
Author by

Eugene

Updated on July 09, 2022

Comments

  • Eugene
    Eugene almost 2 years

    I have this piece of code, which is to write an Ojbect to a byte array stream:

         static byte[] toBytes(MyTokens tokens) throws IOException {
            ByteArrayOutputStream out = null;
            ObjectOutput s = null;
            try {
                out = new ByteArrayOutputStream();
                try {
                    s = new ObjectOutputStream(out);
                    s.writeObject(tokens);
                } finally {
                    try {
                        s.close();
                    } catch (Exception e) {
                        throw new CSBRuntimeException(e);
                    }             
                }
            } catch (Exception e) {
                throw new CSBRuntimeException(e);
            } finally {
                IOUtils.closeQuietly(out);
            }
            return out.toByteArray();
        }
    

    However, FindBugs keeps complaining about line:

    s = new ObjectOutputStream(out);
    

    that "may fail to close stream" - BAD_PRACTICE - OS_OPEN_STREAM. Can somebody help?

  • Duncan Jones
    Duncan Jones over 11 years
    Surely FindBugs would then highlight the out = new ByteArrayOutputStream(); line?
  • partlov
    partlov over 11 years
    It highlights line s = new ObjectOutputStream(out); because inside it is out.close(). It is just a warning that it can lead to errors. If that line wouldn't exist he will see no warnings.
  • Eugene
    Eugene over 11 years
    I already tried this, but IOUtils.closeQuietly() just does not work with ObjectOutput, so it cannot close "s". It's only allow OutputStream...
  • Evgeniy Dorofeev
    Evgeniy Dorofeev over 11 years
    Try it again. It works. ObjectOutputStream extends OutputStream so it is OutputStream
  • Eugene
    Eugene over 11 years
    I don't think so: "cannot find symbol symbol : method closeQuietly(java.io.ObjectOutput) location: class org.apache.commons.io.IOUtils"
  • Eugene
    Eugene over 11 years
    Maybe "s" is defined as ObjectOutput type causing this
  • Evgeniy Dorofeev
    Evgeniy Dorofeev over 11 years
    right, use ObjectOutputStream directly, ObjectOutput is an interface
  • hipokito
    hipokito about 7 years
    We have Java 8 but we can't use try with resources because of Sonar bug but IOUtils.closeQuietly(s); helped, thank you.