From 1d2d9e8630a358b2bb46527a5e8d2d4f21db6a5a Mon Sep 17 00:00:00 2001 From: Denny Date: Thu, 21 Dec 2017 13:42:03 -0500 Subject: [PATCH] Updates IconManager to use faster jackson XML serialization. --- .../io/github/dsheirer/icon/IconManager.java | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/github/dsheirer/icon/IconManager.java b/src/main/java/io/github/dsheirer/icon/IconManager.java index 66e2dbe4..a5f302df 100644 --- a/src/main/java/io/github/dsheirer/icon/IconManager.java +++ b/src/main/java/io/github/dsheirer/icon/IconManager.java @@ -16,6 +16,10 @@ */ package io.github.dsheirer.icon; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; import io.github.dsheirer.properties.SystemProperties; import io.github.dsheirer.util.ThreadPool; import org.slf4j.Logger; @@ -24,10 +28,6 @@ import org.slf4j.LoggerFactory; import javax.swing.*; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; import java.awt.*; import java.io.IOException; import java.io.InputStream; @@ -243,8 +243,6 @@ public class IconManager { IconSet iconSet = getModel().getIconSet(); - JAXBContext context = null; - //Create a backup copy of the current playlist if(Files.exists(getIconFilePath())) { @@ -275,14 +273,11 @@ public class IconManager try(OutputStream out = Files.newOutputStream(getIconFilePath())) { - context = JAXBContext.newInstance(IconSet.class); - - Marshaller m = context.createMarshaller(); - - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - - m.marshal(iconSet, out); - + JacksonXmlModule xmlModule = new JacksonXmlModule(); + xmlModule.setDefaultUseWrapper(false); + ObjectMapper objectMapper = new XmlMapper(xmlModule); + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + objectMapper.writeValue(out, iconSet); out.flush(); //Remove the playlist lock file to indicate that we successfully saved the file @@ -291,10 +286,6 @@ public class IconManager Files.delete(getIconLockFilePath()); } } - catch(JAXBException je) - { - mLog.error("JAXB exception while serializing icons to a file [" + getIconFilePath().toString() + "]", je); - } catch(IOException ioe) { mLog.error("IO error while writing icons to a file [" + getIconFilePath().toString() + "]", ioe); @@ -340,19 +331,13 @@ public class IconManager if(Files.exists(getIconFilePath())) { - JAXBContext context = null; + JacksonXmlModule xmlModule = new JacksonXmlModule(); + xmlModule.setDefaultUseWrapper(false); + ObjectMapper objectMapper = new XmlMapper(xmlModule); try(InputStream in = Files.newInputStream(getIconFilePath())) { - context = JAXBContext.newInstance(IconSet.class); - - Unmarshaller m = context.createUnmarshaller(); - - iconSet = (IconSet) m.unmarshal(in); - } - catch(JAXBException je) - { - mLog.error("JAXB exception while loading/unmarshalling icons", je); + iconSet = objectMapper.readValue(in, IconSet.class); } catch(IOException ioe) {