All pastes #1078137 Raw Edit

CachedReadOnlyFile

public java v1 · immutable
#1078137 ·published 2008-07-21 05:39 UTC
rendered paste body
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Timer;import java.util.TimerTask;public class CachedReadOnlyFile{	private	long 			refreshDelay = 0;	private	File			originalFile = null;	private	File 			cachedFile = null;	private	FileInputStream	cachedStream = null;	private boolean			errorFlag = false;	private Timer			timer = null;		public CachedReadOnlyFile(File file, long msDelay) throws Exception{		 //call update() and all the other crap necessary for it		originalFile = file;		update();		if(errorFlag){			throw new IOException("Failure in creation of cached file or reading of original file.  Check permissions and try again.");		}				// create the timer and set it to run every msDelay milliseconds		timer = new Timer();		timer.schedule(new MyTask(), 10000, msDelay);	}	private synchronized void update(){		errorFlag = true;				if(cachedStream != null){			try{				cachedStream.close();			} catch(Exception ex){				ex.printStackTrace();				return;			} finally{				cachedStream = null;				Runtime.getRuntime().gc();			}		}				if(cachedFile != null){			try{				cachedFile.delete();			} catch(Exception ex){				ex.printStackTrace();				return;			} finally{				cachedFile = null;				Runtime.getRuntime().gc();			}		}				cachedFile = new File("cached" + originalFile.getName());		try{			cachedFile.createNewFile();		} catch(Exception ex){			ex.printStackTrace();			return;		}				FileInputStream		reader = null;		FileOutputStream	writer = null;		try{			reader = new FileInputStream(originalFile);			writer = new FileOutputStream(cachedFile);		} catch(Exception ex){			ex.printStackTrace();			return;		}				try{			while(reader.available() != 0){				writer.write(reader.read());			}		} catch(Exception ex){			ex.printStackTrace();			return;		}				try{			cachedStream = new FileInputStream(cachedFile);		} catch(Exception ex){			ex.printStackTrace();			return;		}				errorFlag = false;	}		public synchronized int available(){		try{			return cachedStream.available();		} catch(Exception ex){			ex.printStackTrace();		}		return 0;	}	public synchronized int read(){		try{			return cachedStream.read();		} catch(Exception ex){			ex.printStackTrace();		}		return 0;	}	public synchronized int read(byte[] b){		try{			return cachedStream.read(b);		} catch(Exception ex){			ex.printStackTrace();		}		return 0;	}	public synchronized int read(byte[] b, int off, int len){		try{			return cachedStream.read(b, off, len);		} catch(Exception ex){			ex.printStackTrace();		}		return 0;	}	public synchronized long skip(long n){		try{			return cachedStream.skip(n);		} catch(Exception ex){			ex.printStackTrace();		}		return 0;	}		protected void finalize() throws Throwable{		try {			cachedStream.close();			cachedFile.delete();		} catch(Exception ex){			ex.printStackTrace();		} finally{			super.finalize();		}	}					private class MyTask extends TimerTask{		public MyTask(){			super();		}		public void run(){			update();		}	}}