Fixing checkstyle test hang with nc version >= 1.110

netcat (nc) 1.110 changed the behavior of how sockets are closed when
stdin is at EOF or closed. Previously, nc would call shutdown (TCP FIN
on the write side of the socket) when the end of stdin was reached.
Now, shutdown is only called if '-N' is passed as an argument.

This change was introduced into Ubuntu's fork of OpenBSD nc in Nov. 2016.
So, the affected versions were Ubuntu 17.04+ as well as any other
distribution that uses nc >= 1.110.

This change of behavior causes the call to ByteStreams.toByteArray()
to hang indefinietly, and thus checkstyle tests to hang indefinitely.

Rather than try to figure out which version of nc is present and set
the -N option, we will use an empty line as a sentinel and stop parsing
input when the first empty line is encountered. For this, we need two
changes: (1) send a newline when checking the socket in start-buck-daemon
and (2) send a newline at the end of the file list in onos.bucklet

We also set SO_TIMEOUT to 1 second and will return an exception if
the socket times out. This will prevent tests from hanging indefinitely.

Change-Id: If46b4b78ae89312e1afa0563f63100ae67762f0a
diff --git a/tools/build/conf/src/main/java/org/onosproject/buckdaemon/BuckTaskContext.java b/tools/build/conf/src/main/java/org/onosproject/buckdaemon/BuckTaskContext.java
index 7e2046f..b8d189c 100644
--- a/tools/build/conf/src/main/java/org/onosproject/buckdaemon/BuckTaskContext.java
+++ b/tools/build/conf/src/main/java/org/onosproject/buckdaemon/BuckTaskContext.java
@@ -17,11 +17,12 @@
 package org.onosproject.buckdaemon;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.io.ByteStreams;
+import com.google.common.collect.Lists;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
+import java.io.InputStreamReader;
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -33,17 +34,36 @@
 
     private final String taskName;
     private final ImmutableList<String> input;
-    private final List<String> output = new ArrayList<>();
+    private final List<String> output;
 
-    BuckTaskContext(InputStream inputString) throws IOException {
-        String[] split = new String(ByteStreams.toByteArray(inputString)).split("\n");
-        checkArgument(split.length >= 1, "Request must contain at least task type");
-        this.taskName = split[0];
-        ImmutableList.Builder<String> builder = ImmutableList.builder();
-        for (int i = 1; i < split.length; i++) {
-            builder.add(split[i]);
+    BuckTaskContext(InputStream inputStream) throws IOException {
+        ImmutableList<String> lines = slurpInput(inputStream);
+        checkArgument(lines.size() >= 1 && !lines.get(0).isEmpty(),
+                "Request must contain at least task type");
+        this.taskName = lines.get(0);
+        this.input = lines.subList(1, lines.size());
+        this.output = Lists.newArrayList();
+    }
+
+    /**
+     * Reads all input, line by line, from a stream until an empty line or EOF is encountered.
+     *
+     * @param stream input stream
+     * @return the lines of the input
+     * @throws IOException
+     */
+    private static ImmutableList<String> slurpInput(InputStream stream) throws IOException {
+        ImmutableList.Builder<String> lines = ImmutableList.builder();
+        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
+        while(true) {
+            String line = bufferedReader.readLine();
+            if (line == null || line.trim().length() == 0) {
+                // Empty line or EOF
+                break;
+            }
+            lines.add(line);
         }
-        input = builder.build();
+        return lines.build();
     }
 
     /**