package org.glassfish.grizzly.http.util;
import static org.glassfish.grizzly.http.util.HttpCodecUtils.toCheckedByteArray;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.glassfish.grizzly.utils.Charsets;
public final class FastHttpDateFormat {
private static final String ASCII_CHARSET_NAME = Charsets.ASCII_CHARSET.name();
private static final int CACHE_SIZE = 1000;
private static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone("GMT");
private static final SimpleDateFormatter FORMATTER = new SimpleDateFormatter();
private static final ThreadLocal<SimpleDateFormatter> FORMAT = new ThreadLocal<SimpleDateFormatter>() {
@Override
protected SimpleDateFormatter initialValue() {
return new SimpleDateFormatter();
}
};
private static final class SimpleDateFormatter {
private final Date date;
private final SimpleDateFormat f;
private final FieldPosition pos = new FieldPosition(-1);
public SimpleDateFormatter() {
date = new Date();
f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
f.setTimeZone(GMT_TIME_ZONE);
}
public String format(final long timeMillis) {
date.setTime(timeMillis);
return f.format(date);
}
public StringBuffer formatTo(final long timeMillis, final StringBuffer buffer) {
date.setTime(timeMillis);
return f.format(date, buffer, pos);
}
}
private static final ThreadLocal FORMATS = new ThreadLocal() {
@Override
protected Object initialValue() {
SimpleDateFormat[] f = new SimpleDateFormat[3];
f[0] = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
f[0].setTimeZone(GMT_TIME_ZONE);
f[1] = new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US);
f[1].setTimeZone(GMT_TIME_ZONE);
f[2] = new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US);
f[2].setTimeZone(GMT_TIME_ZONE);
return f;
}
};
private static volatile long nextGeneration;
private static final AtomicBoolean isGeneratingNow = new AtomicBoolean();
private static final StringBuffer currentDateBuffer = new StringBuffer();
private static byte[] currentDateBytes;
private static String cachedStringDate;
private static volatile byte[] dateBytesForCachedStringDate;
private static final ConcurrentMap<Long, String> formatCache = new ConcurrentHashMap<>(CACHE_SIZE, 0.75f, 64);
private static final ConcurrentMap<String, Long> parseCache = new ConcurrentHashMap<>(CACHE_SIZE, 0.75f, 64);
public static String getCurrentDate() {
final byte[] currentDateBytesNow = getCurrentDateBytes();
if (currentDateBytesNow != dateBytesForCachedStringDate) {
try {
cachedStringDate = new String(currentDateBytesNow, ASCII_CHARSET_NAME);
dateBytesForCachedStringDate = currentDateBytesNow;
} catch (UnsupportedEncodingException ignored) {
}
}
return cachedStringDate;
}
public static byte[] getCurrentDateBytes() {
final long now = System.currentTimeMillis();
final long diff = now - nextGeneration;
if (diff > 0 && (diff > 5000 || !isGeneratingNow.get() && isGeneratingNow.compareAndSet(false, true))) {
synchronized (FORMAT) {
if (now > nextGeneration) {
currentDateBuffer.setLength(0);
FORMATTER.formatTo(now, currentDateBuffer);
currentDateBytes = toCheckedByteArray(currentDateBuffer);
nextGeneration = now + 1000;
}
isGeneratingNow.set(false);
}
}
return currentDateBytes;
}
public static String formatDate(long value, DateFormat threadLocalFormat) {
value = value / 1000 * 1000;
final Long longValue = value;
String cachedDate = formatCache.get(longValue);
if (cachedDate != null) {
return cachedDate;
}
String newDate;
if (threadLocalFormat != null) {
newDate = threadLocalFormat.format(value);
} else {
newDate = FORMAT.get().format(value);
}
updateFormatCache(longValue, newDate);
return newDate;
}
public static long parseDate(final String value, DateFormat[] threadLocalformats) {
Long cachedDate = parseCache.get(value);
if (cachedDate != null) {
return cachedDate;
}
long date;
if (threadLocalformats != null) {
date = internalParseDate(value, threadLocalformats);
} else {
date = internalParseDate(value, (SimpleDateFormat[]) FORMATS.get());
}
if (date != -1) {
updateParseCache(value, date);
}
return date;
}
private static long internalParseDate(String value, DateFormat[] formats) {
for (int i = 0; i < formats.length; i++) {
try {
return formats[i].parse(value).getTime();
} catch (ParseException ignore) {
}
}
return -1;
}
private static void updateFormatCache(Long key, String value) {
if (value == null) {
return;
}
if (formatCache.size() > CACHE_SIZE) {
formatCache.clear();
}
formatCache.put(key, value);
}
private static void updateParseCache(String key, Long value) {
if (parseCache.size() > CACHE_SIZE) {
parseCache.clear();
}
parseCache.put(key, value);
}
}