ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/util/http/FastHttpDateFormat.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
Status
scanned
Type
file
Name
FastHttpDateFormat.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
5745 (5.6 KB)
MD5
ba3742a028b226c1bcb44eb36e0d728d
SHA1
28d90658cdb02d8f551af1771f9d303d416ced73
SHA256
14663f61a8bf1c51e9b5e652b9754962ce132e988c615279caf18d466a3fd42e
SHA512

      
    
SHA1_git
18626f66a531b003383d5ffb2e10bce06879a2ec
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
FastHttpDateFormat.java | 5.6 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.tomcat.util.http; import java.text.ParseException; import java.util.Date; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; /** * Utility class to generate HTTP dates. */ public final class FastHttpDateFormat { // -------------------------------------------------------------- Variables private static final int CACHE_SIZE = Integer.getInteger("org.apache.tomcat.util.http.FastHttpDateFormat.CACHE_SIZE", 1000).intValue(); // HTTP date formats private static final String DATE_RFC5322 = "EEE, dd MMM yyyy HH:mm:ss z"; private static final String DATE_OBSOLETE_RFC850 = "EEEEEE, dd-MMM-yy HH:mm:ss zzz"; private static final String DATE_OBSOLETE_ASCTIME = "EEE MMMM d HH:mm:ss yyyy"; private static final ConcurrentDateFormat FORMAT_RFC5322; private static final ConcurrentDateFormat FORMAT_OBSOLETE_RFC850; private static final ConcurrentDateFormat FORMAT_OBSOLETE_ASCTIME; private static final ConcurrentDateFormat[] httpParseFormats; static { // All the formats that use a timezone use GMT TimeZone tz = TimeZone.getTimeZone("GMT"); FORMAT_RFC5322 = new ConcurrentDateFormat(DATE_RFC5322, Locale.US, tz); FORMAT_OBSOLETE_RFC850 = new ConcurrentDateFormat(DATE_OBSOLETE_RFC850, Locale.US, tz); FORMAT_OBSOLETE_ASCTIME = new ConcurrentDateFormat(DATE_OBSOLETE_ASCTIME, Locale.US, tz); httpParseFormats = new ConcurrentDateFormat[] { FORMAT_RFC5322, FORMAT_OBSOLETE_RFC850, FORMAT_OBSOLETE_ASCTIME }; } /** * Instant on which the currentDate object was generated. */ private static volatile long currentDateGeneratedInSeconds = 0L; /** * Current formatted date. */ private static String currentDate = null; /** * Formatter cache. * <p> * Note: This needs to be a ConcurrentHashMap for correct operation so declare it as such (rather than as Map). */ private static final ConcurrentHashMap<Long,String> formatCache = new ConcurrentHashMap<>(CACHE_SIZE); /** * Parser cache. */ private static final Map<String,Long> parseCache = new ConcurrentHashMap<>(CACHE_SIZE); // --------------------------------------------------------- Public Methods /** * Get the current date in HTTP format. * * @return the HTTP date */ public static String getCurrentDate() { // according rfc5322, date/time data is accurate to the second. long nowInSeconds = System.currentTimeMillis() / 1000L; if (nowInSeconds != currentDateGeneratedInSeconds) { currentDate = FORMAT_RFC5322.format(new Date(nowInSeconds * 1000L)); currentDateGeneratedInSeconds = nowInSeconds; } return currentDate; } /** * Get the HTTP format of the specified date. * * @param value The date * * @return the HTTP date */ public static String formatDate(long value) { Long longValue = Long.valueOf(value); String cachedDate = formatCache.get(longValue); if (cachedDate != null) { return cachedDate; } String newDate = FORMAT_RFC5322.format(new Date(value)); updateFormatCache(longValue, newDate); return newDate; } /** * Try to parse the given date as an HTTP date. * * @param value The HTTP date * * @return the date as a long or <code>-1</code> if the value cannot be parsed */ public static long parseDate(String value) { Long cachedDate = parseCache.get(value); if (cachedDate != null) { return cachedDate.longValue(); } long date = -1; for (int i = 0; (date == -1) && (i < httpParseFormats.length); i++) { try { date = httpParseFormats[i].parse(value).getTime(); updateParseCache(value, Long.valueOf(date)); } catch (ParseException e) { // Ignore } } return date; } /** * Update cache. */ private static void updateFormatCache(Long key, String value) { if (value == null) { return; } if (formatCache.size() > CACHE_SIZE) { formatCache.clear(); } formatCache.put(key, value); } /** * Update cache. */ private static void updateParseCache(String key, Long value) { if (value == null) { return; } if (parseCache.size() > CACHE_SIZE) { parseCache.clear(); } parseCache.put(key, value); } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
20.0
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9