sun.net.www.protocol.file.FileURLConnection.getContentLength() Vs URLConnection.getContentLength() for content unknown cases.
Matteo Mortari
matteo.mortari at gmail.com
Mon Nov 14 20:57:49 UTC 2016
Hello,
I'm using URL and URLConnection for making reference to files,
implementation I get behind is sun.net.www.protocol.file.FileURLConnection:
I have a couple questions about its implementation, please?
If I create an URLConnection for a non-existent file:
> int a = new URL("file:///dev/null/asd").openConnection().getContentLength()
> a ==> 0
however in the javadoc for the URLConnection.getContentLength() it is
written:
> Returns: the content length of the resource that this connection's URL
> references, -1 if the content length is not known, or if the content length
> is greater than Integer.MAX_VALUE.
Q1: I would have actually expected getContentLength() to return -1 in the
case the URLConnection is representing a non-existent file ?
Also, by analogy of a not known content length:
> int a = new URL("http://asd.openjdk.java.net
> ").openConnection().getContentLength()
> a ==> -1
Thanks to OpenJDK I can see in sun.net.www.protocol.file.FileURLConnection
that actually the initialization seems aligned with the URLConnection
javadoc, Line 64 [1]:
> long length = -1;
yet it is Line 114 [2] which is a bit more obscure to me
> if (!initializedHeaders || !exists) {
Q2: Why on line 114 the need to continue within the body of the if
statement, also in the cases where !exists? This would continue to check
for file length and last modified date also in the case file !exists.
Possibly for the scenario it was existing at a previous iteration and no
longer now? Yet that would "reinitialize" at a different value if compared
to Line 64, because file.length(); called inside the body of the if
statement returns "0L if the file does not exist".
Pardon me if these questions are banal, but I cannot understand the reason
behind the second part of the if check at Line 114, " || !exists ". During
a "Ask the JDK Architects" conference talk session I asked where I can
redirect this type of Qs, as I'm not sure if a bug or a corner-case, and I
was redirected to this ML; if there is more appropriate place, kindly let
me know.
Can you shed some lights on the rationale behind line 114, please?
[1]
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/file/FileURLConnection.java#l64
[2]
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/net/www/protocol/file/FileURLConnection.java#l114
Thank you in advance,
Kind Regards,
MM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/net-dev/attachments/20161114/5b191f9f/attachment-0001.html>
More information about the net-dev
mailing list