| 1 | |
package org.webslinger.util; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileInputStream; |
| 5 | |
import java.io.InputStream; |
| 6 | |
import java.io.IOException; |
| 7 | |
import java.io.RandomAccessFile; |
| 8 | |
|
| 9 | |
import org.webslinger.util.GeneratedResult; |
| 10 | |
import org.webslinger.util.TTLCachedObject; |
| 11 | |
|
| 12 | 0 | public abstract class SerializedTTLCachedObject extends TTLCachedObject<File> { |
| 13 | |
public RandomAccessFile getRandomAccess() throws IOException { |
| 14 | 0 | return new RandomAccessFile(getObject(), "r"); |
| 15 | |
} |
| 16 | |
|
| 17 | |
public InputStream getStream() throws IOException { |
| 18 | 0 | return new FileInputStream(getObject()); |
| 19 | |
} |
| 20 | |
|
| 21 | |
protected GeneratedResult<File> generate(File old) throws IOException { |
| 22 | 0 | if (old != null) old.delete(); |
| 23 | 0 | File file = createTempFile(); |
| 24 | 0 | long lastModifiedTime = create(file); |
| 25 | 0 | return new GeneratedResult<File>(lastModifiedTime, file); |
| 26 | |
} |
| 27 | |
|
| 28 | |
protected File createTempFile() throws IOException { |
| 29 | 0 | String base = getClass().getName(); |
| 30 | 0 | int period = base.lastIndexOf("."); |
| 31 | 0 | if (period != -1) base = base.substring(period + 1); |
| 32 | 0 | File file = File.createTempFile(base, ".tmp"); |
| 33 | 0 | file.deleteOnExit(); |
| 34 | 0 | return file; |
| 35 | |
} |
| 36 | |
|
| 37 | |
protected abstract long create(File file) throws IOException; |
| 38 | |
} |