Updates IconManager to use faster jackson XML serialization.

This commit is contained in:
Denny
2017-12-21 13:42:03 -05:00
parent ee6aeafa84
commit 1d2d9e8630

View File

@@ -16,6 +16,10 @@
*/ */
package io.github.dsheirer.icon; 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.properties.SystemProperties;
import io.github.dsheirer.util.ThreadPool; import io.github.dsheirer.util.ThreadPool;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -24,10 +28,6 @@ import org.slf4j.LoggerFactory;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener; 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.awt.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -243,8 +243,6 @@ public class IconManager
{ {
IconSet iconSet = getModel().getIconSet(); IconSet iconSet = getModel().getIconSet();
JAXBContext context = null;
//Create a backup copy of the current playlist //Create a backup copy of the current playlist
if(Files.exists(getIconFilePath())) if(Files.exists(getIconFilePath()))
{ {
@@ -275,14 +273,11 @@ public class IconManager
try(OutputStream out = Files.newOutputStream(getIconFilePath())) try(OutputStream out = Files.newOutputStream(getIconFilePath()))
{ {
context = JAXBContext.newInstance(IconSet.class); JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
Marshaller m = context.createMarshaller(); ObjectMapper objectMapper = new XmlMapper(xmlModule);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); objectMapper.writeValue(out, iconSet);
m.marshal(iconSet, out);
out.flush(); out.flush();
//Remove the playlist lock file to indicate that we successfully saved the file //Remove the playlist lock file to indicate that we successfully saved the file
@@ -291,10 +286,6 @@ public class IconManager
Files.delete(getIconLockFilePath()); Files.delete(getIconLockFilePath());
} }
} }
catch(JAXBException je)
{
mLog.error("JAXB exception while serializing icons to a file [" + getIconFilePath().toString() + "]", je);
}
catch(IOException ioe) catch(IOException ioe)
{ {
mLog.error("IO error while writing icons to a file [" + getIconFilePath().toString() + "]", 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())) 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())) try(InputStream in = Files.newInputStream(getIconFilePath()))
{ {
context = JAXBContext.newInstance(IconSet.class); iconSet = objectMapper.readValue(in, IconSet.class);
Unmarshaller m = context.createUnmarshaller();
iconSet = (IconSet) m.unmarshal(in);
}
catch(JAXBException je)
{
mLog.error("JAXB exception while loading/unmarshalling icons", je);
} }
catch(IOException ioe) catch(IOException ioe)
{ {