[#1384] Cache HttpHeaderDateFormat in ThreadLocal

This commit is contained in:
Norman Maurer 2013-05-27 06:57:15 +02:00
parent 97332f6436
commit 6e1c5926ee
6 changed files with 19 additions and 7 deletions

View File

@ -132,7 +132,7 @@ public class CookieDecoder {
} else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) {
try {
long maxAgeMillis =
new HttpHeaderDateFormat().parse(value).getTime() -
HttpHeaderDateFormat.get().parse(value).getTime() -
System.currentTimeMillis();
maxAge = (int) (maxAgeMillis / 1000) +

View File

@ -110,7 +110,7 @@ public class CookieEncoder {
if (cookie.getMaxAge() != Integer.MIN_VALUE) {
if (cookie.getVersion() == 0) {
addUnquoted(sb, CookieHeaderNames.EXPIRES,
new HttpHeaderDateFormat().format(
HttpHeaderDateFormat.get().format(
new Date(System.currentTimeMillis() +
cookie.getMaxAge() * 1000L)));
} else {

View File

@ -37,11 +37,23 @@ final class HttpHeaderDateFormat extends SimpleDateFormat {
private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
private static final ThreadLocal<HttpHeaderDateFormat> FORMAT_THREAD_LOCAL =
new ThreadLocal<HttpHeaderDateFormat>() {
@Override
protected HttpHeaderDateFormat initialValue() {
return new HttpHeaderDateFormat();
}
};
public static HttpHeaderDateFormat get() {
return FORMAT_THREAD_LOCAL.get();
}
/**
* Standard date format<p>
* Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
*/
HttpHeaderDateFormat() {
private HttpHeaderDateFormat() {
super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
setTimeZone(TimeZone.getTimeZone("GMT"));
}

View File

@ -27,7 +27,7 @@ public class CookieDecoderTest {
@Test
public void testDecodingSingleCookieV0() {
String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
cookieString = cookieString.replace("XXX", new HttpHeaderDateFormat().format(new Date(System.currentTimeMillis() + 50000)));
cookieString = cookieString.replace("XXX", HttpHeaderDateFormat.get().format(new Date(System.currentTimeMillis() + 50000)));
CookieDecoder cookieDecoder = new CookieDecoder();
Set<Cookie> cookies = cookieDecoder.decode(cookieString);

View File

@ -26,7 +26,7 @@ public class CookieEncoderTest {
@Test
public void testEncodingSingleCookieV0() {
String result = "myCookie=myValue; Expires=XXX; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure";
DateFormat df = new HttpHeaderDateFormat();
DateFormat df = HttpHeaderDateFormat.get();
Cookie cookie = new DefaultCookie("myCookie", "myValue");
CookieEncoder encoder = new CookieEncoder(true);
encoder.addCookie(cookie);

View File

@ -32,7 +32,7 @@ public class HttpHeaderDateFormatTest {
@Test
public void testParse() throws ParseException {
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
HttpHeaderDateFormat format = HttpHeaderDateFormat.get();
final Date parsedDateWithSingleDigitDay = format.parse("Sun, 6 Nov 1994 08:49:37 GMT");
Assert.assertNotNull(parsedDateWithSingleDigitDay);
@ -61,7 +61,7 @@ public class HttpHeaderDateFormatTest {
@Test
public void testFormat() {
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
HttpHeaderDateFormat format = HttpHeaderDateFormat.get();
final String formatted = format.format(DATE);
Assert.assertNotNull(formatted);