Skip to content

Commit 6fb4b9d

Browse files
committed
avoid memory/resource leaks
1 parent c06637f commit 6fb4b9d

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/gov/nasa/worldwind/data/BasicRasterServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ protected void init(Object o)
140140
String message = Logging.getMessage("generic.DataSetLimitedAvailability", this.getDataSetName() );
141141
Logging.logger().severe(message);
142142
}
143+
144+
config.dispose();
143145
}
144146

145147
protected String getDataSetName()

src/gov/nasa/worldwind/data/RasterServerConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ public RasterServerConfiguration(Object docSource)
143143

144144
this.initialize();
145145
}
146+
147+
public void dispose() {
148+
try {
149+
eventReader.close();
150+
} catch (XMLStreamException e) {
151+
e.printStackTrace();
152+
}
153+
WWXML.closeEventReader(eventReader, "RasterServerConfiguration");
154+
freeResources();
155+
}
146156

147157
protected void initialize()
148158
{

src/gov/nasa/worldwind/util/WWXML.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class WWXML
3939
{
4040
public static final String XLINK_URI = "http://www.w3.org/1999/xlink";
4141

42+
private static Map<XMLEventReader, InputStream> inputSources = new HashMap<XMLEventReader, InputStream>();
43+
4244
/**
4345
* Create a DOM builder.
4446
*
@@ -170,9 +172,12 @@ public static Document openDocumentFile(String filePath, Class c)
170172
throw new IllegalArgumentException(message);
171173
}
172174

173-
InputStream inputStream = WWIO.openFileOrResourceStream(filePath, c);
174-
175-
return inputStream != null ? openDocumentStream(inputStream) : null;
175+
try (InputStream inputStream = WWIO.openFileOrResourceStream(filePath, c)) {
176+
return inputStream != null ? openDocumentStream(inputStream) : null;
177+
} catch (IOException e) {
178+
e.printStackTrace();
179+
return null;
180+
}
176181
}
177182

178183
/**
@@ -273,10 +278,7 @@ public static void saveDocumentToFile(Document doc, String filePath)
273278
throw new IllegalArgumentException(message);
274279
}
275280

276-
try
277-
{
278-
java.io.FileOutputStream outputStream = new java.io.FileOutputStream(filePath);
279-
281+
try (java.io.FileOutputStream outputStream = new java.io.FileOutputStream(filePath)) {
280282
saveDocumentToStream(doc, outputStream);
281283
}
282284
catch (IOException e)
@@ -356,7 +358,9 @@ public static XMLEventReader openEventReaderStream(InputStream inputStream, bool
356358

357359
try
358360
{
359-
return inputFactory.createXMLEventReader(inputStream);
361+
XMLEventReader reader = inputFactory.createXMLEventReader(inputStream);
362+
inputSources.put(reader, inputStream);
363+
return reader;
360364
}
361365
catch (XMLStreamException e)
362366
{
@@ -440,7 +444,9 @@ public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAwar
440444
try
441445
{
442446
InputStream inputStream = url.openStream();
443-
return openEventReaderStream(inputStream, isNamespaceAware);
447+
XMLEventReader reader = openEventReaderStream(inputStream, isNamespaceAware);
448+
inputSources.put(reader, inputStream);
449+
return reader;
444450
}
445451
catch (IOException e)
446452
{
@@ -531,6 +537,15 @@ public static void closeEventReader(XMLEventReader eventReader, String name)
531537
try
532538
{
533539
eventReader.close();
540+
InputStream is = inputSources.get(eventReader);
541+
if (is != null) {
542+
try {
543+
is.close();
544+
} catch (IOException e) {
545+
e.printStackTrace();
546+
}
547+
inputSources.remove(eventReader);
548+
}
534549
}
535550
catch (XMLStreamException e)
536551
{

0 commit comments

Comments
 (0)