Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 7676 invoked from network); 22 Mar 2011 22:39:30 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 22 Mar 2011 22:39:30 -0000 Received: (qmail 89639 invoked by uid 500); 22 Mar 2011 22:39:30 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 89560 invoked by uid 500); 22 Mar 2011 22:39:30 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 89552 invoked by uid 99); 22 Mar 2011 22:39:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 Mar 2011 22:39:30 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 22 Mar 2011 22:39:29 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2202C23889E0; Tue, 22 Mar 2011 22:39:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1084392 - in /commons/proper/net/trunk/src: changes/changes.xml main/java/org/apache/commons/net/ntp/TimeStamp.java Date: Tue, 22 Mar 2011 22:39:09 -0000 To: commits@commons.apache.org From: sebb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110322223909.2202C23889E0@eris.apache.org> Author: sebb Date: Tue Mar 22 22:39:08 2011 New Revision: 1084392 URL: http://svn.apache.org/viewvc?rev=1084392&view=rev Log: NET-367 ntp.TimeStamp uses incorrect lazy initialisation of static fields simpleFormatter and utcFormatter Modified: commons/proper/net/trunk/src/changes/changes.xml commons/proper/net/trunk/src/main/java/org/apache/commons/net/ntp/TimeStamp.java Modified: commons/proper/net/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1084392&r1=1084391&r2=1084392&view=diff ============================================================================== --- commons/proper/net/trunk/src/changes/changes.xml (original) +++ commons/proper/net/trunk/src/changes/changes.xml Tue Mar 22 22:39:08 2011 @@ -57,6 +57,9 @@ The type attribute can be add,u + + ntp.TimeStamp uses incorrect lazy initialisation of static fields simpleFormatter and utcFormatter. + Parsing is inefficient, as it parses everything twice. Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/ntp/TimeStamp.java URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ntp/TimeStamp.java?rev=1084392&r1=1084391&r2=1084392&view=diff ============================================================================== --- commons/proper/net/trunk/src/main/java/org/apache/commons/net/ntp/TimeStamp.java (original) +++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/ntp/TimeStamp.java Tue Mar 22 22:39:08 2011 @@ -18,7 +18,6 @@ package org.apache.commons.net.ntp; -import java.lang.ref.SoftReference; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -45,6 +44,7 @@ import java.util.TimeZone; */ public class TimeStamp implements java.io.Serializable, Comparable { + private static final long serialVersionUID = 8139806907588338737L; /** * baseline NTP time if bit-0=0 -> 7-Feb-2036 @ 06:28:16 UTC @@ -62,12 +62,6 @@ public class TimeStamp implements java.i */ public final static String NTP_DATE_FORMAT = "EEE, MMM dd yyyy HH:mm:ss.SSS"; - /* - * Caches for the DateFormatters used by various toString methods. - */ - private static SoftReference simpleFormatter = null; - private static SoftReference utcFormatter = null; - /** * NTP timestamp value: 64-bit unsigned fixed-point number as defined in RFC-1305 * with high-order 32 bits the seconds field and the low-order 32-bits the @@ -75,7 +69,8 @@ public class TimeStamp implements java.i */ private final long ntpTime; - private static final long serialVersionUID = 8139806907588338737L; + private DateFormat simpleFormatter; + private DateFormat utcFormatter; // initialization of static time bases /* @@ -404,20 +399,12 @@ public class TimeStamp implements java.i */ public String toDateString() { - DateFormat formatter = null; - if (simpleFormatter != null) { - formatter = simpleFormatter.get(); - } - if (formatter == null) { - // No cache yet, or cached formatter GC'd - formatter = new SimpleDateFormat(NTP_DATE_FORMAT, Locale.US); - formatter.setTimeZone(TimeZone.getDefault()); - simpleFormatter = new SoftReference(formatter); + if (simpleFormatter == null) { + simpleFormatter = new SimpleDateFormat(NTP_DATE_FORMAT, Locale.US); + simpleFormatter.setTimeZone(TimeZone.getDefault()); } Date ntpDate = getDate(); - synchronized (formatter) { - return formatter.format(ntpDate); - } + return simpleFormatter.format(ntpDate); } /*** @@ -431,20 +418,13 @@ public class TimeStamp implements java.i */ public String toUTCString() { - DateFormat formatter = null; - if (utcFormatter != null) - formatter = utcFormatter.get(); - if (formatter == null) { - // No cache yet, or cached formatter GC'd - formatter = new SimpleDateFormat(NTP_DATE_FORMAT + " 'UTC'", + if (utcFormatter == null) { + utcFormatter = new SimpleDateFormat(NTP_DATE_FORMAT + " 'UTC'", Locale.US); - formatter.setTimeZone(TimeZone.getTimeZone("UTC")); - utcFormatter = new SoftReference(formatter); + utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); } Date ntpDate = getDate(); - synchronized (formatter) { - return formatter.format(ntpDate); - } + return utcFormatter.format(ntpDate); } /***