Skip to content

Commit

Permalink
update the CircularBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
chenghao-intel committed Jun 29, 2015
1 parent 692b19e commit 76ff46b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
22 changes: 17 additions & 5 deletions core/src/main/scala/org/apache/spark/util/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 8 additions & 0 deletions core/src/test/scala/org/apache/spark/util/UtilsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 76ff46b

Please sign in to comment.