[FLINK-4612] Close FileWriter using try with resources

This closes #2492.


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/f06930bc
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/f06930bc
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/f06930bc

Branch: refs/heads/master
Commit: f06930bcf0d3c2a840cdc5a2e6e5f4b1d03f45d2
Parents: 1586fc8
Author: Alexander Pivovarov <apivova...@gmail.com>
Authored: Sun Sep 11 22:46:55 2016 -0700
Committer: zentol <ches...@apache.org>
Committed: Fri Sep 16 12:34:16 2016 +0200

----------------------------------------------------------------------
 .../apache/flink/testutils/TestFileUtils.java   | 33 +++++-----------
 .../relational/util/WebLogDataGenerator.java    | 21 ++--------
 .../flink/api/java/io/CsvInputFormatTest.java   |  6 +--
 .../api/java/io/PrimitiveInputFormatTest.java   |  8 ++--
 .../runtime/io/disk/FileChannelStreamsTest.java |  6 +--
 .../runtime/operators/DataSourceTaskTest.java   | 40 ++++++++++----------
 .../cassandra/CassandraConnectorITCase.java     | 23 ++++++-----
 .../api/functions/sink/WriteFormatAsCsv.java    |  4 +-
 .../api/functions/sink/WriteFormatAsText.java   |  4 +-
 .../clients/examples/LocalExecutorITCase.java   |  6 +--
 .../aggregators/AggregatorsITCase.java          |  6 +--
 .../org/apache/flink/yarn/YarnTestBase.java     |  8 ++--
 12 files changed, 66 insertions(+), 99 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-core/src/test/java/org/apache/flink/testutils/TestFileUtils.java
----------------------------------------------------------------------
diff --git 
a/flink-core/src/test/java/org/apache/flink/testutils/TestFileUtils.java 
b/flink-core/src/test/java/org/apache/flink/testutils/TestFileUtils.java
index 683bc4d..ebe68f0 100644
--- a/flink-core/src/test/java/org/apache/flink/testutils/TestFileUtils.java
+++ b/flink-core/src/test/java/org/apache/flink/testutils/TestFileUtils.java
@@ -54,12 +54,9 @@ public class TestFileUtils {
                f.getParentFile().mkdirs();
                f.createNewFile();
                f.deleteOnExit();
-               
-               BufferedWriter out = new BufferedWriter(new FileWriter(f));
-               try { 
+
+               try (BufferedWriter out = new BufferedWriter(new 
FileWriter(f))) {
                        out.write(contents);
-               } finally {
-                       out.close();
                }
                return f.toURI().toString();
        }
@@ -73,13 +70,10 @@ public class TestFileUtils {
                f.createNewFile();
                f.deleteOnExit();
 
-               BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(f));
-               try {
+               try (BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(f))) {
                        for (; bytes > 0; bytes--) {
                                out.write(0);
                        }
-               } finally {
-                       out.close();
                }
                return f.toURI().toString();
        }
