diff --git a/src/main/java/net/openhft/chronicle/map/JsonSerializer.java b/src/main/java/net/openhft/chronicle/map/JsonSerializer.java index 36c070270..d0b84b359 100644 --- a/src/main/java/net/openhft/chronicle/map/JsonSerializer.java +++ b/src/main/java/net/openhft/chronicle/map/JsonSerializer.java @@ -31,8 +31,11 @@ import java.util.zip.GZIPOutputStream; /** * @author Rob Austin. */ -class JsonSerializer { - static final String logErrorSuggestXStreem = +final class JsonSerializer { + private JsonSerializer() { + } + + static final String LOG_ERROR_SUGGEST_X_STREAM = "map.getAll() and map.putAll() methods require the JSON XStream serializer, " + "we don't include these artifacts by default as some users don't require this functionality. " + "Please add the following artifacts to your project\n" + @@ -48,29 +51,37 @@ class JsonSerializer { "\n"; private static final Logger LOG = LoggerFactory.getLogger(JsonSerializer.class); - static synchronized void getAll(File toFile, Map map, List jsonConverters) throws IOException { + static synchronized void getAll(File toFile, Map map, List jsonConverters) throws IOException { final XStream xstream = xStream(map, jsonConverters); - OutputStream outputStream = new FileOutputStream(toFile); + + try (OutputStream outputStream = createOutputStream(toFile)) { + xstream.toXML(map, outputStream); + } + } + + static synchronized void putAll(File fromFile, Map map, List jsonConverters) throws IOException { + final XStream xstream = xStream(map, jsonConverters); + + try (InputStream inputStream = createInputStream(fromFile)) { + xstream.fromXML(inputStream); + } + } + + private static InputStream createInputStream(File toFile) throws IOException { if (toFile.getName().toLowerCase().endsWith(".gz")) - outputStream = new GZIPOutputStream(outputStream); - try (OutputStream out = outputStream) { - xstream.toXML(map, out); - } + return new GZIPInputStream(new FileInputStream(toFile)); + else + return new FileInputStream(toFile); } - static synchronized void putAll(File fromFile, Map map, List jsonConverters) - throws IOException { - final XStream xstream = xStream(map, jsonConverters); - - InputStream inputStream = new FileInputStream(fromFile); - if (fromFile.getName().toLowerCase().endsWith(".gz")) - inputStream = new GZIPInputStream(inputStream); - try (InputStream out = inputStream) { - xstream.fromXML(out); - } + private static OutputStream createOutputStream(File toFile) throws IOException { + if (toFile.getName().toLowerCase().endsWith(".gz")) + return new GZIPOutputStream(new FileOutputStream(toFile)); + else + return new FileOutputStream(toFile); } - private static XStream xStream(Map map, List jsonConverters) { + private static XStream xStream(Map map, List jsonConverters) { try { final XStream xstream = new XStream(new JettisonMappedXmlDriver()); xstream.setMode(XStream.NO_REFERENCES); @@ -94,12 +105,11 @@ class JsonSerializer { return xstream; } catch (NoClassDefFoundError e) { - throw new RuntimeException(logErrorSuggestXStreem, e); + throw new RuntimeException(LOG_ERROR_SUGGEST_X_STREAM, e); } } private static void registerChronicleMapConverter(Map map, XStream xstream) { xstream.registerConverter(new VanillaChronicleMapConverter<>(map)); } -} - +} \ No newline at end of file