FileDocCategorySizeDatePackage
FastHttpDateFormat.javaAPI DocApache Tomcat 6.0.146436Fri Jul 20 04:20:34 BST 2007org.apache.tomcat.util.http

FastHttpDateFormat

public final class FastHttpDateFormat extends Object
Utility class to generate HTTP dates.
author
Remy Maucherat

Fields Summary
protected static final int
CACHE_SIZE
protected static final SimpleDateFormat
format
HTTP date format.
protected static final SimpleDateFormat[]
formats
The set of SimpleDateFormat formats to use in getDateHeader().
protected static final TimeZone
gmtZone
protected static long
currentDateGenerated
Instant on which the currentDate object was generated.
protected static String
currentDate
Current formatted date.
protected static final ConcurrentHashMap
formatCache
Formatter cache.
protected static final ConcurrentHashMap
parseCache
Parser cache.
Constructors Summary
Methods Summary
public static final java.lang.StringformatDate(long value, java.text.DateFormat threadLocalformat)
Get the HTTP format of the specified date.


        Long longValue = new Long(value);
        String cachedDate = formatCache.get(longValue);
        if (cachedDate != null)
            return cachedDate;

        String newDate = null;
        Date dateValue = new Date(value);
        if (threadLocalformat != null) {
            newDate = threadLocalformat.format(dateValue);
            updateFormatCache(longValue, newDate);
        } else {
            synchronized (formatCache) {
                synchronized (format) {
                    newDate = format.format(dateValue);
                }
                updateFormatCache(longValue, newDate);
            }
        }
        return newDate;

    
public static final java.lang.StringgetCurrentDate()
Get the current date in HTTP format.



    // --------------------------------------------------------- Public Methods


                
         

        long now = System.currentTimeMillis();
        if ((now - currentDateGenerated) > 1000) {
            synchronized (format) {
                if ((now - currentDateGenerated) > 1000) {
                    currentDateGenerated = now;
                    currentDate = format.format(new Date(now));
                }
            }
        }
        return currentDate;

    
private static final java.lang.LonginternalParseDate(java.lang.String value, java.text.DateFormat[] formats)
Parse date with given formatters.

        Date date = null;
        for (int i = 0; (date == null) && (i < formats.length); i++) {
            try {
                date = formats[i].parse(value);
            } catch (ParseException e) {
                ;
            }
        }
        if (date == null) {
            return null;
        }
        return new Long(date.getTime());
    
public static final longparseDate(java.lang.String value, java.text.DateFormat[] threadLocalformats)
Try to parse the given date as a HTTP date.


        Long cachedDate = parseCache.get(value);
        if (cachedDate != null)
            return cachedDate.longValue();

        Long date = null;
        if (threadLocalformats != null) {
            date = internalParseDate(value, threadLocalformats);
            updateParseCache(value, date);
        } else {
            synchronized (parseCache) {
                date = internalParseDate(value, formats);
                updateParseCache(value, date);
            }
        }
        if (date == null) {
            return (-1L);
        } else {
            return date.longValue();
        }

    
private static voidupdateFormatCache(java.lang.Long key, java.lang.String value)
Update cache.

        if (value == null) {
            return;
        }
        if (formatCache.size() > CACHE_SIZE) {
            formatCache.clear();
        }
        formatCache.put(key, value);
    
private static voidupdateParseCache(java.lang.String key, java.lang.Long value)
Update cache.

        if (value == null) {
            return;
        }
        if (parseCache.size() > CACHE_SIZE) {
            parseCache.clear();
        }
        parseCache.put(key, value);