Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.URL cache when reading from files

Tags:

java

file

io

It seems like java is holding some kind of a cache to URL (& files). e.g. I have a file "resourcs.txt" in a jar file in my classpath. The content of this file is: "Version 1"

new java.io.BufferedReader (new java.io.InputStreamReader( new URL("jar", "", "file:test.jar!/resourcs.txt").openConnection().getInputStream())).readLine()

returns "Version 1" (as expected)

I change the file content to be "Version 2" and call again to this code. And I still get "Version 1"

How can I clear this "cache".

Notice: I found out it only happens on Linux.

like image 939
Guy Korland Avatar asked Jul 19 '12 21:07

Guy Korland


1 Answers

Because of the jar protocol used in your URL, the connection is an instance of sun.net.www.protocol.jar.JarURLConnection which takes benefit from a cache implemented in sun.net.www.protocol.jar.JarFileFactory

Source code confirms a setUseCache(false) on URLConnection implementation will prevent the use of that cache.

My hypothesis about the Linux/Windows behavior difference: the close event notification from URLJarFileCloseController interface is triggered faster on Windows because it does not appreciate to keep file handles opened for a too long period...

like image 56
Yves Martin Avatar answered Oct 30 '22 04:10

Yves Martin