From: Javen O'Neal Date: Thu, 7 Jul 2016 01:10:24 +0000 (+0000) Subject: bug 59805: avoid memory leaks if time zone or locale are never set or user never... X-Git-Tag: REL_3_15_BETA3~173 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2365d88bd4d12c60d08cfb4193466961045c571d;p=poi.git bug 59805: avoid memory leaks if time zone or locale are never set or user never resets the time zone or locale; patch from apptaro git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751739 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/util/LocaleUtil.java b/src/java/org/apache/poi/util/LocaleUtil.java index 54a8d6c0ec..81bdd4a079 100644 --- a/src/java/org/apache/poi/util/LocaleUtil.java +++ b/src/java/org/apache/poi/util/LocaleUtil.java @@ -52,21 +52,8 @@ public final class LocaleUtil { */ public static final Charset CHARSET_1252 = Charset.forName("CP1252"); - private static final ThreadLocal userTimeZone = new ThreadLocal() { - @Override - @SuppressForbidden("implementation around default locales in POI") - protected TimeZone initialValue() { - return TimeZone.getDefault(); - } - }; - - private static final ThreadLocal userLocale = new ThreadLocal() { - @Override - @SuppressForbidden("implementation around default locales in POI") - protected Locale initialValue() { - return Locale.getDefault(); - } - }; + private static final ThreadLocal userTimeZone = new ThreadLocal(); + private static final ThreadLocal userLocale = new ThreadLocal(); /** * As time zone information is not stored in any format, it can be @@ -80,12 +67,17 @@ public final class LocaleUtil { } /** - * @return the time zone which is used for date calculations, defaults to UTC - */ + * @return the time zone which is used for date calculations. If not set, returns {@link TimeZone#getDefault()}. + */ + @SuppressForbidden("implementation around default locales in POI") public static TimeZone getUserTimeZone() { - return userTimeZone.get(); + TimeZone timeZone = userTimeZone.get(); + return (timeZone != null) ? timeZone : TimeZone.getDefault(); } - + + /** + * Clear the thread-local user time zone. + */ public static void resetUserTimeZone() { userTimeZone.remove(); } @@ -98,15 +90,17 @@ public final class LocaleUtil { userLocale.set(locale); } - public static void resetUserLocale() { - userLocale.remove(); - } - /** - * @return the default user locale, defaults to {@link Locale#ROOT} - */ + * @return the default user locale. If not set, returns {@link Locale#getDefault()}. + */ + @SuppressForbidden("implementation around default locales in POI") public static Locale getUserLocale() { - return userLocale.get(); + Locale locale = userLocale.get(); + return (locale != null) ? locale : Locale.getDefault(); + } + + public static void resetUserLocale() { + userLocale.remove(); } /**