Skip to content

Commit

Permalink
[GEOT-5419] Fix NetCDFImageReaderSpi file dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Oliveira committed May 16, 2016
1 parent ec4e4b8 commit fab2c13
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
Expand Up @@ -2,7 +2,7 @@
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2007-2015, Open Source Geospatial Foundation (OSGeo)
* (C) 2007-2016, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -21,6 +21,7 @@
import it.geosolutions.imageio.stream.input.URIImageInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -251,11 +252,15 @@ public boolean canDecodeInput(Object source) throws IOException {
return canDecode;
}

private boolean isNcML(File input) throws IOException {
final StreamSource streamSource = new StreamSource(input);
private boolean isNcML(File file) throws IOException {
FileInputStream input = null;
StreamSource streamSource = null;
XMLStreamReader reader = null;
try {
reader = XMLInputFactory.newInstance().createXMLStreamReader(streamSource);
input = new FileInputStream(file);
streamSource = new StreamSource(input);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
reader = inputFactory.createXMLStreamReader(streamSource);
reader.nextTag();
if ("netcdf".equals(reader.getName().getLocalPart())) {
return true;
Expand All @@ -265,6 +270,9 @@ private boolean isNcML(File input) throws IOException {
} catch (FactoryConfigurationError e) {

} finally {
if (input != null) {
input.close();
}
if (reader != null) {
if (streamSource.getInputStream() != null) {
streamSource.getInputStream().close();
Expand Down
@@ -0,0 +1,49 @@
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2016, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.imageio.netcdf;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.util.UUID;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

public final class NetCDFImageReaderSpiTest {

@Test
public void testFileIsProperlyClosed() throws Exception {
// create a temporary file and write some random bytes on it
Path path = Files.createTempFile(UUID.randomUUID().toString(), ".txt");
byte[] content = new byte[100];
Random random = new Random();
random.nextBytes(content);
Files.write(path, content);
// invoking the reader that will not be able to read the file
File file = path.toFile();
NetCDFImageReaderSpi reader = new NetCDFImageReaderSpi();
reader.canDecodeInput(file);
// check if the file as some lock on it
assertThat(file.delete(), is(true));
}
}

0 comments on commit fab2c13

Please sign in to comment.