@NotNull
  private static PrintStream createStream(@NotNull GroovycOutputParser parser,
                                          @NotNull Key<?> type,
                                          @Nullable("null means not overridden") PrintStream overridden) throws IOException {
    final Thread thread = Thread.currentThread();
    OutputStream out = new OutputStream() {
      ByteArrayOutputStream line = new ByteArrayOutputStream();
      boolean hasLineSeparator = false;

      @Override
      public void write(int b) {
        if (overridden != null && Thread.currentThread() != thread) {
          overridden.write(b);
          return;
        }

        if (hasLineSeparator && !isLineSeparator(b)) {
          flush();
        }
        else {
          hasLineSeparator |= isLineSeparator(b);
        }
        line.write(b);
      }

      private boolean isLineSeparator(int b) {
        return b == '\n' || b == '\r';
      }

      @Override
      public void flush() {
        if (overridden != null && Thread.currentThread() != thread) {
          overridden.flush();
          return;
        }

        if (line.size() > 0) {
          parser.notifyTextAvailable(StringUtil.convertLineSeparators(line.toString(StandardCharsets.UTF_8)), type);
          line = new ByteArrayOutputStream();
          hasLineSeparator = false;
        }
      }
    };
    return new PrintStream(out, false, StandardCharsets.UTF_8);
  }
@NotNull
  private static PrintStream createStream(@NotNull GroovycOutputParser parser,
                                          @NotNull Key<?> type,
                                          @Nullable("null means not overridden") PrintStream overridden)