Skip to content

Commit 811f204

Browse files
Modify NitfReading classes to support closing / try with resources
1 parent 4821fbb commit 811f204

7 files changed

Lines changed: 54 additions & 5 deletions

File tree

core-api/src/main/java/org/codice/imaging/nitf/core/common/NitfReader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,12 @@ This is used to ignore some of the file content (e.g. data segments, when only p
164164
@throws NitfFormatException if something went wrong during parsing (e.g. end of file).
165165
*/
166166
void skip(final long count) throws NitfFormatException;
167+
168+
/**
169+
Closes the file.
170+
171+
@throws Exception if there is an error closing the file.
172+
*/
173+
174+
void close() throws Exception;
167175
}

core/src/main/java/org/codice/imaging/nitf/core/common/impl/FileReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
NitfReader implementation using a (random access) File.
3333
*/
34-
public class FileReader extends SharedReader implements NitfReader {
34+
public class FileReader extends SharedReader implements NitfReader, AutoCloseable {
3535
// Error Messages
3636
static final String GENERIC_READ_ERROR_MESSAGE = "Error reading from NITF file: ";
3737
static final String FILE_NOT_FOUND_EXCEPTION_MESSAGE = "File Not Found Exception opening file:";

core/src/main/java/org/codice/imaging/nitf/core/common/impl/NitfInputStreamReader.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**
2626
NitfReader implementation using an InputStream.
2727
*/
28-
public class NitfInputStreamReader extends SharedReader implements NitfReader {
28+
public class NitfInputStreamReader extends SharedReader implements NitfReader, AutoCloseable {
2929

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

@@ -125,4 +125,11 @@ public final void skip(final long count) throws NitfFormatException {
125125
throw new NitfFormatException(GENERIC_READ_ERROR_MESSAGE + ex.getMessage(), numBytesRead);
126126
}
127127
}
128+
129+
@Override
130+
public final void close() throws Exception {
131+
if (input != null) {
132+
input.close();
133+
}
134+
}
128135
}

core/src/main/java/org/codice/imaging/nitf/core/common/impl/SharedReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
Shared NitfReader implementation.
2323
*/
24-
abstract class SharedReader extends NitfReaderDefaultImpl implements NitfReader {
24+
abstract class SharedReader extends NitfReaderDefaultImpl implements NitfReader, AutoCloseable {
2525

2626
@Override
2727
public final Integer readBytesAsInteger(final int count) throws NitfFormatException {

core/src/main/java/org/codice/imaging/nitf/core/impl/NitfReaderDefaultImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
combination of shared methods and default implementation methods that a concrete
2828
implementation class can call.
2929
*/
30-
public abstract class NitfReaderDefaultImpl implements NitfReader {
30+
public abstract class NitfReaderDefaultImpl implements NitfReader, AutoCloseable {
3131

3232
/**
3333
The type (version) of NITF file.

core/src/test/java/org/codice/imaging/nitf/core/common/impl/NitfInputStreamReaderTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,33 @@ public void testEndOfFileException() throws IOException, NitfFormatException {
5353
NitfReader reader = new NitfInputStreamReader(mockInputStream);
5454
reader.readBytes(100);
5555
}
56+
57+
@Test(expected = NitfFormatException.class)
58+
public void testIOExceptionReadingFromClosedStream() throws Exception {
59+
InputStream mockInputStream = new InputStream() {
60+
boolean closed = false;
61+
@Override public int read() throws IOException {
62+
throwIOExceptionIfClosed();
63+
return 100;
64+
}
65+
@Override public int read(byte[] buff, int offset, int len) throws IOException {
66+
throwIOExceptionIfClosed();
67+
return 100;
68+
}
69+
public void close() throws IOException {
70+
throwIOExceptionIfClosed();
71+
closed = true;
72+
}
73+
private void throwIOExceptionIfClosed() throws IOException {
74+
if (closed) {
75+
throw new IOException("Stream already closed");
76+
}
77+
}
78+
};
79+
80+
// IOException is rethrown as NitfFormatException
81+
NitfReader reader = new NitfInputStreamReader(mockInputStream);
82+
reader.close();
83+
reader.readBytes(100);
84+
}
5685
}

fluent/src/main/java/org/codice/imaging/nitf/fluent/impl/NitfParserParsingFlowImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
* A builder class that handles parsing.
4141
*/
42-
public class NitfParserParsingFlowImpl implements NitfParserParsingFlow {
42+
public class NitfParserParsingFlowImpl implements NitfParserParsingFlow, AutoCloseable {
4343
private final NitfReader reader;
4444

4545
private HeapStrategy<ImageInputStream> imageDataStrategy =
@@ -128,4 +128,9 @@ public final NitfSegmentsFlow build(final ParseStrategy parseStrategy)
128128
NitfParser.parse(reader, parseStrategy);
129129
return new NitfSegmentsFlowImpl(parseStrategy.getDataSource(), imageDataStrategy::cleanUp);
130130
}
131+
132+
@Override
133+
public final void close() throws Exception {
134+
this.reader.close();
135+
}
131136
}

0 commit comments

Comments
 (0)