@@ -87,12 +81,9 @@ public class TestFileUtils {
        public static String createTempFile(String contents) throws IOException 
{
                File f = File.createTempFile(FILE_PREFIX, FILE_SUFFIX);
                f.deleteOnExit();
-               
-               BufferedWriter out = new BufferedWriter(new FileWriter(f));
-               try { 
+
+               try (BufferedWriter out = new BufferedWriter(new 
FileWriter(f))) {
                        out.write(contents);
-               } finally {
-                       out.close();
                }
                return f.toURI().toString();
        }
@@ -111,14 +102,11 @@ public class TestFileUtils {
                for (long l : bytes) {
                        File child = new File(f, randomFileName());
                        child.deleteOnExit();
-               
-                       BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(child));
-                       try { 
+
+                       try (BufferedOutputStream out = new 
BufferedOutputStream(new FileOutputStream(child))) {
                                for (; l > 0; l--) {
                                        out.write(0);
                                }
-                       } finally {
-                               out.close();
                        }
                }
                return f.toURI().toString();
@@ -140,12 +128,9 @@ public class TestFileUtils {
                for (String s : contents) {
                        File child = new File(f, randomFileName(fileExtension));
                        child.deleteOnExit();
-               
-                       BufferedWriter out = new BufferedWriter(new 
FileWriter(child));
-                       try { 
+
+                       try (BufferedWriter out = new BufferedWriter(new 
FileWriter(child))) {
                                out.write(s);
-                       } finally {
-                               out.close();
                        }
                }
                return f.toURI().toString();

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-examples/flink-examples-batch/src/main/java/org/apache/flink/examples/java/relational/util/WebLogDataGenerator.java
----------------------------------------------------------------------
diff --git 
a/flink-examples/flink-examples-batch/src/main/java/org/apache/flink/examples/java/relational/util/WebLogDataGenerator.java
 
b/flink-examples/flink-examples-batch/src/main/java/org/apache/flink/examples/java/relational/util/WebLogDataGenerator.java
index 8efa243..e8dbe25 100644
--- 
a/flink-examples/flink-examples-batch/src/main/java/org/apache/flink/examples/java/relational/util/WebLogDataGenerator.java
+++ 
b/flink-examples/flink-examples-batch/src/main/java/org/apache/flink/examples/java/relational/util/WebLogDataGenerator.java
@@ -99,9 +99,7 @@ public class WebLogDataGenerator {
 
                Random rand = new 
Random(Calendar.getInstance().getTimeInMillis());
 
-               try {
-                       FileWriter fw = new FileWriter(path);
-
+               try (FileWriter fw = new FileWriter(path)) {
                        for (int i = 0; i < noDocs; i++) {
 
                                int wordsInDoc = rand.nextInt(40) + 10;
@@ -110,8 +108,7 @@ public class WebLogDataGenerator {
                                for (int j = 0; j < wordsInDoc; j++) {
                                        if (rand.nextDouble() > 0.9) {
                                                // Approx. every 10th word is a 
keyword
-                                               doc.append(filterKeyWords[rand
-                                                               
.nextInt(filterKeyWords.length)] + " ");
+                                               
doc.append(filterKeyWords[rand.nextInt(filterKeyWords.length)] + " ");
                                        } else {
                                                // Fills up the docs file(s) 
with random words
                                                
doc.append(words[rand.nextInt(words.length)] + " ");
@@ -121,8 +118,6 @@ public class WebLogDataGenerator {
 
                                fw.write(doc.toString());
                        }
-                       fw.close();
-
                } catch (IOException e) {
                        e.printStackTrace();
                }
@@ -142,9 +137,7 @@ public class WebLogDataGenerator {
 
                Random rand = new 
Random(Calendar.getInstance().getTimeInMillis());
 
-               try {
-                       FileWriter fw = new FileWriter(path);
-
+               try (FileWriter fw = new FileWriter(path)) {
                        for (int i = 0; i < noDocs; i++) {
                                // Rank
                                StringBuilder rank = new 
StringBuilder(rand.nextInt(100) + "|");
@@ -155,8 +148,6 @@ public class WebLogDataGenerator {
 
                                fw.write(rank.toString());
                        }
-                       fw.close();
-
                } catch (IOException e) {
                        e.printStackTrace();
                }
@@ -178,9 +169,7 @@ public class WebLogDataGenerator {
 
                Random rand = new 
Random(Calendar.getInstance().getTimeInMillis());
 
-               try {
-                       FileWriter fw = new FileWriter(path);
-
+               try (FileWriter fw = new FileWriter(path)) {
                        for (int i = 0; i < noVisits; i++) {
 
                                int year = 2000 + rand.nextInt(10); // 
yearFilter 3
@@ -200,8 +189,6 @@ public class WebLogDataGenerator {
 
                                fw.write(visit.toString());
                        }
-                       fw.close();
-
                } catch (IOException e) {
                        e.printStackTrace();
                }

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-java/src/test/java/org/apache/flink/api/java/io/CsvInputFormatTest.java
----------------------------------------------------------------------
diff --git 
a/flink-java/src/test/java/org/apache/flink/api/java/io/CsvInputFormatTest.java 
b/flink-java/src/test/java/org/apache/flink/api/java/io/CsvInputFormatTest.java
index ecf55c3..fa091d9 100644
--- 
a/flink-java/src/test/java/org/apache/flink/api/java/io/CsvInputFormatTest.java
+++ 
b/flink-java/src/test/java/org/apache/flink/api/java/io/CsvInputFormatTest.java
@@ -78,9 +78,9 @@ public class CsvInputFormatTest {
                final File tempFile = 
File.createTempFile("input-stream-decoration-test", "tmp");
                tempFile.deleteOnExit();
 
-               FileOutputStream fileOutputStream = new 
FileOutputStream(tempFile);
-               fileOutputStream.write(fileContent.getBytes());
-               fileOutputStream.close();
+               try (FileOutputStream fileOutputStream = new 
FileOutputStream(tempFile)) {
+                       fileOutputStream.write(fileContent.getBytes());
+               }
 
                // fix the number of blocks and the size of each one.
                final int noOfBlocks = 3;

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-java/src/test/java/org/apache/flink/api/java/io/PrimitiveInputFormatTest.java
----------------------------------------------------------------------
diff --git 
a/flink-java/src/test/java/org/apache/flink/api/java/io/PrimitiveInputFormatTest.java
 
b/flink-java/src/test/java/org/apache/flink/api/java/io/PrimitiveInputFormatTest.java
index 4a92702..f9dc28a 100644
--- 
a/flink-java/src/test/java/org/apache/flink/api/java/io/PrimitiveInputFormatTest.java
+++ 
b/flink-java/src/test/java/org/apache/flink/api/java/io/PrimitiveInputFormatTest.java
@@ -179,11 +179,11 @@ public class PrimitiveInputFormatTest {
                File tempFile = File.createTempFile("test_contents", "tmp");
                tempFile.deleteOnExit();
 
-               FileWriter wrt = new FileWriter(tempFile);
-               wrt.write(content);
-               wrt.close();
+               try (FileWriter wrt = new FileWriter(tempFile)) {
+                       wrt.write(content);
+               }
 
                return new FileInputSplit(0, new 
Path(tempFile.toURI().toString()), 0, tempFile.length(), new String[] 
{"localhost"});
        }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/FileChannelStreamsTest.java
----------------------------------------------------------------------
diff --git 
a/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/FileChannelStreamsTest.java
 
b/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/FileChannelStreamsTest.java
index 1c2b3de..1044a35 100644
--- 
a/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/FileChannelStreamsTest.java
+++ 
b/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/FileChannelStreamsTest.java
@@ -85,10 +85,8 @@ public class FileChannelStreamsTest {
                        FileIOChannel.ID channel = ioManager.createChannel();
                        
                        // add some test data
-                       {
-                               FileWriter wrt = new 
FileWriter(channel.getPath());
+                       try (FileWriter wrt = new 
FileWriter(channel.getPath())) {
                                wrt.write("test data");
-                               wrt.close();
                        }
                        
                        BlockChannelReader<MemorySegment> reader = 
ioManager.createBlockChannelReader(channel);
@@ -116,4 +114,4 @@ public class FileChannelStreamsTest {
                        ioManager.shutdown();
                }
        }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-runtime/src/test/java/org/apache/flink/runtime/operators/DataSourceTaskTest.java
----------------------------------------------------------------------
diff --git 
a/flink-runtime/src/test/java/org/apache/flink/runtime/operators/DataSourceTaskTest.java
 
b/flink-runtime/src/test/java/org/apache/flink/runtime/operators/DataSourceTaskTest.java
index 95f2991..0388c2b 100644
--- 
a/flink-runtime/src/test/java/org/apache/flink/runtime/operators/DataSourceTaskTest.java
+++ 
b/flink-runtime/src/test/java/org/apache/flink/runtime/operators/DataSourceTaskTest.java
@@ -221,28 +221,26 @@ public class DataSourceTaskTest extends TaskTestBase {
        
        private static class InputFilePreparator {
                public static void 
prepareInputFile(MutableObjectIterator<Record> inIt, String inputFilePath, 
boolean insertInvalidData)
-               throws IOException
-               {
-                       FileWriter fw = new FileWriter(inputFilePath);
-                       BufferedWriter bw = new BufferedWriter(fw);
-                       
-                       if (insertInvalidData) {
-                               bw.write("####_I_AM_INVALID_########\n");
-                       }
-                       
-                       Record rec = new Record();
-                       while ((rec = inIt.next(rec)) != null) {
-                               IntValue key = rec.getField(0, IntValue.class);
-                               IntValue value = rec.getField(1, 
IntValue.class);
-                               
-                               bw.write(key.getValue() + "_" + 
value.getValue() + "\n");
-                       }
-                       if (insertInvalidData) {
-                               bw.write("####_I_AM_INVALID_########\n");
+               throws IOException {
+
+                       try (BufferedWriter bw = new BufferedWriter(new 
FileWriter(inputFilePath))) {
+                               if (insertInvalidData) {
+                                       
bw.write("####_I_AM_INVALID_########\n");
+                               }
+
+                               Record rec = new Record();
+                               while ((rec = inIt.next(rec)) != null) {
+                                       IntValue key = rec.getField(0, 
IntValue.class);
+                                       IntValue value = rec.getField(1, 
IntValue.class);
+
+                                       bw.write(key.getValue() + "_" + 
value.getValue() + "\n");
+                               }
+                               if (insertInvalidData) {
+                                       
bw.write("####_I_AM_INVALID_########\n");
+                               }
+
+                               bw.flush();
                        }
-                       
-                       bw.flush();
-                       bw.close();
                }
        }
        

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-streaming-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/streaming/connectors/cassandra/CassandraConnectorITCase.java
----------------------------------------------------------------------
diff --git 
a/flink-streaming-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/streaming/connectors/cassandra/CassandraConnectorITCase.java
 
b/flink-streaming-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/streaming/connectors/cassandra/CassandraConnectorITCase.java
index f94ff68..cc4a527 100644
--- 
a/flink-streaming-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/streaming/connectors/cassandra/CassandraConnectorITCase.java
+++ 
b/flink-streaming-connectors/flink-connector-cassandra/src/test/java/org/apache/flink/streaming/connectors/cassandra/CassandraConnectorITCase.java
@@ -166,17 +166,20 @@ public class CassandraConnectorITCase extends 
WriteAheadSinkTestBase<Tuple3<Stri
                File tmp = new File(tmpDir.getAbsolutePath() + File.separator + 
"cassandra.yaml");
                
                assertTrue(tmp.createNewFile());
-               BufferedWriter b = new BufferedWriter(new FileWriter(tmp));
-
-               //copy cassandra.yaml; inject absolute paths into cassandra.yaml
-               Scanner scanner = new Scanner(file);
-               while (scanner.hasNextLine()) {
-                       String line = scanner.nextLine();
-                       line = line.replace("$PATH", "'" + tmp.getParentFile());
-                       b.write(line + "\n");
-                       b.flush();
+
+               try (
+                       BufferedWriter b = new BufferedWriter(new 
FileWriter(tmp));
+
+                       //copy cassandra.yaml; inject absolute paths into 
cassandra.yaml
+                       Scanner scanner = new Scanner(file);
+               ) {
+                       while (scanner.hasNextLine()) {
+                               String line = scanner.nextLine();
+                               line = line.replace("$PATH", "'" + 
tmp.getParentFile());
+                               b.write(line + "\n");
+                               b.flush();
+                       }
                }
-               scanner.close();
 
 
                // Tell cassandra where the configuration files are.

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsCsv.java
----------------------------------------------------------------------
diff --git 
a/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsCsv.java
 
b/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsCsv.java
index 22f304a..da03859 100644
--- 
a/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsCsv.java
+++ 
b/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsCsv.java
@@ -37,13 +37,11 @@ public class WriteFormatAsCsv<IN> extends WriteFormat<IN> {
 
        @Override
        protected void write(String path, ArrayList<IN> tupleList) {
-               try {
-                       PrintWriter outStream = new PrintWriter(new 
BufferedWriter(new FileWriter(path, true)));
+               try (PrintWriter outStream = new PrintWriter(new 
BufferedWriter(new FileWriter(path, true)))) {
                        for (IN tupleToWrite : tupleList) {
                                String strTuple = tupleToWrite.toString();
                                outStream.println(strTuple.substring(1, 
strTuple.length() - 1));
                        }
-                       outStream.close();
                } catch (IOException e) {
                        throw new RuntimeException("Exception occured while 
writing file " + path, e);
                }

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsText.java
----------------------------------------------------------------------
diff --git 
a/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsText.java
 
b/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsText.java
index c36bc9e..e331ed9 100644
--- 
a/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsText.java
+++ 
b/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/WriteFormatAsText.java
@@ -37,12 +37,10 @@ public class WriteFormatAsText<IN> extends WriteFormat<IN> {
 
        @Override
        public void write(String path, ArrayList<IN> tupleList) {
-               try {
-                       PrintWriter outStream = new PrintWriter(new 
BufferedWriter(new FileWriter(path, true)));
+               try (PrintWriter outStream = new PrintWriter(new 
BufferedWriter(new FileWriter(path, true)))) {
                        for (IN tupleToWrite : tupleList) {
                                outStream.println(tupleToWrite);
                        }
-                       outStream.close();
                } catch (IOException e) {
                        throw new RuntimeException("Exception occured while 
writing file " + path, e);
                }

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-tests/src/test/java/org/apache/flink/test/clients/examples/LocalExecutorITCase.java
----------------------------------------------------------------------
diff --git 
a/flink-tests/src/test/java/org/apache/flink/test/clients/examples/LocalExecutorITCase.java
 
b/flink-tests/src/test/java/org/apache/flink/test/clients/examples/LocalExecutorITCase.java
index 5b05b67..4ed28a8 100644
--- 
a/flink-tests/src/test/java/org/apache/flink/test/clients/examples/LocalExecutorITCase.java
+++ 
b/flink-tests/src/test/java/org/apache/flink/test/clients/examples/LocalExecutorITCase.java
@@ -46,9 +46,9 @@ public class LocalExecutorITCase {
                        inFile.deleteOnExit();
                        outFile.deleteOnExit();
                        
-                       FileWriter fw = new FileWriter(inFile);
-                       fw.write(WordCountData.TEXT);
-                       fw.close();
+                       try (FileWriter fw = new FileWriter(inFile)) {
+                               fw.write(WordCountData.TEXT);
+                       }
 
                        LocalExecutor executor = new LocalExecutor();
                        executor.setDefaultOverwriteFiles(true);

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-tests/src/test/java/org/apache/flink/test/iterative/aggregators/AggregatorsITCase.java
----------------------------------------------------------------------
diff --git 
a/flink-tests/src/test/java/org/apache/flink/test/iterative/aggregators/AggregatorsITCase.java
 
b/flink-tests/src/test/java/org/apache/flink/test/iterative/aggregators/AggregatorsITCase.java
index 8b98b29..4c5e955 100644
--- 
a/flink-tests/src/test/java/org/apache/flink/test/iterative/aggregators/AggregatorsITCase.java
+++ 
b/flink-tests/src/test/java/org/apache/flink/test/iterative/aggregators/AggregatorsITCase.java
@@ -91,9 +91,9 @@ public class AggregatorsITCase extends 
MultipleProgramsTestBase {
        @Test
        public void testDistributedCacheWithIterations() throws Exception{
                File tempFile = new File(testPath);
-               FileWriter writer = new FileWriter(tempFile);
-               writer.write(testString);
-               writer.close();
+               try (FileWriter writer = new FileWriter(tempFile)) {
+                       writer.write(testString);
+               }
 
                final ExecutionEnvironment env = 
ExecutionEnvironment.getExecutionEnvironment();
                env.registerCachedFile(resultPath, testName);

http://git-wip-us.apache.org/repos/asf/flink/blob/f06930bc/flink-yarn-tests/src/test/java/org/apache/flink/yarn/YarnTestBase.java
----------------------------------------------------------------------
diff --git 
a/flink-yarn-tests/src/test/java/org/apache/flink/yarn/YarnTestBase.java 
b/flink-yarn-tests/src/test/java/org/apache/flink/yarn/YarnTestBase.java
index 31a3d98..6270010 100644
--- a/flink-yarn-tests/src/test/java/org/apache/flink/yarn/YarnTestBase.java
+++ b/flink-yarn-tests/src/test/java/org/apache/flink/yarn/YarnTestBase.java
@@ -246,10 +246,10 @@ public abstract class YarnTestBase extends TestLogger {
                tmp.create();
                File yarnSiteXML = new File(tmp.newFolder().getAbsolutePath() + 
"/yarn-site.xml");
 
-               FileWriter writer = new FileWriter(yarnSiteXML);
-               yarnConf.writeXml(writer);
-               writer.flush();
-               writer.close();
+               try (FileWriter writer = new FileWriter(yarnSiteXML)) {
+                       yarnConf.writeXml(writer);
+                       writer.flush();
+               }
                return yarnSiteXML;
        }
 

Reply via email to