Mercurial Hosting > nabble
comparison src/cachingfilter/CachedDir.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package cachingfilter; | |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import org.slf4j.Logger; | |
6 import org.slf4j.LoggerFactory; | |
7 | |
8 | |
9 final class CachedDir implements CachedPage { | |
10 private static final Logger logger = LoggerFactory.getLogger(CachedDir.class); | |
11 private static final long tooLong = 1000L*60*10; // 10 minutes | |
12 private final File dir; | |
13 private File newFile; | |
14 | |
15 CachedDir( File parent, String child ) { | |
16 this.dir = new File(parent,child); | |
17 } | |
18 | |
19 public String name() { | |
20 return dir.getName(); | |
21 } | |
22 | |
23 public boolean exists() { | |
24 if( !dir.exists() ) | |
25 return false; | |
26 File[] files = dir.listFiles(); | |
27 if( files == null ) | |
28 throw new RuntimeException("error doing listFiles of "+dir); | |
29 return files.length >= 1; | |
30 } | |
31 | |
32 public File lastFile() { | |
33 File[] files = dir.listFiles(); | |
34 if( files.length == 1 ) { | |
35 return files[0]; | |
36 } | |
37 int n = 0; | |
38 File lastFile = null; | |
39 for( File file : files ) { | |
40 String name = file.getName(); | |
41 int i = Integer.parseInt(name); | |
42 if( n < i ) { | |
43 if( lastFile != null ) | |
44 lastFile.delete(); | |
45 n = i; | |
46 lastFile = file; | |
47 } else { | |
48 file.delete(); | |
49 } | |
50 } | |
51 if( lastFile==null ) | |
52 throw new RuntimeException(dir.toString()); | |
53 return lastFile; | |
54 } | |
55 | |
56 public File newFile() throws IOException { | |
57 if( !dir.exists() && !dir.mkdir() ) | |
58 throw new RuntimeException("couldn't create "+dir); | |
59 int n = 0; | |
60 for( File file : dir.listFiles() ) { | |
61 String name = file.getName(); | |
62 int i = Integer.parseInt(name); | |
63 if( n < i ) | |
64 n = i; | |
65 file.delete(); | |
66 } | |
67 newFile = new File( dir, Integer.toString( n + 1 ) ); | |
68 if( !newFile.createNewFile() ) | |
69 throw new RuntimeException("couldn't create "+newFile); | |
70 return newFile; | |
71 } | |
72 | |
73 public void deleteNewFile() { | |
74 if( !newFile.delete() ) | |
75 throw new RuntimeException("couldn't delete new file "+newFile); | |
76 } | |
77 | |
78 public boolean delete() { | |
79 File die = new File(dir.getParentFile(),"~die"); | |
80 if( die.exists() ) { | |
81 for( File f : die.listFiles() ) { | |
82 if( !f.delete() ) { | |
83 logger.error("couldn't delete "+f); | |
84 return false; | |
85 } | |
86 } | |
87 if( !die.delete() ) { | |
88 logger.error("couldn't delete "+die); | |
89 return false; | |
90 } | |
91 } | |
92 return dir.renameTo(die); | |
93 } | |
94 | |
95 public String toString() { | |
96 return dir.toString(); | |
97 } | |
98 | |
99 } |