diff options
author | Javen O'Neal <onealj@apache.org> | 2016-07-07 01:10:24 +0000 |
---|---|---|
committer | Javen O'Neal <onealj@apache.org> | 2016-07-07 01:10:24 +0000 |
commit | 2365d88bd4d12c60d08cfb4193466961045c571d (patch) | |
tree | 9a107f5e233c353ec1f7b0bbff5fbcbf0db00409 /src/java/org/apache/poi/util/LocaleUtil.java | |
parent | 5c43bcf4363ea1f0497dcc70963543fa4137a0b9 (diff) | |
download | poi-2365d88bd4d12c60d08cfb4193466961045c571d.tar.gz poi-2365d88bd4d12c60d08cfb4193466961045c571d.zip |
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
Diffstat (limited to 'src/java/org/apache/poi/util/LocaleUtil.java')
-rw-r--r-- | src/java/org/apache/poi/util/LocaleUtil.java | 46 |
1 files changed, 20 insertions, 26 deletions
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<TimeZone> userTimeZone = new ThreadLocal<TimeZone>() {
- @Override
- @SuppressForbidden("implementation around default locales in POI")
- protected TimeZone initialValue() {
- return TimeZone.getDefault();
- }
- };
-
- private static final ThreadLocal<Locale> userLocale = new ThreadLocal<Locale>() {
- @Override
- @SuppressForbidden("implementation around default locales in POI")
- protected Locale initialValue() {
- return Locale.getDefault();
- }
- };
+ private static final ThreadLocal<TimeZone> userTimeZone = new ThreadLocal<TimeZone>();
+ private static final ThreadLocal<Locale> userLocale = new ThreadLocal<Locale>();
/**
* 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();
}
/**
|