Skip to content

Commit 290b2cc

Browse files
authored
Merge pull request #3496 from melissalinkert/tiff-closeable
Make TiffParser and TiffSaver implement Closeable
2 parents e49e93f + b8fec7c commit 290b2cc

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

components/formats-bsd/src/loci/formats/tiff/TiffParser.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package loci.formats.tiff;
3434

35+
import java.io.Closeable;
3536
import java.io.IOException;
3637
import java.util.ArrayList;
3738
import java.util.Arrays;
@@ -59,7 +60,7 @@
5960
* @author Melissa Linkert melissa at glencoesoftware.com
6061
* @author Chris Allan callan at blackcat.ca
6162
*/
62-
public class TiffParser {
63+
public class TiffParser implements Closeable {
6364

6465
// -- Constants --
6566

@@ -95,11 +96,14 @@ public class TiffParser {
9596
/** Codec options to be used when decoding compressed pixel data. */
9697
private CodecOptions codecOptions = CodecOptions.getDefaultOptions();
9798

99+
private boolean canClose = false;
100+
98101
// -- Constructors --
99102

100103
/** Constructs a new TIFF parser from the given file name. */
101104
public TiffParser(String filename) throws IOException {
102105
this(new RandomAccessInputStream(filename));
106+
canClose = true;
103107
}
104108

105109
/** Constructs a new TIFF parser from the given input source. */
@@ -114,6 +118,20 @@ public TiffParser(RandomAccessInputStream in) {
114118
catch (IOException e) { }
115119
}
116120

121+
// -- Closeable methods --
122+
123+
/**
124+
* Close the underlying stream, if this TiffParser was constructed
125+
* with a file path. Closing a stream that was pre-initialized
126+
* is potentially dangerous.
127+
*/
128+
@Override
129+
public void close() throws IOException {
130+
if (canClose && in != null) {
131+
in.close();
132+
}
133+
}
134+
117135
// -- TiffParser methods --
118136

119137
/**

components/formats-bsd/src/loci/formats/tiff/TiffSaver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package loci.formats.tiff;
3434

3535
import java.io.ByteArrayOutputStream;
36+
import java.io.Closeable;
3637
import java.io.DataOutputStream;
3738
import java.io.IOException;
3839
import java.nio.charset.Charset;
@@ -60,7 +61,7 @@
6061
* @author Melissa Linkert melissa at glencoesoftware.com
6162
* @author Chris Allan callan at blackcat.ca
6263
*/
63-
public class TiffSaver {
64+
public class TiffSaver implements Closeable {
6465

6566
// -- Constructor --
6667

0 commit comments

Comments
 (0)