Skip to content

Commit 76ff46b

Browse files
update the CircularBuffer
1 parent 692b19e commit 76ff46b

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,13 +2339,25 @@ private[spark] class RedirectThread(
23392339
* in a circular buffer. The current contents of the buffer can be accessed using
23402340
* the toString method.
23412341
*/
2342-
private[spark] class CircularBuffer(sizeInByte: Int = 10240) extends java.io.OutputStream {
2342+
private[spark] class CircularBuffer(sizeInBytes: Int = 10240) extends java.io.OutputStream {
23432343
var pos: Int = 0
2344-
var buffer = new Array[Int](sizeInByte / 4)
2344+
var buffer = new Array[Byte](sizeInBytes)
23452345

2346-
def write(i: Int): Unit = {
2347-
buffer(pos) = i
2348-
pos = (pos + 1) % buffer.size
2346+
/**
2347+
* Writes the specified byte to this output stream. The general
2348+
* contract for [[write]] is that one byte is written
2349+
* to the output stream. The byte to be written is the eight
2350+
* low-order bits of the argument `i`. The 24
2351+
* high-order bits of `i` are ignored.
2352+
*
2353+
* Subclasses of [[OutputStream]] must provide an
2354+
* implementation for this method.
2355+
*
2356+
* @param i the byte to be written.
2357+
*/
2358+
override def write(i: Int): Unit = {
2359+
buffer(pos) = i.asInstanceOf[Byte]
2360+
pos = (pos + 1) % buffer.length
23492361
}
23502362

23512363
override def toString: String = {

core/src/test/scala/org/apache/spark/util/UtilsSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,12 @@ class UtilsSuite extends SparkFunSuite with ResetSystemProperties with Logging {
673673
assert(!Utils.isInDirectory(nullFile, parentDir))
674674
assert(!Utils.isInDirectory(nullFile, childFile3))
675675
}
676+
677+
test("circular buffer") {
678+
val buffer = new CircularBuffer(25)
679+
val stream = new java.io.PrintStream(buffer, true, "UTF-8")
680+
681+
stream.println("test circular test circular test circular test circular test circular")
682+
assert(buffer.toString === "t circular test circular\n")
683+
}
676684
}

0 commit comments

Comments
 (0)