Skip to content

Commit

Permalink
Modify NitfReading classes to support closing / try with resources
Browse files Browse the repository at this point in the history
  • Loading branch information
svargasConnexta committed Jan 7, 2019
1 parent 4821fbb commit 811f204
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,12 @@ This is used to ignore some of the file content (e.g. data segments, when only p
@throws NitfFormatException if something went wrong during parsing (e.g. end of file).
*/
void skip(final long count) throws NitfFormatException;

/**
Closes the file.
@throws Exception if there is an error closing the file.
*/

void close() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
NitfReader implementation using a (random access) File.
*/
public class FileReader extends SharedReader implements NitfReader {
public class FileReader extends SharedReader implements NitfReader, AutoCloseable {
// Error Messages
static final String GENERIC_READ_ERROR_MESSAGE = "Error reading from NITF file: ";
static final String FILE_NOT_FOUND_EXCEPTION_MESSAGE = "File Not Found Exception opening file:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
NitfReader implementation using an InputStream.
*/
public class NitfInputStreamReader extends SharedReader implements NitfReader {
public class NitfInputStreamReader extends SharedReader implements NitfReader, AutoCloseable {

private static final Logger LOG = LoggerFactory.getLogger(NitfInputStreamReader.class);

Expand Down Expand Up @@ -125,4 +125,11 @@ public final void skip(final long count) throws NitfFormatException {
throw new NitfFormatException(GENERIC_READ_ERROR_MESSAGE + ex.getMessage(), numBytesRead);
}
}

@Override
public final void close() throws Exception {
if (input != null) {
input.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
Shared NitfReader implementation.
*/
abstract class SharedReader extends NitfReaderDefaultImpl implements NitfReader {
abstract class SharedReader extends NitfReaderDefaultImpl implements NitfReader, AutoCloseable {

@Override
public final Integer readBytesAsInteger(final int count) throws NitfFormatException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
combination of shared methods and default implementation methods that a concrete
implementation class can call.
*/
public abstract class NitfReaderDefaultImpl implements NitfReader {
public abstract class NitfReaderDefaultImpl implements NitfReader, AutoCloseable {

/**
The type (version) of NITF file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,33 @@ public void testEndOfFileException() throws IOException, NitfFormatException {
NitfReader reader = new NitfInputStreamReader(mockInputStream);
reader.readBytes(100);
}

@Test(expected = NitfFormatException.class)
public void testIOExceptionReadingFromClosedStream() throws Exception {
InputStream mockInputStream = new InputStream() {
boolean closed = false;
@Override public int read() throws IOException {
throwIOExceptionIfClosed();
return 100;
}
@Override public int read(byte[] buff, int offset, int len) throws IOException {
throwIOExceptionIfClosed();
return 100;
}
public void close() throws IOException {
throwIOExceptionIfClosed();
closed = true;
}
private void throwIOExceptionIfClosed() throws IOException {
if (closed) {
throw new IOException("Stream already closed");
}
}
};

// IOException is rethrown as NitfFormatException
NitfReader reader = new NitfInputStreamReader(mockInputStream);
reader.close();
reader.readBytes(100);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/**
* A builder class that handles parsing.
*/
public class NitfParserParsingFlowImpl implements NitfParserParsingFlow {
public class NitfParserParsingFlowImpl implements NitfParserParsingFlow, AutoCloseable {
private final NitfReader reader;

private HeapStrategy<ImageInputStream> imageDataStrategy =
Expand Down Expand Up @@ -128,4 +128,9 @@ public final NitfSegmentsFlow build(final ParseStrategy parseStrategy)
NitfParser.parse(reader, parseStrategy);
return new NitfSegmentsFlowImpl(parseStrategy.getDataSource(), imageDataStrategy::cleanUp);
}

@Override
public final void close() throws Exception {
this.reader.close();
}
}

0 comments on commit 811f204

Please sign in to comment.