Browse Source

Make ReverseFileReader autocloseable.

Because why not?

Change-Id: I996ff53c4bb7bd0d237691c260ed7dc55e6499fa
Reviewed-on: http://gerrit.dmdirc.com/4048
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/48/4048/2
Chris Smith 9 years ago
parent
commit
f6b56531d3

+ 4
- 10
src/com/dmdirc/util/io/ReverseFileReader.java View File

36
 /**
36
 /**
37
  * Reads a file in reverse.
37
  * Reads a file in reverse.
38
  */
38
  */
39
-public class ReverseFileReader {
39
+public class ReverseFileReader implements AutoCloseable {
40
 
40
 
41
     /** Path to the file we're reading. */
41
     /** Path to the file we're reading. */
42
     private final Path file;
42
     private final Path file;
91
         seekLength = newValue;
91
         seekLength = newValue;
92
     }
92
     }
93
 
93
 
94
-    /**
95
-     * Close the file pointer.
96
-     * This should be called before removing the reference to this object
97
-     *
98
-     * @throws IOException If there is an error closing the file, or if it has been closed already.
99
-     */
94
+    @Override
100
     public void close() throws IOException {
95
     public void close() throws IOException {
101
-        if (!byteChannel.isOpen()) {
102
-            throw new IOException("Channel has been closed.");
96
+        if (byteChannel.isOpen()) {
97
+            byteChannel.close();
103
         }
98
         }
104
-        byteChannel.close();
105
     }
99
     }
106
 
100
 
107
     /**
101
     /**

+ 1
- 2
test/com/dmdirc/util/io/ReverseFileReaderTest.java View File

136
         reader.close();
136
         reader.close();
137
     }
137
     }
138
 
138
 
139
-    @Test(expected=IOException.class)
140
-    public void testIllegalClose() throws URISyntaxException, IOException {
139
+    public void testCloseIsIdempotent() throws URISyntaxException, IOException {
141
         final ReverseFileReader reader = new ReverseFileReader(
140
         final ReverseFileReader reader = new ReverseFileReader(
142
                 Paths.get((getClass().getResource("test1.txt").toURI())));
141
                 Paths.get((getClass().getResource("test1.txt").toURI())));
143
         reader.close();
142
         reader.close();

Loading…
Cancel
Save