diff --git a/core/src/main/scala/org/apache/spark/util/Utils.scala b/core/src/main/scala/org/apache/spark/util/Utils.scala index c9c97ba847856..3f01983eb7379 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -2339,13 +2339,25 @@ private[spark] class RedirectThread( * in a circular buffer. The current contents of the buffer can be accessed using * the toString method. */ -private[spark] class CircularBuffer(sizeInByte: Int = 10240) extends java.io.OutputStream { +private[spark] class CircularBuffer(sizeInBytes: Int = 10240) extends java.io.OutputStream { var pos: Int = 0 - var buffer = new Array[Int](sizeInByte / 4) + var buffer = new Array[Byte](sizeInBytes) - def write(i: Int): Unit = { - buffer(pos) = i - pos = (pos + 1) % buffer.size + /** + * Writes the specified byte to this output stream. The general + * contract for [[write]] is that one byte is written + * to the output stream. The byte to be written is the eight + * low-order bits of the argument `i`. The 24 + * high-order bits of `i` are ignored. + * + * Subclasses of [[OutputStream]] must provide an + * implementation for this method. + * + * @param i the byte to be written. + */ + override def write(i: Int): Unit = { + buffer(pos) = i.asInstanceOf[Byte] + pos = (pos + 1) % buffer.length } override def toString: String = { diff --git a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala index a61ea3918f46a..baa4c661cc21e 100644 --- a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala +++ b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala @@ -673,4 +673,12 @@ class UtilsSuite extends SparkFunSuite with ResetSystemProperties with Logging { assert(!Utils.isInDirectory(nullFile, parentDir)) assert(!Utils.isInDirectory(nullFile, childFile3)) } + + test("circular buffer") { + val buffer = new CircularBuffer(25) + val stream = new java.io.PrintStream(buffer, true, "UTF-8") + + stream.println("test circular test circular test circular test circular test circular") + assert(buffer.toString === "t circular test circular\n") + } }