From 3057b83bdd0db9f222e4aeb4ba9e08e11a1d475a Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Thu, 14 Dec 2017 19:36:18 +0100
Subject: [PATCH] Fix copyrights and NPE.

---
 .../packages/analyzer/FileLineListReader.java | 24 +++++-
 .../packages/analyzer/FileLineReader.java     | 53 ++++++-------
 .../analyzer/FileLineStreamReader.java        | 75 +++++++++++++++++--
 .../analyzer/parser/LogFileParser.java        |  8 +-
 4 files changed, 116 insertions(+), 44 deletions(-)

diff --git a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineListReader.java b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineListReader.java
index c2d9f1d960..aabc942a40 100644
--- a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineListReader.java
+++ b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineListReader.java
@@ -1,3 +1,25 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
 package com.oracle.truffle.r.test.packages.analyzer;
 
 import java.io.IOException;
@@ -12,7 +34,7 @@ public class FileLineListReader extends FileLineReader {
     public FileLineListReader(List<String> lines) {
         Iterator<String> iterator = lines.iterator();
         this.it = iterator;
-        this.empty = iterator.hasNext();
+        this.empty = !iterator.hasNext();
     }
 
     @Override
diff --git a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineReader.java b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineReader.java
index 4d3301e999..adfc223aa6 100644
--- a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineReader.java
+++ b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineReader.java
@@ -1,8 +1,28 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
 package com.oracle.truffle.r.test.packages.analyzer;
 
 import java.io.IOException;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
 
 public abstract class FileLineReader implements Iterable<String> {
 
@@ -10,33 +30,4 @@ public abstract class FileLineReader implements Iterable<String> {
 
     public abstract boolean isEmpty();
 
-    @Override
-    public Iterator<String> iterator() {
-        return new Iterator<String>() {
-
-            private String line = null;
-
-            @Override
-            public boolean hasNext() {
-                try {
-                    if (line == null) {
-                        line = readLine();
-                    }
-                    return line != null;
-                } catch (IOException e) {
-                    // ignore
-                }
-                return false;
-            }
-
-            @Override
-            public String next() {
-                if (hasNext()) {
-                    return line;
-                }
-                throw new NoSuchElementException();
-            }
-        };
-    }
-
 }
diff --git a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineStreamReader.java b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineStreamReader.java
index fec19a56cb..0589724e9f 100644
--- a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineStreamReader.java
+++ b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/FileLineStreamReader.java
@@ -1,7 +1,31 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
 package com.oracle.truffle.r.test.packages.analyzer;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 public class FileLineStreamReader extends FileLineReader {
     /** Maximum number of lines that will be analyzed. */
@@ -9,25 +33,60 @@ public class FileLineStreamReader extends FileLineReader {
 
     private final BufferedReader reader;
     private int cnt = 0;
+    private String lookahead = null;
+    private boolean empty;
 
     public FileLineStreamReader(BufferedReader in) {
         this.reader = in;
+        empty = true;
+        try {
+            nextLine();
+            empty = lookahead == null;
+        } catch (IOException e) {
+            // ignore
+        }
+
     }
 
-    @Override
-    public String readLine() throws IOException {
+    private String nextLine() throws IOException {
         if (cnt++ >= MAX_LINES) {
             throw new IOException("File is too large for analysis.");
         }
-        return reader.readLine();
+        String line = lookahead;
+        lookahead = reader.readLine();
+        return line;
+    }
+
+    @Override
+    public String readLine() throws IOException {
+        return nextLine();
     }
 
     @Override
     public boolean isEmpty() {
-        try {
-            return reader.ready();
-        } catch (IOException e) {
-            return false;
-        }
+        return empty;
     }
+
+    @Override
+    public Iterator<String> iterator() {
+        return new Iterator<String>() {
+
+            @Override
+            public boolean hasNext() {
+                return lookahead != null;
+            }
+
+            @Override
+            public String next() {
+                if (hasNext()) {
+                    try {
+                        return nextLine();
+                    } catch (IOException e) {
+                    }
+                }
+                throw new NoSuchElementException();
+            }
+        };
+    }
+
 }
diff --git a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/parser/LogFileParser.java b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/parser/LogFileParser.java
index 7e93063deb..25abcbbc3e 100644
--- a/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/parser/LogFileParser.java
+++ b/com.oracle.truffle.r.test.packages.analyzer/src/com/oracle/truffle/r/test/packages/analyzer/parser/LogFileParser.java
@@ -189,13 +189,13 @@ public class LogFileParser {
                     // cases)
                     Optional<Path> findFirst = null;
                     findFirst = Files.find(logFile.path.getParent(), 3, (path, attr) -> path.getFileName().equals(outputFile.getFileName())).findFirst();
-                    try (BufferedReader in = Files.newBufferedReader(findFirst.get())) {
-                        if (findFirst.isPresent()) {
+                    if (findFirst.isPresent()) {
+                        try (BufferedReader in = Files.newBufferedReader(findFirst.get())) {
                             ignoreFiles.add(findFirst.get());
                             checkResults.problems.addAll(applyDetectors(Token.OUTPUT_MISMATCH_FASTR, findFirst.get(), 0, new FileLineStreamReader(in)));
+                        } catch (IOException e) {
+                            // silently ignore
                         }
-                    } catch (IOException e) {
-                        // silently ignore
                     }
                     if (findFirst == null || !findFirst.isPresent()) {
                         LOGGER.warning("Cannot read output file " + outputFile);
-- 
GitLab