Skip to content

Commit

Permalink
Make DateFormatter threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
bpellin committed Nov 22, 2017
1 parent 2dae805 commit 924db24
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions app/src/main/java/com/keepassdroid/database/PwDatabaseV4XML.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2013 Brian Pellin.
* Copyright 2009-2017 Brian Pellin.
*
* This file is part of KeePassDroid.
*
Expand All @@ -23,16 +23,8 @@
import java.text.SimpleDateFormat;
import java.util.TimeZone;

@SuppressLint("SimpleDateFormat")
public class PwDatabaseV4XML {

public static final SimpleDateFormat dateFormat;

static {
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}

public static final String ElemDocNode = "KeePassFile";
public static final String ElemMeta = "Meta";
public static final String ElemRoot = "Root";
Expand Down Expand Up @@ -134,4 +126,15 @@ public class PwDatabaseV4XML {

public static final String ElemCustomData = "CustomData";
public static final String ElemStringDictExItem = "Item";

public static final ThreadLocal<SimpleDateFormat> dateFormatter =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
SimpleDateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ private Date ReadTime(XmlPullParser xpp) throws IOException, XmlPullParserExcept
} else {

try {
utcDate = PwDatabaseV4XML.dateFormat.parse(sDate);
utcDate = PwDatabaseV4XML.dateFormatter.get().parse(sDate);
} catch (ParseException e) {
// Catch with null test below
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ private void writeObject(String name, String value) throws IllegalArgumentExcept

private void writeObject(String name, Date value) throws IllegalArgumentException, IllegalStateException, IOException {
if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) {
writeObject(name, PwDatabaseV4XML.dateFormat.format(value));
writeObject(name, PwDatabaseV4XML.dateFormatter.get().format(value));
} else {
DateTime dt = new DateTime(value);
long seconds = DateUtil.convertDateToKDBX4Time(dt);
Expand Down

0 comments on commit 924db24

Please sign in to comment.