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
This commit is contained in:
Javen O'Neal 2016-07-07 01:10:24 +00:00
parent 5cbd7552eb
commit b45a65e667
1 changed files with 20 additions and 26 deletions

View File

@ -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,17 +90,19 @@ public final class LocaleUtil {
userLocale.set(locale);
}
/**
* @return the default user locale. If not set, returns {@link Locale#getDefault()}.
*/
@SuppressForbidden("implementation around default locales in POI")
public static Locale getUserLocale() {
Locale locale = userLocale.get();
return (locale != null) ? locale : Locale.getDefault();
}
public static void resetUserLocale() {
userLocale.remove();
}
/**
* @return the default user locale, defaults to {@link Locale#ROOT}
*/
public static Locale getUserLocale() {
return userLocale.get();
}
/**
* @return a calendar for the user locale and time zone
*/