From 1b58a2360d093fcd159c12674408d1f772ec7b71 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 9 Aug 2010 20:26:56 +0000 Subject: [PATCH] Fixed FileCacheEntry factory git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@983804 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/client/cache/BasicIdGenerator.java | 8 +++++-- .../client/cache/FileCacheEntryFactory.java | 21 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicIdGenerator.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicIdGenerator.java index 9a7b7c1d7..385cfc2aa 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicIdGenerator.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicIdGenerator.java @@ -63,16 +63,20 @@ class BasicIdGenerator { this.rnd.setSeed(System.currentTimeMillis()); } - public synchronized String generate() { + public synchronized void generate(final StringBuilder buffer) { this.count++; int rndnum = this.rnd.nextInt(); - StringBuilder buffer = new StringBuilder(); buffer.append(System.currentTimeMillis()); buffer.append('.'); Formatter formatter = new Formatter(buffer, Locale.US); formatter.format("%1$016x-%2$08x", this.count, rndnum); buffer.append('.'); buffer.append(this.hostname); + } + + public String generate() { + StringBuilder buffer = new StringBuilder(); + generate(buffer); return buffer.toString(); } diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/FileCacheEntryFactory.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/FileCacheEntryFactory.java index 0db889fce..377202475 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/FileCacheEntryFactory.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/FileCacheEntryFactory.java @@ -64,8 +64,25 @@ public class FileCacheEntryFactory implements HttpCacheEntryFactory { final Header[] headers, byte[] body) throws IOException { - String uid = this.idgen.generate(); - File file = new File(this.cacheDir, uid + "." + requestId); + StringBuilder buffer = new StringBuilder(); + 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( requestDate, responseDate,