4

Why DataOutputStream doesn't create a "Resource leak: stream never closed " warning.

public static void main(String[] args) {
    try{
        DataOutputStream o = new DataOutputStream(System.out);
        o.writeInt(12);
    }
    catch(IOException ex){

    }
}

I thought it was because it extends FilterOutputStream But when extending my own class, it gives the warning.

Mohamed Kamaly
  • 127
  • 3
  • 10

3 Answers3

2

I think compiler (at least my Eclipse compiler) knows that System.out should never be closed. Try DataOutputStream o = new DataOutputStream(new FileOutputStream("test")); and you'll see the warning.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • The compiler doesn't know any such thing. It's an IDE warning, not a compiler error. In your example the warning should be given on the FileOutputStream, not the DataOutputStream, if the IDE is doing it correctly. – user207421 Jul 06 '14 at 07:01
  • DataOutputStream is Closeable so it's a resource. DataOutputStream o = new DataOutputStream(null); is also giving warning from Eclipse compiler. Eclipse compiler is also a compiler. – Evgeniy Dorofeev Jul 06 '14 at 07:18
  • @EJP the compiler is the one responsible for the warnings not the IDE, eclipse comes with a java compiler, that's why Evgeniy says "eclipse compiler" https://www.ibm.com/developerworks/community/blogs/Wayner/entry/did_you_know_that_eclipse?lang=en – Mohamed Kamaly Jul 07 '14 at 18:35
  • @EvgeniyDorofeev I am passing a stream as a parameter to a function, inside the function I use two filters stream, one is DataOutputStream and the other using my own filter output stream, the first gives a warning, the second one don't, so I don't think it is about the stream passed to the DataOutputStream, it is something related to DataOutputStream itself – Mohamed Kamaly Jul 07 '14 at 18:37
1

You may want to create a DataOutputStream to an underlying stream, write to it, but not close the underlying stream because you want to do some further writing. So in this case there is no resource leak even though the DataOutputStream is not closed.

However, I'm a bit surprised that you don't at least get a warning.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    Why would the same not be true for another `FilterOutputStream` subtype? – Sotirios Delimanolis Jul 06 '14 at 05:52
  • @SotiriosDelimanolis - Good question. :) It may be as Evgeniy Dorofeev suggests in his answer: that the compiler recognizes `System.out`. Or perhaps it's a bug in the analyzer (presumably Eclipse). – Ted Hopp Jul 06 '14 at 06:03
  • I'm more inclined to believe an Eclipse shortcoming. I've tried with a custom `FilterOutputStream` subtype that wraps `System.out` and it does provide a warning. – Sotirios Delimanolis Jul 06 '14 at 06:05
  • @TedHopp that's exactly what I am doing, I am passing a stream as a parameter to a function, and the function should not close the stream ... that's when I noticed inside the function the DataOutputStream doesn't give the warning and using my FilterStream derived class gives a warning ... which what got me interested in why that happens and how to apply it to my subtype if possible. – Mohamed Kamaly Jul 07 '14 at 18:40
  • @MohamedKamalKamaly - After experimenting a little more with this, I'm beginning to think that it's a bug (or maybe a feature :-)) in the Eclipse resource analyzer. (You are using Eclipse, right?) If that's the case, then I don't know that you're going to be able to apply it to your own `FilterStream` subclass. – Ted Hopp Jul 07 '14 at 20:30
0

A DataOutputStream isn't a resource, so there is no resource leak.

The underlying file or socket or system FD is the resource. In this case it is System.out, which wasn't opened in this method, so not closing it isn't a resource leak.

If you had opened a file or a socket in this method you should get this IDE warning.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Can you address `I thought it was because it extends FilterOutputStream But when extending my own class, it gives the warning.`? – Sotirios Delimanolis Jul 06 '14 at 07:47
  • _"A `DataOutputStream` isn't a resource"_ -- This is wrong. The article from Oracle ["Better Resource Management with Java SE 7"](http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html) specifically uses `DataOutputStream` as an example of a resource. The [Javadocs](http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html) also have it implementing `AutoCloseable`, which makes it a resource more or less by definition (at least for the _try-with-resources_ statement). – Ted Hopp Jul 06 '14 at 08:16
  • I would argue it's an incorrect definition. The underlying OS resource is the only resource. The DataInputStream is only a decoration. – user207421 Jul 07 '14 at 12:07
  • This `InputStream in = new PipedInputStream();` will give you a `resource leak` warning (on Eclipse) but there aren't any resources involved, as you've defined them. – Sotirios Delimanolis Jul 07 '14 at 14:41
  • @EJP I have replied to this in a another comment, here's what I said: "I am passing a stream as a parameter to a function, inside the function I use two filters stream, one is DataOutputStream and the other using my own filter output stream, the first gives a warning, the second one don't, so I don't think it is about the stream passed to the DataOutputStream, it is something related to DataOutputStream itself" – Mohamed Kamaly Jul 07 '14 at 18:42