Automated rollback of commit 886d7d590fae2f860788a4c8ccb4624e59fc212d.

*** Reason for rollback ***

b/154243114

Fixes https://github.com/bazelbuild/bazel/issues/11125

*** Original change description ***

Use ByteStrings instead of Strings for stdout/err

These are starting as opaque bytes in SynchronizedOutputStream (and/or whatever
comes before that) and finishing as bytes in a proto. No need to take an extra
trip through String which add encoding overhead and room for error.

Drive-by cleanup to delete `discardAll` because it was always false.

Also add a test for SynchronizedOutputStream which documents the minutia of the
current behavior.

RELNOTES: None
PiperOrigin-RevId: 306913995
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java b/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
index e764ad3..2922b69 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
@@ -71,12 +71,12 @@
 import com.google.devtools.build.lib.testutil.FoundationTestCase;
 import com.google.devtools.build.lib.util.DetailedExitCode;
 import com.google.devtools.build.lib.util.ExitCode;
+import com.google.devtools.build.lib.util.Pair;
 import com.google.devtools.build.lib.vfs.Path;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import com.google.devtools.build.lib.vfs.Root;
 import com.google.devtools.build.lib.vfs.util.FileSystems;
 import com.google.devtools.common.options.Options;
-import com.google.protobuf.ByteString;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -746,8 +746,8 @@
         Mockito.mock(BuildEventStreamer.OutErrProvider.class);
     String stdoutMsg = "Some text that was written to stdout.";
     String stderrMsg = "The UI text that bazel wrote to stderr.";
-    when(outErr.getOut()).thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stdoutMsg)));
-    when(outErr.getErr()).thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stderrMsg)));
+    when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg));
+    when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg));
     BuildEvent startEvent =
         new GenericBuildEvent(
             testId("Initial"),
@@ -787,8 +787,8 @@
         Mockito.mock(BuildEventStreamer.OutErrProvider.class);
     String stdoutMsg = "Some text that was written to stdout.";
     String stderrMsg = "The UI text that bazel wrote to stderr.";
-    when(outErr.getOut()).thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stdoutMsg)));
-    when(outErr.getErr()).thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stderrMsg)));
+    when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg));
+    when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg));
     BuildEvent startEvent =
         new GenericBuildEvent(
             testId("Initial"),
@@ -815,51 +815,48 @@
     verify(outErr, times(1)).getErr();
   }
 
