Fixed FileCacheEntry factory

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@983804 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-08-09 20:26:56 +00:00
parent 143f3b0a0c
commit 1b58a2360d
2 changed files with 25 additions and 4 deletions

View File

@ -63,16 +63,20 @@ class BasicIdGenerator {
this.rnd.setSeed(System.currentTimeMillis()); this.rnd.setSeed(System.currentTimeMillis());
} }
public synchronized String generate() { public synchronized void generate(final StringBuilder buffer) {
this.count++; this.count++;
int rndnum = this.rnd.nextInt(); int rndnum = this.rnd.nextInt();
StringBuilder buffer = new StringBuilder();
buffer.append(System.currentTimeMillis()); buffer.append(System.currentTimeMillis());
buffer.append('.'); buffer.append('.');
Formatter formatter = new Formatter(buffer, Locale.US); Formatter formatter = new Formatter(buffer, Locale.US);
formatter.format("%1$016x-%2$08x", this.count, rndnum); formatter.format("%1$016x-%2$08x", this.count, rndnum);
buffer.append('.'); buffer.append('.');
buffer.append(this.hostname); buffer.append(this.hostname);
}
public String generate() {
StringBuilder buffer = new StringBuilder();
generate(buffer);
return buffer.toString(); return buffer.toString();
} }

View File

@ -64,8 +64,25 @@ public class FileCacheEntryFactory implements HttpCacheEntryFactory {
final Header[] headers, final Header[] headers,
byte[] body) throws IOException { byte[] body) throws IOException {
String uid = this.idgen.generate(); StringBuilder buffer = new StringBuilder();
File file = new File(this.cacheDir, uid + "." + requestId); this.idgen.generate(buffer);
buffer.append('.');
int len = Math.min(requestId.length(), 100);
for (int i = 0; i < len; i++) {
char ch = requestId.charAt(i);
if (Character.isLetterOrDigit(ch) || ch == '.') {
buffer.append(ch);
} else {
buffer.append('-');
}
}
File file = new File(this.cacheDir, buffer.toString());
FileOutputStream outstream = new FileOutputStream(file);
try {
outstream.write(body);
} finally {
outstream.close();
}
return new FileCacheEntry( return new FileCacheEntry(
requestDate, requestDate,
responseDate, responseDate,