screenshot plugin: fix unsafe multithreaded usage of DateFormat

This commit is contained in:
Adam
2018-06-16 13:30:13 -04:00
parent e6cd1fedf0
commit 9075b44320
2 changed files with 14 additions and 6 deletions

View File

@@ -28,7 +28,7 @@ import java.awt.event.KeyEvent;
import java.util.Date; import java.util.Date;
import javax.inject.Inject; import javax.inject.Inject;
import net.runelite.client.input.KeyListener; import net.runelite.client.input.KeyListener;
import static net.runelite.client.plugins.screenshot.ScreenshotPlugin.TIME_FORMAT; import static net.runelite.client.plugins.screenshot.ScreenshotPlugin.format;
public class ScreenshotInput implements KeyListener public class ScreenshotInput implements KeyListener
{ {
@@ -60,7 +60,7 @@ public class ScreenshotInput implements KeyListener
if (event.getKeyCode() == KeyEvent.VK_INSERT) if (event.getKeyCode() == KeyEvent.VK_INSERT)
{ {
plugin.takeScreenshot(TIME_FORMAT.format(new Date())); plugin.takeScreenshot(format(new Date()));
} }
} }

View File

@@ -102,7 +102,7 @@ public class ScreenshotPlugin extends Plugin
private static final HttpUrl IMGUR_IMAGE_UPLOAD_URL = HttpUrl.parse("https://api.imgur.com/3/image"); private static final HttpUrl IMGUR_IMAGE_UPLOAD_URL = HttpUrl.parse("https://api.imgur.com/3/image");
private static final MediaType JSON = MediaType.parse("application/json"); private static final MediaType JSON = MediaType.parse("application/json");
static final DateFormat TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); private static final DateFormat TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
private static final Pattern NUMBER_PATTERN = Pattern.compile("([0-9]+)"); private static final Pattern NUMBER_PATTERN = Pattern.compile("([0-9]+)");
private static final Pattern LEVEL_UP_PATTERN = Pattern.compile("Your ([a-zA-Z]+) (?:level is|are)? now (\\d+)\\."); private static final Pattern LEVEL_UP_PATTERN = Pattern.compile("Your ([a-zA-Z]+) (?:level is|are)? now (\\d+)\\.");
@@ -116,6 +116,14 @@ public class ScreenshotPlugin extends Plugin
"Can anyone defeat you? Certainly", "was no match for you", "You were clearly a better fighter than", "RIP", "Can anyone defeat you? Certainly", "was no match for you", "You were clearly a better fighter than", "RIP",
"You have defeated", "What an embarrassing performance by", "was no match for your awesomeness"); "You have defeated", "What an embarrassing performance by", "was no match for your awesomeness");
static String format(Date date)
{
synchronized (TIME_FORMAT)
{
return TIME_FORMAT.format(date);
}
}
private String clueType; private String clueType;
private Integer clueNumber; private Integer clueNumber;
@@ -182,7 +190,7 @@ public class ScreenshotPlugin extends Plugin
titleBarButton = NavigationButton.builder() titleBarButton = NavigationButton.builder()
.tooltip("Take screenshot") .tooltip("Take screenshot")
.icon(iconImage) .icon(iconImage)
.onClick(() -> takeScreenshot(TIME_FORMAT.format(new Date()))) .onClick(() -> takeScreenshot(format(new Date())))
.popup(ImmutableMap .popup(ImmutableMap
.<String, Runnable>builder() .<String, Runnable>builder()
.put("Open screenshot folder...", () -> .put("Open screenshot folder...", () ->
@@ -259,13 +267,13 @@ public class ScreenshotPlugin extends Plugin
if (config.screenshotPet() && PET_MESSAGES.stream().anyMatch(chatMessage::contains)) if (config.screenshotPet() && PET_MESSAGES.stream().anyMatch(chatMessage::contains))
{ {
String fileName = "Pet " + TIME_FORMAT.format(new Date()); String fileName = "Pet " + format(new Date());
takeScreenshot(fileName); takeScreenshot(fileName);
} }
if (config.screenshotKills() && KILL_MESSAGES.stream().anyMatch(chatMessage::contains)) if (config.screenshotKills() && KILL_MESSAGES.stream().anyMatch(chatMessage::contains))
{ {
String fileName = "Kill " + TIME_FORMAT.format(new Date()); String fileName = "Kill " + format(new Date());
takeScreenshot(fileName); takeScreenshot(fileName);
} }
} }