-  // private static ImmutableList<ImmutableList<Pair<String, String>>> consumeToLists(
-  //     Iterable<String> left, Iterable<String> right) {
-  //   ImmutableList.Builder<Pair<String, String>> consumerBuilder = ImmutableList.builder();
-  //   ImmutableList.Builder<Pair<String, String>> lastConsumerBuilder = ImmutableList.builder();
-  //
-  //   BuildEventStreamer.consumeAsPairs(
-  //       left,
-  //       right,
-  //       (t1, t2) -> consumerBuilder.add(Pair.of(t1, t2)),
-  //       (t1, t2) -> lastConsumerBuilder.add(Pair.of(t1, t2)));
-  //
-  //   return ImmutableList.of(consumerBuilder.build(), lastConsumerBuilder.build());
-  // }
-  //
-  // @Test
-  // public void testConsumeAsPairs() {
-  //   assertThat(consumeToLists(ImmutableList.of("1", "2", "3"), ImmutableList.of("4", "5", "6")))
-  //       .containsExactly(
-  //           ImmutableList.of(
-  //               Pair.of("1", null), Pair.of("2", null), Pair.of("3", "4"), Pair.of(null, "5")),
-  //           ImmutableList.of(Pair.of(null, "6")))
-  //       .inOrder();
-  //
-  //   assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of()))
-  //       .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, null)))
-  //       .inOrder();
-  //
-  //   assertThat(consumeToLists(ImmutableList.of("1"), ImmutableList.of("2")))
-  //       .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of("1", "2")))
-  //       .inOrder();
-  //
-  //   assertThat(consumeToLists(ImmutableList.of("1"), ImmutableList.of("2", "3")))
-  //       .containsExactly(ImmutableList.of(Pair.of("1", "2")), ImmutableList.of(Pair.of(null,
-  // "3")))
-  //       .inOrder();
-  //
-  //   assertThat(consumeToLists(ImmutableList.of("1", "2"), ImmutableList.of()))
-  //       .containsExactly(ImmutableList.of(Pair.of("1", null)), ImmutableList.of(Pair.of("2",
-  // null)))
-  //       .inOrder();
-  //
-  //   assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of("1")))
-  //       .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, "1")))
-  //       .inOrder();
-  // }
+  private static <T> ImmutableList<ImmutableList<Pair<T, T>>> consumeToLists(
+      Iterable<T> left, Iterable<T> right) {
+    ImmutableList.Builder<Pair<T, T>> consumerBuilder = ImmutableList.builder();
+    ImmutableList.Builder<Pair<T, T>> lastConsumerBuilder = ImmutableList.builder();
+
+    BuildEventStreamer.consumeAsPairs(
+        left,
+        right,
+        (t1, t2) -> consumerBuilder.add(Pair.of(t1, t2)),
+        (t1, t2) -> lastConsumerBuilder.add(Pair.of(t1, t2)));
+
+    return ImmutableList.of(consumerBuilder.build(), lastConsumerBuilder.build());
+  }
+
+  @Test
+  public void testConsumeAsPairs() {
+    assertThat(consumeToLists(ImmutableList.of(1, 2, 3), ImmutableList.of(4, 5, 6)))
+        .containsExactly(
+            ImmutableList.of(Pair.of(1, null), Pair.of(2, null), Pair.of(3, 4), Pair.of(null, 5)),
+            ImmutableList.of(Pair.of(null, 6)))
+        .inOrder();
+
+    assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of()))
+        .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, null)))
+        .inOrder();
+
+    assertThat(consumeToLists(ImmutableList.of(1), ImmutableList.of(2)))
+        .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(1, 2)))
+        .inOrder();
+
+    assertThat(consumeToLists(ImmutableList.of(1), ImmutableList.of(2, 3)))
+        .containsExactly(ImmutableList.of(Pair.of(1, 2)), ImmutableList.of(Pair.of(null, 3)))
+        .inOrder();
+
+    assertThat(consumeToLists(ImmutableList.of(1, 2), ImmutableList.of()))
+        .containsExactly(ImmutableList.of(Pair.of(1, null)), ImmutableList.of(Pair.of(2, null)))
+        .inOrder();
+
+    assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of(1)))
+        .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, 1)))
+        .inOrder();
+  }
 
   @Test
   public void testReportedConfigurations() throws Exception {
@@ -915,11 +912,11 @@
     String secondStdoutMsg = "More text that was written to stdout, still before the start event.";
     String secondStderrMsg = "More text written to stderr, still before the start event.";
     when(outErr.getOut())
-        .thenReturn(ImmutableList.of(ByteString.copyFromUtf8(firstStdoutMsg)))
-        .thenReturn(ImmutableList.of(ByteString.copyFromUtf8(secondStdoutMsg)));
+        .thenReturn(ImmutableList.of(firstStdoutMsg))
+        .thenReturn(ImmutableList.of(secondStdoutMsg));
     when(outErr.getErr())
-        .thenReturn(ImmutableList.of(ByteString.copyFromUtf8(firstStderrMsg)))
-        .thenReturn(ImmutableList.of(ByteString.copyFromUtf8(secondStderrMsg)));
+        .thenReturn(ImmutableList.of(firstStderrMsg))
+        .thenReturn(ImmutableList.of(secondStderrMsg));
     BuildEvent startEvent =
         new GenericBuildEvent(
             testId("Initial"),
@@ -958,14 +955,8 @@
     String firstStderrMsg = "The UI text that bazel wrote to stderr.";
     String secondStdoutMsg = "More text that was written to stdout, still before the start event.";
     String secondStderrMsg = "More text written to stderr, still before the start event.";
-    when(outErr.getOut())
-        .thenReturn(
-            ImmutableList.of(
-                ByteString.copyFromUtf8(firstStdoutMsg), ByteString.copyFromUtf8(secondStdoutMsg)));
-    when(outErr.getErr())
-        .thenReturn(
-            ImmutableList.of(
-                ByteString.copyFromUtf8(firstStderrMsg), ByteString.copyFromUtf8(secondStderrMsg)));
+    when(outErr.getOut()).thenReturn(ImmutableList.of(firstStdoutMsg, secondStdoutMsg));
+    when(outErr.getErr()).thenReturn(ImmutableList.of(firstStderrMsg, secondStderrMsg));
     BuildEvent startEvent =
         new GenericBuildEvent(
             testId("Initial"),
@@ -1008,12 +999,8 @@
         Mockito.mock(BuildEventStreamer.OutErrProvider.class);
     String stdoutMsg = "Some text that was written to stdout.";
     String stderrMsg = "The UI text that bazel wrote to stderr.";
-    when(outErr.getOut())
-        .thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stdoutMsg)))
-        .thenReturn(ImmutableList.of());
-    when(outErr.getErr())
-        .thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stderrMsg)))
-        .thenReturn(ImmutableList.of());
+    when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)).thenReturn(ImmutableList.of());
+    when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)).thenReturn(ImmutableList.of());
     BuildEvent startEvent =
         new GenericBuildEvent(
             testId("Initial"),
@@ -1044,8 +1031,8 @@
         Mockito.mock(BuildEventStreamer.OutErrProvider.class);
     String stdoutMsg = "Some text that was written to stdout.";
     String stderrMsg = "The UI text that bazel wrote to stderr.";
-    when(outErr.getOut()).thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stdoutMsg)));
-    when(outErr.getErr()).thenReturn(ImmutableList.of(ByteString.copyFromUtf8(stderrMsg)));
+    when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg));
+    when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg));
 
     BuildEvent unexpectedStartEvent =
         new GenericBuildEvent(testId("unexpected start"), ImmutableSet.<BuildEventId>of());