mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Fix false warning about resource leak in JsonSerializer, Fix #245
This commit is contained in:
@@ -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(<file>) and map.putAll(<file>) 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 {
|
||||
"</dependency>\n";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JsonSerializer.class);
|
||||
|
||||
static synchronized <K, V> void getAll(File toFile, Map<K, V> map, List jsonConverters) throws IOException {
|
||||
static synchronized <K, V> void getAll(File toFile, Map<K, V> 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 <K, V> void putAll(File fromFile, Map<K, V> 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 <K, V> void putAll(File fromFile, Map<K, V> 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 <K, V> XStream xStream(Map<K, V> map, List jsonConverters) {
|
||||
private static <K, V> XStream xStream(Map<K, V> 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 <K, V> void registerChronicleMapConverter(Map<K, V> map, XStream xstream) {
|
||||
xstream.registerConverter(new VanillaChronicleMapConverter<>(map));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user