jar_cacheXXXXXXX ......

just fall into another plugin bugs todays …

I was wondering why I got out of disk space and after looking for some minutes I finally found this folder: XX:/Users/DzzD/AppData/Local/Temp/ (on Vista, this is a little different folder on XP but bug can be reproduced with XP too) it was containing hundreds of thousands file each named jar_cacheXXXXX ( more than 14GB ! ), seems that those files are created when using URLConnection with setUseCache(true) the funny things is that they are even NOT used as cached files (always reloaded) and they are not jar files… in my particular case renaming them to “.png” works and I can then open them with any image editor…

so conclusion are : never deleted and never used as cached file version and file are not what the name would indicate (jar_cacheXXX are not jar …)

( NB: after removing setUsecahe(true) all is going nice & faster )

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4166799

and they said fixed ?!?

Oracle should hire another bunch of monkeys.

Most clientside bugs that plague Java are the kind of bugs you make in highschool.

here is a test case :

open you temp folder X:/Users/~/AppData/Local/Temp (on Vista) and try to run this Applet http://demo.dzzd.net/JarCacheBug/ it will fullfill your disk (no fear it would requiere a very long time…)

this only happen on aborted or unclosed transferts : developpers may not use setUseCache(true) on URLConnection or ensure that stream is always properly closed to solve this but it would be a lot better if the JVM was ALWAYS freing those files when exiting …

EDIT: bug seems to be appear randomly… sometimes it work nice

/**
 * JarCacheBug Applet application
 * 
 * Using URLConnection with setUseCache(true) have severals issues :
 *  - create hundreds of useless (not used as cache) in ~/AppData/Local/Temp/ (for Vista) 

 *  - slow down the network 

 *  - misnamed files (jar_cacheXXXXX files can be any kind of file and are the raw copy of the downloaded ressource ec: png / class / txt / etc.. files )

 * 

 * this Applet will download several times the same file from the server using URLConnection & setUseCache(true) and intentionaly not gracefully end the connection , while running open your XX:/Users/~/AppData/Local/Temp/ directory (or the equivalent on your system)

 * 
 * @author Bruno Augier http://dzzd.net/ 
 * @version 1.00 2010/04/02
 */
 
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class JarCacheBug extends Applet implements Runnable
{
	public static final long serialVersionUID = 0x01;
	
	
	private volatile boolean threadMustRun;
	private volatile Thread thread;
	private volatile int downloadCount;
	private volatile int downloadSize;
	
	public void init() 
	{
		this.threadMustRun=true;
		this.downloadCount=0;
		this.downloadSize=0;
	}
	
	public void start()
	{
		this.thread=new Thread(this);
		this.thread.start();
	}
	
	public void run()
	{
		try
		{
		
			while(this.threadMustRun)
			{
				URL url=new URL(this.getCodeBase()+"/JAVA.PNG");
				URLConnection uc=url.openConnection();
				uc.setUseCaches(true);
				
				int size=uc.getContentLength();
				
				if(size==-1)
					throw new Exception(" No source file length ");
				
				byte data[]=new byte[size];
				
				InputStream is=uc.getInputStream();
				
				int nbRead=0;
				do
				{
					int nbToRead=Math.min(size-nbRead,10*1024);
					int nb=is.read(data,nbRead,nbToRead);
				
					if(nb==-1)
						throw new Exception(" reading error ");
				
					nbRead+=nb;
				
				}
				while(nbRead!=size);
				
				//Simulate transfert abort by not closing the stream
				//is.close();
				
				this.downloadSize+=size;				
				this.downloadCount++;
				
				this.repaint();
				
				Thread.sleep(100);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		this.threadMustRun=false;
		this.thread=null;
	}

	public void paint(Graphics g) 
	{
		g.drawString( this.downloadCount + " downloads finished (" + this.downloadSize + " total bytes) ", 50, 60 );
	}
	
	public void destroy()
	{
		this.threadMustRun=false;
		try
		{
			this.thread.join();
		}
		catch(InterruptedException ie)
		{
			ie.printStackTrace();
		}
	}
}

hum… not sure how to always reproduce it … seems that if the ressource is loaded once than it does not appear anymor , dont know this version http://demo.dzzd.net/JarCacheBug/ seems now to always cause the bug for me … I changed the image

the jar_cacheXXX files sometimes appears when you close the applet and not while it is running

I would say that the issue seems to be that when you close a browser with an applet every opened URLConnection using cache (not closed inputstream) even old one are all copied to some jar_cacheXXX files in the temp dir rather than being discarded