diff --git a/platform/lang-impl/src/com/intellij/build/output/BuildOutputInstantReaderImpl.kt b/platform/lang-impl/src/com/intellij/build/output/BuildOutputInstantReaderImpl.kt index 8a3abc4484a9..e64c6a5ae259 100644 --- a/platform/lang-impl/src/com/intellij/build/output/BuildOutputInstantReaderImpl.kt +++ b/platform/lang-impl/src/com/intellij/build/output/BuildOutputInstantReaderImpl.kt @@ -23,7 +23,7 @@ class BuildOutputInstantReaderImpl(private val buildId: Any, private val receivedLinesBuffer = LinkedList() private var currentIndex = -1 - private var lineBuilder: StringBuilder? = null + private val lineProcessor: LineProcessor init { val thisReader = this @@ -48,6 +48,19 @@ class BuildOutputInstantReaderImpl(private val buildId: Any, } } } + + lineProcessor = object : LineProcessor() { + override fun process(line: String) { + if (job.isCompleted) { + LOG.warn("Build output reader closed") + return + } + if (!job.isActive) { + job.start() + } + runBlocking { channel.send(line) } + } + } } override fun getBuildId(): Any { @@ -55,59 +68,28 @@ class BuildOutputInstantReaderImpl(private val buildId: Any, } override fun append(csq: CharSequence): BuildOutputInstantReaderImpl { - for (i in 0 until csq.length) { - append(csq[i]) - } + lineProcessor.append(csq) return this } override fun append(csq: CharSequence, start: Int, end: Int): BuildOutputInstantReaderImpl { - append(csq.subSequence(start, end)) + lineProcessor.append(csq, start, end) return this } override fun append(c: Char): BuildOutputInstantReaderImpl { - if (lineBuilder == null) { - lineBuilder = StringBuilder() - } - if (c == '\n') { - runBlocking { - doFlush() - } - } - else { - lineBuilder!!.append(c) - } + lineProcessor.append(c) return this } override fun close() { runBlocking { - doFlush() + lineProcessor.close() channel.close() job.cancelAndJoin() } } - private suspend fun doFlush() { - if (lineBuilder == null) { - return - } - if (job.isCompleted) { - LOG.warn("Build output reader closed") - lineBuilder!!.setLength(0) - return - } - - val line = lineBuilder!!.toString() - lineBuilder!!.setLength(0) - - if (!job.isActive) { - job.start() - } - channel.send(line) - } - override fun readLine(): String? { if (currentIndex < -1) { LOG.error("Wrong buffered output lines index") diff --git a/platform/lang-impl/src/com/intellij/build/output/LineProcessor.kt b/platform/lang-impl/src/com/intellij/build/output/LineProcessor.kt new file mode 100644 index 000000000000..d58d3d260da4 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/build/output/LineProcessor.kt @@ -0,0 +1,45 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.build.output + +import java.io.Closeable + +abstract class LineProcessor : Appendable, Closeable { + private var lineBuilder: StringBuilder? = null + + abstract fun process(line: String) + + override fun append(csq: CharSequence): LineProcessor { + for (i in 0 until csq.length) { + append(csq[i]) + } + return this + } + + override fun append(csq: CharSequence, start: Int, end: Int): LineProcessor { + append(csq.subSequence(start, end)) + return this + } + + override fun append(c: Char): LineProcessor { + if (lineBuilder == null) { + lineBuilder = StringBuilder() + } + if (c == '\n') { + flushBuffer() + } + else { + lineBuilder!!.append(c) + } + return this + } + + override fun close() { + flushBuffer() + } + + private fun flushBuffer() { + val line = lineBuilder?.toString() ?: return + lineBuilder!!.setLength(0) + process(line) + } +}