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 <action> type attribute can be add,u
<body>
<release version="3.0" date="TBA" description="TBA">
+ <action issue="NET-367" dev="sebb" type="fix">
+ ntp.TimeStamp uses incorrect lazy initialisation of static fields
simpleFormatter and utcFormatter.
+ </action>
<action issue="NET-381" dev="sebb" type="update">
Parsing is inefficient, as it parses everything twice.
</action>
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<TimeStamp>
{
+ 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<DateFormat> simpleFormatter = null;
- private static SoftReference<DateFormat> 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<DateFormat>(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<DateFormat>(formatter);
+ utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
Date ntpDate = getDate();
- synchronized (formatter) {
- return formatter.format(ntpDate);
- }
+ return utcFormatter.format(ntpDate);
}
/***